我应该使用什么Java集合?

时间:2022-09-11 11:59:19

In this question How can I efficiently select a Standard Library container in C++11? is a handy flow chart to use when choosing C++ collections.

在这个问题中,如何有效地选择c++ 11中的标准库容器?在选择c++集合时,可以使用一个方便的流程图。

I thought that this was a useful resource for people who are not sure which collection they should be using so I tried to find a similar flow chart for Java and was not able to do so.

我认为,对于那些不确定应该使用哪个集合的人来说,这是一个有用的资源,因此我试图为Java找到一个类似的流程图,但未能找到。

What resources and "cheat sheets" are available to help people choose the right Collection to use when programming in Java? How do people know what List, Set and Map implementations they should use?

有哪些资源和“备忘单”可以帮助人们在Java编程时选择正确的集合?人们如何知道他们应该使用什么列表、设置和映射实现?

4 个解决方案

#1


213  

Since I couldn't find a similar flowchart I decided to make one myself.

因为我找不到一个类似的流程图,所以我决定自己做一个。

This flow chart does not try and cover things like synchronized access, thread safety etc or the legacy collections, but it does cover the 3 standard Sets, 3 standard Maps and 2 standard Lists.

这个流程图没有尝试并涵盖同步访问、线程安全等内容或遗留集合,但是它涵盖了3个标准集、3个标准映射和2个标准列表。

我应该使用什么Java集合?

This image was created for this answer and is licensed under a Creative Commons Attribution 4.0 International License. The simplest attribution is by linking to either this question or this answer.

这张图片是为这个答案而创建的,并在Creative Commons attributed4.0 International License下获得许可。最简单的归因是通过链接到这个问题或者这个答案。

Other resources

其他资源

Probably the most useful other reference is the following page from the oracle documentation which describes each Collection.

可能最有用的其他引用是描述每个集合的oracle文档中的以下页面。

HashSet vs TreeSet

HashSet vs TreeSet

There is a detailed discussion of when to use HashSet or TreeSet here: Hashset vs Treeset

这里有一个关于何时使用HashSet或TreeSet的详细讨论:HashSet vs TreeSet

ArrayList vs LinkedList

ArrayList vs LinkedList

Detailed discussion: When to use LinkedList over ArrayList?

详细讨论:什么时候使用LinkedList而不是ArrayList?

#2


44  

Summary of the major non-concurrent, non-synchronized collections

Collection: An interface representing an unordered "bag" of items, called "elements". The "next" element is undefined (random).

