Java中的Collection类

Java collection framework consists of various classes that are used to store objects. These classes on the top implements the Collection interface. Some of the classes provide full implementations that can be used as it is. Others are abstract classes, which provides skeletal implementations that can be used as a starting point for creating concrete collections.

Java收集框架由用于存储对象的各种类组成。 顶部的这些类实现了Collection接口。 一些类提供了可以按原样使用的完整实现。 其他类是抽象类,提供了可作为创建具体集合的起点的骨架实现。

Java Collection Framework类 (Java Collection Framework Classes)

This table contains abstract and non-abstract classes that implements collection interface.

该表包含实现集合接口的抽象类和非抽象类。

The standard collection classes are:

标准的收集类是:

ClassDescription
AbstractCollection Implements most of the Collection interface.
AbstractListExtends AbstractCollection and implements most of the List interface.
AbstractQueueExtends AbstractCollection and implements parts of the Queue interface.
AbstractSequentialListExtends AbstractList for use by a collection that uses sequential rather than random access of its elements.
LinkedListImplements a linked list by extending AbstractSequentialList
ArrayListImplements a dynamic array by extending AbstractList
ArrayDequeImplements a dynamic double-ended queue by extending AbstractCollection and implementing the Deque interface(Added by Java SE 6).
AbstractSetExtends AbstractCollection and implements most of the Set interface.
EnumSetExtends AbstractSet for use with enum elements.
HashSetExtends AbstractSet for use with a hash table.
LinkedHashSetExtends HashSet to allow insertion-order iterations.
PriorityQueueExtends AbstractQueue to support a priority-based queue.
TreeSetImplements a set stored in a tree. Extends AbstractSet.
描述
抽象馆藏 实现大多数Collection接口。
摘要清单 扩展AbstractCollection并实现大多数List接口。
抽象队列 扩展AbstractCollection并实现Queue接口的各个部分。
AbstractSequentialList 扩展AbstractList供由使用其元素的顺序访问而不是随机访问的集合使用。
链表 通过扩展AbstractSequentialList实现链接列表
数组列表 通过扩展AbstractList实现动态数组
ArrayDeque 通过扩展AbstractCollection并实现Deque接口(由Java SE 6添加)来实现动态双端队列。
抽象集 扩展AbstractCollection并实现大多数Set接口。
枚举集 扩展AbstractSet以供枚举元素使用。
哈希集 扩展AbstractSet以与哈希表一起使用。
链接哈希集 扩展HashSet以允许插入顺序迭代。
PriorityQueue 扩展AbstractQueue以支持基于优先级的队列。
树集 实现存储在树中的集合。 扩展AbstractSet。
注意: (Note:)
  1. To use any Collection class in your program, you need to import java.util package.

    要在程序中使用任何Collection类,您需要导入java.util包。

  2. Whenever you print any Collection class, it gets printed inside the square brackets [] with its elements.

    每当您打印任何Collection类时,该类都会随其元素一起打印在方括号[]中。

ArrayList类 (ArrayList class)

This class provides implementation of an array based data structure that is used to store elements in linear order. This class implements List interface and an abstract AbstractList class. It creates a dynamic array that grows based on the elements strength.

此类提供基于数组的数据结构的实现,该数据结构用于按线性顺序存储元素。 此类实现List接口和抽象AbstractList类。 它创建一个动态数组,该数组根据元素的强度而增长。

Simple array has fixed size i.e it can store fixed number of elements but sometimes you may not know beforehand about the number of elements that you are going to store in your array. In such situations, We can use an ArrayList, which is an array whose size can increase or decrease dynamically.

简单数组的大小是固定的,即它可以存储固定数量的元素,但是有时您可能事先不知道要存储在数组中的元素数量。 在这种情况下,我们可以使用ArrayList,它是一个大小可以动态增加或减少的数组。

  1. ArrayList class extends AbstractList class and implements the List interface.

    ArrayList类扩展AbstractList类并实现List接口。

  2. ArrayList supports dynamic array that can grow as needed.

    ArrayList支持可以根据需要增长的动态数组。

  3. It can contain Duplicate elements and it also maintains the insertion order.

    它可以包含Duplicate元素,并且还可以保持插入顺序。

  4. Manipulation is slow because a lot of shifting needs to be occurred if any element is removed from the array list.

    操作很慢,因为如果将任何元素从数组列表中删除,则需要进行很多移位。

  5. ArrayLists are not synchronized.

    ArrayList不同步。

  6. ArrayList allows random access because it works on the index basis.

    ArrayList允许随机访问,因为它基于索引工作。

