Java 中的集合类
Java 集合框架由用于存储对象的各种类组成。顶部的这些类实现了集合接口。一些类提供了可以直接使用的完整实现。其他的是抽象类,它提供了骨架实现,可以作为创建具体集合的起点。
Java 集合框架类
此表包含实现集合接口的抽象和非抽象类。
标准集合类有:
| 班级 | 描述 |
| 抽象集合 | 实现大多数集合接口。 |
| AbstractList
| 扩展抽象集合并实现大部分列表接口。 |
| 抽象队列 | 扩展抽象集合并实现部分Queue
接口。 |
| AbstractSequentialList
| 扩展AbstractList
,供使用其元素的顺序访问而不是随机访问的集合使用。 |
| LinkedList
| 通过扩展AbstractSequentialList
实现LinkedList
|
| ArrayList
| 通过扩展AbstractList
实现动态数组 |
| ArrayDeque(数组) | 通过扩展 AbstractCollection 和实现 Deque 接口(由 Java SE 6 添加)来实现动态双端队列。 |
| 抽象集 | 扩展抽象集合并实现集合接口的大部分。 |
| enumset 集 | 扩展抽象集以用于枚举元素。 |
| HashSet
| 扩展抽象集用于HashMap
。 |
| LinkedHashSet | 扩展 HashSet 以允许插入顺序迭代。 |
| 优先级队列 | 扩展抽象队列以支持基于优先级的队列。 |
| TreeSet(TreeSet
) | 实现存储在树中的集合。扩展抽象集。 |
注意:
- 要在程序中使用任何 Collection 类,需要导入 java.util 包。
- 无论何时打印任何集合类,都会在方括号[]内打印它及其元素。
ArrayList
类
此类提供基于数组的数据结构的实现,该数据结构用于以线性顺序存储元素。这个类实现了列表接口和一个抽象的AbstractList
类。它创建了一个基于元素强度增长的动态数组。
简单数组有固定的大小,即它可以存储固定数量的元素,但有时您可能事先不知道要存储在数组中的元素数量。在这种情况下,我们可以使用ArrayList
,这是一个大小可以动态增减的数组。
- ArrayList 类扩展了
AbstractList
类,实现了列表接口。 - ArrayList 支持动态数组,可以根据需要增长。
- 它可以包含重复元素,并且还保持插入顺序。
- 操作很慢,因为如果从
ArrayList
中删除任何元素,都需要进行大量的移位。 ArrayList
不同步。- ArrayList 允许随机访问,因为它基于索引工作。
ArrayList
构造器
Arraylist 类有三个构造器,可用于从空集合或其他集合的元素创建 ArrayList。
ArrayList() // It creates an empty ArrayList
ArrayList( Collection C ) // It creates an ArrayList that is initialized with elements of the Collection C
ArrayList( int capacity ) // It creates an ArrayList that has the specified initial capacity
ArrayList
示例
让我们创建一个ArrayList
来存储字符串元素。看,我们使用列表界面的 add 方法来添加元素。
import java.util.*;
class Demo
{
public static void main(String[] args)
{
ArrayList< String> al = new ArrayList< String>();
al.add("ab");
al.add("bc");
al.add("cd");
System.out.println(al);
}
}
[ab,bc,cd]
LinkedList
类
Java LinkedList 类提供了LinkedList
数据结构的实现。它使用双向LinkedList
来存储元素。
- LinkedList 类扩展了 AbstractSequentialList,实现了 List、dequeue 和 Queue 接口。
- 它可以用作列表、栈或队列,因为它实现了所有相关的接口。
- 它本质上是动态的,即在需要时分配内存。因此,可以容易地实现插入和删除操作。
- 它可以包含重复的元素,并且不同步。
- 在
LinkedList
中反向 t 遍历是困难的。 - 在
LinkedList
中,操作很快,因为不需要发生移位。
LinkedList
构造器
LinkedList 类有两个构造器。
LinkedList() // It creates an empty LinkedList
LinkedList( Collection c) // It creates a LinkedList that is initialized with elements of the Collection c
LinkedList
类示例
让我们举一个例子来创建一个LinkedList
,并使用 add 和其他方法添加元素。见下面的例子。
import java.util.* ;
class Demo
{
public static void main(String[] args)
{
LinkedList< String> ll = new LinkedList< String>();
ll.add("a");
ll.add("b");
ll.add("c");
ll.addLast("z");
ll.addFirst("A");
System.out.println(ll);
}
}
[A,A,b,c,z]
ArrayList
和LinkedList
的区别
ArrayList
和LinkedList
是集合类,都实现了 List 接口。ArrayList 类创建一个列表,该列表内部存储在一个动态数组中,随着元素的添加或删除,该数组的大小会增大或缩小。LinkedList
还会创建一个列表,该列表内部存储在一个双LinkedList
中。这两个类都用于存储列表中的元素,但是这两个类之间的主要区别是ArrayList
允许随机访问列表中的元素,因为它是在基于索引的数据结构上运行的。另一方面,LinkedList 不允许随机访问,因为它没有直接访问元素的索引,它必须遍历列表才能从列表中检索或访问元素。
更多不同之处:
- ArrayList 扩展了 AbstarctList 类,而 LinkedList 扩展了 AbstractSequentialList。
AbstractList
实现列表接口,因此它只能作为一个列表,而LinkedList
实现列表、去队列和Queue
接口,因此它可以同时作为一个队列和列表。- 在列表中,访问
ArrayList
中的元素更快,因为随机访问也是可能的。对LinkedList
元素的访问较慢,因为它只遵循顺序访问。 - 在列表中,
ArrayList
中的元素操作速度较慢,而LinkedList
中的操作速度较快。
HashSet 类
- HashSet 扩展了抽象集类,实现了集接口。
- HashSet 有三个构造器。
HashSet() *//This creates an empty HashSet*
HashSet( Collection C ) *//This creates a HashSet that is initialized with the elements of the Collection C*
HashSet( int capacity ) *//This creates a HashSet that has the specified initial capacity*
- 它创建一个使用
HashMap
进行存储的集合。HashMap
通过一种叫做哈希的机制来存储信息。 - 在哈希中,密钥的信息内容用于确定唯一的值,称为其哈希代码。然后,哈希码被用作存储与密钥相关联的数据的索引。
- HashSet 不维护元素的任何顺序。
- HashSet 只包含唯一的元素。
HashSet 类的示例
在这个例子中,我们创建了一个存储字符串值的 HashSet。由于 HashSet 不存储重复的元素,我们尝试添加一个重复的元素,但是输出只包含唯一的元素。
import java.util.*;
class Demo
{
public static void main(String args[])
{
HashSet<String> hs = new HashSet<String>();
hs.add("B");
hs.add("A");
hs.add("D");
hs.add("E");
hs.add("C");
hs.add("A");
System.out.println(hs);
}
}
[甲、乙、丙、丁、戊]
LinkedHashSet Class
- LinkedHashSet 类扩展了 HashSet 类
- LinkedHashSet 维护集合中条目的
LinkedList
。 - LinkedHashSet 按照元素插入的顺序存储元素,即它保持插入顺序。
LinkedHashSet 类示例
import java.util.*;
class Demo
{
public static void main(String args[])
{
LinkedHashSet<String> hs = new LinkedHashSet<String>();
hs.add("B");
hs.add("A");
hs.add("D");
hs.add("E");
hs.add("C");
hs.add("F");
System.out.println(hs);
}
}
[乙、甲、丁、戊、丙、己]
TreeSet
合类
- 它扩展了抽象集类,实现了导航集接口。
- 它以升序存储元素。
- 它使用树形结构来存储元素。
- 它只包含像 HashSet 这样的独特元素。
- 它的存取和检索时间相当快。
- 它有四个构造器。
TreeSet() *//It creates an empty tree set that will be sorted in an ascending order according to the
natural order of the tree set*
TreeSet( Collection *C* ) *//It creates a new tree set that contains the elements of the Collection C*
TreeSet( Comparator *comp* ) *//It creates an empty tree set that will be sorted according to given Comparator*
TreeSet( SortedSet *ss* ) *//It creates a TreeSet that contains the elements of given SortedSet*
TreeSet 类的示例
让我们举一个例子来创建一个包含重复元素的TreeSet
。但是您可以注意到它打印独特的元素,这意味着它不允许重复的元素。
import java.util.*;
class Demo{
public static void main(String args[]){
TreeSet<String> al=new TreeSet<String>();
al.add("Ravi");
al.add("Vijay");
al.add("Ravi");
al.add("Ajay");
Iterator itr=al.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}
Ajay Ravi Vijay
优先级队列类
- 它扩展了抽象队列类。
- PriorityQueue 类提供了使用队列的功能。
- 它不以先进先出的方式对元素进行排序。
优先级队列有六个构造器。在所有情况下,容量都会随着元素的添加而自动增长。
**PriorityQueue**( ) *//This constructor creates an empty queue. By default, its starting capacity is 11* **PriorityQueue**(int capacity) *//This constructor creates a queue that has the specified initial capacity* **PriorityQueue**(int capacity, Comparator comp) *//This constructor creates a queue with the specified capacity and comparator* *//The last three constructors create queues that are initialized with elements of Collection passed in c* **PriorityQueue**(*Collection* c) **PriorityQueue**(*PriorityQueue* c) **PriorityQueue**(*SortedSet* c)
注意:如果在构建优先级队列时没有指定Comparator
,则使用队列中存储的数据类型的默认Comparator
。默认Comparator
将按升序排列队列。因此,队列的头部将是最小值。但是,通过提供自定义Comparator
,您可以指定不同的排序方案。
优先级队列类的示例
让我们举一个例子来创建一个存储和移除元素的优先级队列。
import java.util.*;
class Demo
{
public static void main(String args[])
{
PriorityQueue<String> queue=new PriorityQueue<String>();
queue.add("WE");
queue.add("LOVE");
queue.add("STUDY");
queue.add("TONIGHT");
System.out.println("At head of the queue:"+queue.element());
System.out.println("At head of the queue:"+queue.peek());
System.out.println("Iterating the queue elements:");
Iterator itr=queue.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
queue.remove();
queue.poll();
System.out.println("After removing two elements:");
Iterator itr2=queue.iterator();
while(itr2.hasNext()){
System.out.println(itr2.next());
}
}
}
在队列的最前面:爱在队列的最前面:爱迭代队列元素:爱今晚研究我们在移除两个元素后:今晚我们