集合:表示无序“包”项的接口,称为“元素”。“next”元素是未定义的(随机的)。

  • Set: An interface representing a Collection with no duplicates.
    • HashSet: A Set backed by a Hashtable. Fastest and smallest memory usage, when ordering is unimportant.
    • HashSet:由Hashtable支持的集合。当排序不重要时,最快和最小的内存使用。
    • LinkedHashSet: A HashSet with the addition of a linked list to associate elements in insertion order. The "next" element is the next-most-recently inserted element.
    • LinkedHashSet:添加一个链表以将元素插入到插入顺序中的一个HashSet。“next”元素是下一个最近插入的元素。
    • TreeSet: A Set where elements are ordered by a Comparator (typically natural ordering). Slowest and largest memory usage, but necessary for comparator-based ordering.
    • TreeSet:由比较器(通常是自然排序)排序元素的集合。最慢和最大的内存使用,但是对于基于比较器的排序是必需的。
    • EnumSet: An extremely fast and efficient Set customized for a single enum type.
    • 枚举集:为单个枚举类型定制的极其快速和高效的集合。
  • 集合:表示没有重复集合的接口。HashSet:由Hashtable支持的集合。当排序不重要时,最快和最小的内存使用。LinkedHashSet:添加一个链表以将元素插入到插入顺序中的一个HashSet。“下一个”元素是最近插入的元素。TreeSet:由比较器(通常是自然排序)排序元素的集合。最慢和最大的内存使用,但是对于基于比较器的排序是必需的。枚举集:为单个枚举类型定制的极其快速和高效的集合。
  • List: An interface representing a Collection whose elements are ordered and each have a numeric index representing its position, where zero is the first element, and (length - 1) is the last.
    • ArrayList: A List backed by an array, where the array has a length (called "capacity") that is at least as large as the number of elements (the list's "size"). When size exceeds capacity (when the (capacity + 1)-th element is added), the array is recreated with a new capacity of (new length * 1.5)--this recreation is fast, since it uses System.arrayCopy(). Deleting and inserting/adding elements requires all neighboring elements (to the right) be shifted into or out of that space. Accessing any element is fast, as it only requires the calculation (element-zero-address + desired-index * element-size) to find it's location. In most situations, an ArrayList is preferred over a LinkedList.
    • ArrayList:数组支持的列表,其中数组的长度(称为“容量”)至少与元素的数量(列表的“大小”)相同。当大小超过容量时(当(容量+ 1)-th元素被添加时),该数组将重新创建(新长度* 1.5),因为它使用System.arrayCopy()。删除和插入/添加元素需要将所有相邻的元素(右)移到该空间中或从该空间中移出。访问任何元素都是快速的,因为它只需要计算(元素零地址+需求索引*元素大小)就可以找到它的位置。在大多数情况下,ArrayList优于LinkedList。
    • LinkedList: A List backed by a set of objects, each linked to its "previous" and "next" neighbors. A LinkedList is also a Queue and Deque. Accessing elements is done starting at the first or last element, and traversing until the desired index is reached. Insertion and deletion, once the desired index is reached via traversal is a trivial matter of re-mapping only the immediate-neighbor links to point to the new element or bypass the now-deleted element.
    • LinkedList:由一组对象支持的列表,每个对象都链接到它的“前”和“下”邻居。LinkedList也是一个队列和Deque。访问元素是从第一个或最后一个元素开始的,然后遍历,直到达到所需的索引。插入和删除,一旦通过遍历到达所需的索引,只需重新映射直接邻居链接到新元素或绕过现在删除的元素即可。
  • 列表:一个表示集合的接口,它的元素是有序的,每个元素都有一个表示其位置的数字索引,其中0是第一个元素,而(长度- 1)是最后一个元素。ArrayList:数组支持的列表,其中数组的长度(称为“容量”)至少与元素的数量(列表的“大小”)相同。当大小超过容量时(当(容量+ 1)-th元素被添加时),该数组将重新创建(新长度* 1.5),因为它使用System.arrayCopy()。删除和插入/添加元素需要将所有相邻的元素(右)移到该空间中或从该空间中移出。访问任何元素都是快速的,因为它只需要计算(元素零地址+需求索引*元素大小)就可以找到它的位置。在大多数情况下,ArrayList优于LinkedList。LinkedList:由一组对象支持的列表,每个对象都链接到它的“前”和“下”邻居。LinkedList也是一个队列和Deque。访问元素是从第一个或最后一个元素开始的,然后遍历,直到达到所需的索引。插入和删除,一旦通过遍历到达所需的索引,只需重新映射直接邻居链接到新元素或绕过现在删除的元素即可。
  • Map: An interface representing an Collection where each element has an identifying "key"--each element is a key-value pair.
    • HashMap: A Map where keys are unordered, and backed by a Hashtable.
    • HashMap:一种映射,其中的键是无序的,并由Hashtable支持。
    • LinkedhashMap: Keys are ordered by insertion order.
    • LinkedhashMap:键按插入顺序排序。
    • TreeMap: A Map where keys are ordered by a Comparator (typically natural ordering).
    • TreeMap:比较器(通常是自然排序)对键进行排序的映射。
  • Map:表示集合的接口,其中每个元素都有标识“key”——每个元素都是键-值对。HashMap:一种映射,其中的键是无序的,并由Hashtable支持。LinkedhashMap:键是按插入顺序排列的。TreeMap:比较器(通常是自然排序)对键进行排序的映射。
  • Queue: An interface that represents a Collection where elements are, typically, added to one end, and removed from the other (FIFO: first-in, first-out).
  • 队列:一个代表集合的接口,其中元素通常添加到一端,并从另一端删除(FIFO:先入先出)。
  • Stack: An interface that represents a Collection where elements are, typically, both added (pushed) and removed (popped) from the same end (LIFO: last-in, first-out).
  • 堆栈:表示集合的接口,其中的元素通常都是从相同的端(LIFO: last-in, first-out)添加(push)和删除(pop)。
  • Deque: Short for "double ended queue", usually pronounced "deck". A linked list that is typically only added to and read from either end (not the middle).
  • Deque:双端队列的缩写,通常读作deck。一个链表,通常只从两端(而不是中间)添加和读取。

Basic collection diagrams:

基本集合图:

我应该使用什么Java集合?

Comparing the insertion of an element with an ArrayList and LinkedList:

将元素的插入与ArrayList和LinkedList进行比较:

我应该使用什么Java集合?

#3


8  

Even simpler picture is here. Intentionally simplified!

更简单的图在这里。故意简化!

  1. Collection is anything holding data called "elements" (of the same type). Nothing more specific is assumed.

    集合是任何包含称为“元素”(相同类型)的数据的东西。没有更具体的假设。

  2. List is an indexed collection of data where each element has an index. Something like the array, but more flexible.

    List是一个索引数据集合,其中每个元素都有一个索引。有点像数组,但更灵活。

    Data in the list keep the order of insertion.

    列表中的数据保持插入顺序。

  3. Set is a bag of elements, each elements just once (the elements are distinguished using their equals() method.

    Set是一组元素,每个元素只有一次(元素使用equals()方法进行区分)。

    Data in the set are stored mostly just to know what data are there.

    集合中的数据存储的主要是为了知道那里有什么数据。

  4. Map is something like the List, but instead of accessing the elements by their integer index, you access them by their key, which is any object. Like the array in PHP :)

    Map类似于列表,但是不是通过它们的整数索引访问元素,而是通过它们的键(即任何对象)访问它们。就像PHP中的数组:)

    Data in the map are searchable by their key.

    地图中的数据按其键可搜索。

    The main difference between the Set and the Map is that in the Set you search data by themselves, whilst in the map by their key.

    Set和Map之间的主要区别在于,在设置中,你可以自己搜索数据,而在地图中则是按他们的键。