ArrayList构造函数 (ArrayList Constructors)

ArrayList class has three constructors that can be used to create Arraylist either from empty or elements of other collection.

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的例子 (Example of ArrayList)

Lets create an ArrayList to store string elements. See, we used add method of list interface to add elements.

让我们创建一个ArrayList来存储字符串元素。 看到,我们使用list接口的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]

[ab,bc,cd]

LinkedList类 (LinkedList class)

Java LinkedList class provides implementation of linked-list data structure. It used doubly linked list to store the elements.

Java LinkedList类提供了链表数据结构的实现。 它使用了双向链表来存储元素。

  1. LinkedList class extends AbstractSequentialList and implements List,Deque and Queue inteface.

    LinkedList类扩展AbstractSequentialList并实现List,Deque和Queue接口。

  2. It can be used as List, stack or Queue as it implements all the related interfaces.

    它可以实现所有相关接口,因此可以用作列表,堆栈或队列。

  3. It is dynamic in nature i.e it allocates memory when required. Therefore insertion and deletion operations can be easily implemented.

    它本质上是动态的,即在需要时分配内存。 因此,插入和删除操作可以容易地实现。

  4. It can contain duplicate elements and it is not synchronized.

    它可以包含重复的元素,并且不同步。

  5. Reverse Traversing is difficult in linked list.

    在链表中,反向遍历比较困难。

  6. In LinkedList, manipulation is fast because no shifting needs to be occurred.

    在LinkedList中,操作是快速的,因为不需要进行移位。

LinkedList构造函数 (LinkedList Constructors)

LinkedList class has two constructors.

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 class Example)

Lets take an example to create a linked-list and add elements using add and other methods as well. See the below example.

让我们举个例子来创建一个链表并使用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]

[A,a,b,c,z]

ArrayList和链接列表之间的区别 (Difference between ArrayList and Linked List)

ArrayList and LinkedList are the Collection classes, and both of them implements the List interface. The ArrayList class creates the list which is internally stored in a dynamic array that grows or shrinks in size as the elements are added or deleted from it. LinkedList also creates the list which is internally stored in a DoublyLinked List. Both the classes are used to store the elements in the list, but the major difference between both the classes is that ArrayList allows random access to the elements in the list as it operates on an index-based data structure. On the other hand, the LinkedList does not allow random access as it does not have indexes to access elements directly, it has to traverse the list to retrieve or access an element from the list.

ArrayListLinkedList是Collection类,它们都实现List接口。 ArrayList类创建一个列表,该列表内部存储在一个动态数组中,该数组的大小会随着元素的添加或删除而增大或缩小。 LinkedList还会创建内部存储在DoublyLinked List中的列表。 这两个类都用于存储列表中的元素,但是这两个类之间的主要区别在于,由于ArrayList对基于索引的数据结构进行操作,因此可以随机访问列表中的元素。 另一方面,LinkedList不允许随机访问,因为它没有直接访问元素的索引,它必须遍历列表以从列表中检索或访问元素。

Some more differences:

更多区别:

  • ArrayList extends AbstarctList class whereas LinkedList extends AbstractSequentialList.

    ArrayList扩展了AbstarctList类,而LinkedList扩展了AbstractSequentialList。

  • AbstractList implements List interface, thus it can behave as a list only whereas LinkedList implements List, Deque and Queue interface, thus it can behave as a Queue and List both.

    AbstractList实现List接口,因此它只能充当列表,而LinkedList实现List,Deque和Queue接口,因此可以充当Queue和List。

  • In a list, access to elements is faster in ArrayList as random access is also possible. Access to LinkedList elements is slower as it follows sequential access only.

    在列表中,在ArrayList中访问元素的速度更快,因为也可以进行随机访问。 由于仅遵循顺序访问,因此对LinkedList元素的访问较慢。

  • In a list, manipulation of elements is slower in ArrayList whereas it is faster in LinkedList.

    在列表中,元素的操作在ArrayList中较慢,而在LinkedList中则较快。