#4


1  

It is simple: if you need to store values with keys mapped to them go for the Map interface, otherwise use List for values which may be duplicated and finally use the Set interface if you don’t want duplicated values in your collection.

很简单:如果需要存储映射到它们的键的值,可以使用Map接口,否则可以使用List来存储可能重复的值,如果不希望在集合中使用重复的值,则使用Set接口。

Here is the complete explanation http://javatutorial.net/choose-the-right-java-collection , including flowchart etc

以下是完整的解释http://javatutorial.net/choose-the-right-java-collection,包括流程图等

#1


213  

Since I couldn't find a similar flowchart I decided to make one myself.

因为我找不到一个类似的流程图,所以我决定自己做一个。

This flow chart does not try and cover things like synchronized access, thread safety etc or the legacy collections, but it does cover the 3 standard Sets, 3 standard Maps and 2 standard Lists.

这个流程图没有尝试并涵盖同步访问、线程安全等内容或遗留集合,但是它涵盖了3个标准集、3个标准映射和2个标准列表。

我应该使用什么Java集合?

This image was created for this answer and is licensed under a Creative Commons Attribution 4.0 International License. The simplest attribution is by linking to either this question or this answer.

这张图片是为这个答案而创建的,并在Creative Commons attributed4.0 International License下获得许可。最简单的归因是通过链接到这个问题或者这个答案。

Other resources

其他资源

Probably the most useful other reference is the following page from the oracle documentation which describes each Collection.

可能最有用的其他引用是描述每个集合的oracle文档中的以下页面。

HashSet vs TreeSet

HashSet vs TreeSet

There is a detailed discussion of when to use HashSet or TreeSet here: Hashset vs Treeset

这里有一个关于何时使用HashSet或TreeSet的详细讨论:HashSet vs TreeSet

ArrayList vs LinkedList

ArrayList vs LinkedList

Detailed discussion: When to use LinkedList over ArrayList?

详细讨论:什么时候使用LinkedList而不是ArrayList?

#2


44  

Summary of the major non-concurrent, non-synchronized collections

Collection: An interface representing an unordered "bag" of items, called "elements". The "next" element is undefined (random).