HashSet类 (HashSet class)

  1. HashSet extends AbstractSet class and implements the Set interface.

    HashSet扩展AbstractSet类并实现Set接口。

  2. HashSet has three constructors.

    HashSet具有三个构造函数。

  3. 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
  4. It creates a collection that uses hash table for storage. A hash table stores information by using a mechanism called hashing.

    它创建一个使用哈希表进行存储的集合。 哈希表通过使用称为哈希的机制来存储信息。

  5. In hashing, the informational content of a key is used to determine a unique value, called its hash code. The hash code is then used as the index at which the data associated with the key is stored.

    在散列中,密钥的信息内容用于确定唯一值,称为其散列码。 然后,将哈希码用作存储与密钥关联的数据的索引。

  6. HashSet does not maintain any order of elements.

    HashSet不维护元素的任何顺序。

  7. HashSet contains only unique elements.

    HashSet仅包含唯一元素。

HashSet类的示例 (Example of HashSet class)

In this example, we are creating a HashSet that store string values. Since HashSet does not store duplicate elements, we tried to add a duplicate elements but the output contains only unique elements.

在此示例中,我们将创建一个存储字符串值的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);
  }
}

[A, B, C, D, E]

[A,B,C,D,E]

LinkedHashSet类 (LinkedHashSet Class)

  1. LinkedHashSet class extends HashSet class

    LinkedHashSet类扩展了HashSet

  2. LinkedHashSet maintains a linked list of entries in the set.

    LinkedHashSet维护集合中条目的链接列表。

  3. LinkedHashSet stores elements in the order in which elements are inserted i.e it maintains the insertion order.

    LinkedHashSet按插入元素的顺序存储元素,即,它保持插入顺序。

LinkedHashSet类的示例 (Example of LinkedHashSet class)

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);
  }
}

[B, A, D, E, C, F]

[B,A,D,E,C,F]

TreeSet类 (TreeSet Class)

  1. It extends AbstractSet class and implements the NavigableSet interface.

    它扩展了AbstractSet类并实现了NavigableSet接口。

  2. It stores the elements in ascending order.

    它按升序存储元素。

  3. It uses a Tree structure to store elements.

    它使用Tree结构存储元素。

  4. It contains unique elements only like HashSet.

    它仅包含像HashSet这样的唯一元素。

  5. It's access and retrieval times are quite fast.

    它的访问和检索时间都非常快。

  6. It has four Constructors.

    它具有四个构造函数。

  7. 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类的示例 (Example of TreeSet class)

Lets take an example to create a treeset that contains duplicate elements. But you can notice that it prints unique elements that means it does not allow duplicate elements.

让我们以创建包含重复元素的树集为例。 但是您会注意到,它打印出唯一的元素,这意味着它不允许重复的元素。

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

阿杰·拉维·维杰(Ajay Ravi Vijay)

PriorityQueue类 (PriorityQueue Class)

  1. It extends the AbstractQueue class.

    它扩展了AbstractQueue类。

  2. The PriorityQueue class provides the facility of using queue.

    PriorityQueue类提供使用队列的便利。

  3. It does not orders the elements in FIFO manner.

    它不以FIFO方式对元素进行排序。

  4. PriorityQueue has six constructors. In all cases, the capacity grows automatically as elements are added.

    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)

Note: If no comparator is specified when a PriorityQueue is constructed, then the default comparator for the type of data stored in the queue is used. The default comparator will order the queue in ascending order. Thus, the head of the queue will be the smallest value. However, by providing a custom comparator, you can specify a different ordering scheme.

注意:如果在构造PriorityQueue时未指定比较器,则使用队列中存储的数据类型的默认比较器。 默认比较器将按升序对队列进行排序。 因此,队列的开头将是最小值。 但是,通过提供自定义比较器,可以指定其他排序方案。

PriorityQueue类的示例 (Example of PriorityQueue class)

Lets take an example to create a priority queue that store and remove elements.

让我们以创建存储和删除元素的优先级队列为例。

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());
    }
  }
}

At head of the queue:LOVE At head of the queue:LOVE Iterating the queue elements: LOVE TONIGHT STUDY WE After removing two elements: TONIGHT WE

在队列开头:LOVE在队列开头:LOVE迭代队列元素:LOVE TONIGHT STUDY WE删除两个元素之后:TONIGHT WE

翻译自: https://www.studytonight.com/java/collection-classes.php

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值