集合:表示无序“包”项的接口,称为“元素”。“next”元素是未定义的(随机的)。

  • Set: An interface representing a Collection with no duplicates.
    • HashSet: A Set backed by a Hashtable. Fastest and smallest memory usage, when ordering is unimportant.
    • HashSet:由Hashtable支持的集合。当排序不重要时,最快和最小的内存使用。
    • LinkedHashSet: A HashSet with the addition of a linked list to associate elements in insertion order. The "next" element is the next-most-recently inserted element.
    • LinkedHashSet:添加一个链表以将元素插入到插入顺序中的一个HashSet。“next”元素是下一个最近插入的元素。
    • TreeSet: A Set where elements are ordered by a Comparator (typically natural ordering). Slowest and largest memory usage, but necessary for comparator-based ordering.
    • TreeSet:由比较器(通常是自然排序)排序元素的集合。最慢和最大的内存使用,但是对于基于比较器的排序是必需的。
    • EnumSet: An extremely fast and efficient Set customized for a single enum type.
    • 枚举集:为单个枚举类型定制的极其快速和高效的集合。
  • 集合:表示没有重复集合的接口。HashSet:由Hashtable支持的集合。当排序不重要时,最快和最小的内存使用。LinkedHashSet:添加一个链表以将元素插入到插入顺序中的一个HashSet。“下一个”元素是最近插入的元素。TreeSet:由比较器(通常是自然排序)排序元素的集合。最慢和最大的内存使用,但是对于基于比较器的排序是必需的。枚举集:为单个枚举类型定制的极其快速和高效的集合。
  • List: An interface representing a Collection whose elements are ordered and each have a numeric index representing its position, where zero is the first element, and (length - 1) is the last.
    • ArrayList: A List backed by an array, where the array has a length (called "capacity") that is at least as large as the number of elements (the list's "size"). When size exceeds capacity (when the (capacity + 1)-th element is added), the array is recreated with a new capacity of (new length * 1.5)--this recreation is fast, since it uses System.arrayCopy(). Deleting and inserting/adding elements requires all neighboring elements (to the right) be shifted into or out of that space. Accessing any element is fast, as it only requires the calculation (element-zero-address + desired-index * element-size) to find it's location. In most situations, an ArrayList is preferred over a LinkedList.
    • ArrayList:数组支持的列表,其中数组的长度(称为“容量”)至少与元素的数量(列表的“大小”)相同。当大小超过容量时(当(容量+ 1)-th元素被添加时),该数组将重新创建(新长度* 1.5),因为它使用System.arrayCopy()。删除和插入/添加元素需要将所有相邻的元素(右)移到该空间中或从该空间中移出。访问任何元素都是快速的,因为它只需要计算(元素零地址+需求索引*元素大小)就可以找到它的位置。在大多数情况下,ArrayList优于LinkedList。
    • LinkedList: A List backed by a set of objects, each linked to its "previous" and "next" neighbors. A LinkedList is also a Queue and Deque. Accessing elements is done starting at the first or last element, and traversing until the desired index is reached. Insertion and deletion, once the desired index is reached via traversal is a trivial matter of re-mapping only the immediate-neighbor links to point to the new element or bypass the now-deleted element.
    • LinkedList:由一组对象支持的列表,每个对象都链接到它的“前”和“下”邻居。LinkedList也是一个队列和Deque。访问元素是从第一个或最后一个元素开始的,然后遍历,直到达到所需的索引。插入和删除,一旦通过遍历到达所需的索引,只需重新映射直接邻居链接到新元素或绕过现在删除的元素即可。
  • 列表:一个表示集合的接口,它的元素是有序的,每个元素都有一个表示其位置的数字索引,其中0是第一个元素,而(长度- 1)是最后一个元素。ArrayList:数组支持的列表,其中数组的长度(称为“容量”)至少与元素的数量(列表的“大小”)相同。当大小超过容量时(当(容量+ 1)-th元素被添加时),该数组将重新创建(新长度* 1.5),因为它使用System.arrayCopy()。删除和插入/添加元素需要将所有相邻的元素(右)移到该空间中或从该空间中移出。访问任何元素都是快速的,因为它只需要计算(元素零地址+需求索引*元素大小)就可以找到它的位置。在大多数情况下,ArrayList优于LinkedList。LinkedList:由一组对象支持的列表,每个对象都链接到它的“前”和“下”邻居。LinkedList也是一个队列和Deque。访问元素是从第一个或最后一个元素开始的,然后遍历,直到达到所需的索引。插入和删除,一旦通过遍历到达所需的索引,只需重新映射直接邻居链接到新元素或绕过现在删除的元素即可。
  • Map: An interface representing an Collection where each element has an identifying "key"--each element is a key-value pair.
    • HashMap: A Map where keys are unordered, and backed by a Hashtable.
    • HashMap:一种映射,其中的键是无序的,并由Hashtable支持。
    • LinkedhashMap: Keys are ordered by insertion order.
    • LinkedhashMap:键按插入顺序排序。
    • TreeMap: A Map where keys are ordered by a Comparator (typically natural ordering).
    • TreeMap:比较器(通常是自然排序)对键进行排序的映射。
  • Map:表示集合的接口,其中每个元素都有标识“key”——每个元素都是键-值对。HashMap:一种映射,其中的键是无序的,并由Hashtable支持。LinkedhashMap:键是按插入顺序排列的。TreeMap:比较器(通常是自然排序)对键进行排序的映射。
  • Queue: An interface that represents a Collection where elements are, typically, added to one end, and removed from the other (FIFO: first-in, first-out).
  • 队列:一个代表集合的接口,其中元素通常添加到一端,并从另一端删除(FIFO:先入先出)。
  • Stack: An interface that represents a Collection where elements are, typically, both added (pushed) and removed (popped) from the same end (LIFO: last-in, first-out).
  • 堆栈:表示集合的接口,其中的元素通常都是从相同的端(LIFO: last-in, first-out)添加(push)和删除(pop)。
  • Deque: Short for "double ended queue", usually pronounced "deck". A linked list that is typically only added to and read from either end (not the middle).
  • Deque:双端队列的缩写,通常读作deck。一个链表,通常只从两端(而不是中间)添加和读取。

Basic collection diagrams:

基本集合图:

我应该使用什么Java集合?

Comparing the insertion of an element with an ArrayList and LinkedList:

将元素的插入与ArrayList和LinkedList进行比较:

我应该使用什么Java集合?

#3


8  

Even simpler picture is here. Intentionally simplified!

更简单的图在这里。故意简化!

  1. Collection is anything holding data called "elements" (of the same type). Nothing more specific is assumed.

    集合是任何包含称为“元素”(相同类型)的数据的东西。没有更具体的假设。

  2. List is an indexed collection of data where each element has an index. Something like the array, but more flexible.

    List是一个索引数据集合,其中每个元素都有一个索引。有点像数组,但更灵活。

    Data in the list keep the order of insertion.

    列表中的数据保持插入顺序。

  3. Set is a bag of elements, each elements just once (the elements are distinguished using their equals() method.

    Set是一组元素,每个元素只有一次(元素使用equals()方法进行区分)。

    Data in the set are stored mostly just to know what data are there.

    集合中的数据存储的主要是为了知道那里有什么数据。

  4. Map is something like the List, but instead of accessing the elements by their integer index, you access them by their key, which is any object. Like the array in PHP :)

    Map类似于列表,但是不是通过它们的整数索引访问元素,而是通过它们的键(即任何对象)访问它们。就像PHP中的数组:)

    Data in the map are searchable by their key.

    地图中的数据按其键可搜索。

    The main difference between the Set and the Map is that in the Set you search data by themselves, whilst in the map by their key.

    Set和Map之间的主要区别在于,在设置中,你可以自己搜索数据,而在地图中则是按他们的键。

#4


1  

It is simple: if you need to store values with keys mapped to them go for the Map interface, otherwise use List for values which may be duplicated and finally use the Set interface if you don’t want duplicated values in your collection.

很简单:如果需要存储映射到它们的键的值,可以使用Map接口,否则可以使用List来存储可能重复的值,如果不希望在集合中使用重复的值,则使用Set接口。

Here is the complete explanation http://javatutorial.net/choose-the-right-java-collection , including flowchart etc

以下是完整的解释http://javatutorial.net/choose-the-right-java-collection,包括流程图等