从 Java 集合访问元素时哪个更快

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20300490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 00:56:40  来源:igfitidea点击:

Which is faster in accessing elements from Java collections

javaperformancecollections

提问by Chaitanya

I am trying to understand which is faster in accessing elements from collections in Java like ArrayList, LinkedList, HashSet, TreeSet, HashMap, TreeMap etc.

我试图了解从 Java 中的集合(如 ArrayList、LinkedList、HashSet、TreeSet、HashMap、TreeMap 等)访问元素时哪个更快。

From this question: Suitable java collection for fast get and fast removal, I got to know that ArrayList takes O(1) and TreeMap as O(log n)

从这个问题:适用于快速获取和快速删除的 Java 集合,我知道 ArrayList 将 O(1) 和 TreeMap 作为 O(log n)

where as this: Map/ArrayList: which one is faster to search for an elementshows that ArryList is O(n), HashMap as O(1) and TreeMap as O(log n)

其中:Map/ArrayList: 哪个更快地搜索元素表明ArryList 为O(n),HashMap 为O(1),TreeMap 为O(log n)

where as this: Why is it faster to process a sorted array than an unsorted array?says that sorted array is faster than unsorted array. As the elements in TreeMap are sorted then can I assume all sorted collections are faster than un-sorted collections?

哪里是这样:为什么处理排序数组比处理未排序数组更快?说排序数组比未排序数组快。由于 TreeMap 中的元素已排序,那么我可以假设所有已排序的集合都比未排序的集合更快吗?

Please help me in understanding which is faster to use in accessing elements from java collections of list, set, map etc implementations.

请帮助我理解在从列表、集合、映射等实现的 java 集合中访问元素时使用哪个更快。

采纳答案by Mohammad Dehghan

Every collection type is suitable for a particular scenario. There is no fastestor bestcollection.

每种集合类型都适用于特定场景。没有最快最好的收藏。

  • If you need fast access to elements using index, ArrayListis your answer.
  • If you need fast access to elements using a key, use HashMap.
  • If you need fast add and removal of elements, use LinkedList(but it has a verypoor index access performance).
  • 如果您需要使用index快速访问元素,这ArrayList就是您的答案。
  • 如果您需要使用快速访问元素,请使用HashMap.
  • 如果您需要快速添加和删除元素,请使用LinkedList(但它的索引访问性能非常差)。

and so on.

等等。

回答by Kumar Abhinav

It depends whether you want to access an element as index based(in case of list) or see if an Object exists in the Collection

这取决于您是要访问基于索引的元素(在列表的情况下)还是查看集合中是否存在对象

If you want to access an element index based,then arraylist is faster as it implements RandomAccess Marker interface and is internally backed by an array.

如果您想访问基于元素索引,那么 arraylist 会更快,因为它实现了 RandomAccess Marker 接口并且在内部由数组支持。

Sets are internally backed by Map ,so performance of Map and Set is same(Set use a dummy Object as value in key-value pair).I would suggest you to use a HashSet.

Sets 由 Map 内部支持,因此 Map 和 Set 的性能相同(Set 使用虚拟对象作为键值对中的值)。我建议您使用 HashSet。

The problem that many programmers dont notice is that performance of Hashset or HashMap is best O(1) when the hashing function of Key Object is good,ie. it produces different values for different Objects (though this is not a strict requirement).

很多程序员没有注意到的问题是,当Key Object的hash函数好时,Hashset或HashMap的性能最好是O(1),即。它为不同的对象产生不同的值(尽管这不是一个严格的要求)。

NOTE :- If you are Hashing funciton is not good,it degrades to a LinkedList internally and its performance degrades to O(n)

注意:- 如果你的哈希函数不好,它会在内部降级为 LinkedList,其性能降级为 O(n)

My personal preference is to Use EnumMap or EnumSet.It simply uses the Enum values for its functioning and programmers dont have to worry about the Enum's hashcode/equals function.For rest other cases,use HashSet or HashMap(if you dont have to make it ordered)

我个人的偏好是使用 EnumMap 或 EnumSet。它只是使用 Enum 值来实现它的功能,程序员不必担心 Enum 的 hashcode/equals 函数。对于其他情况,请使用 HashSet 或 HashMap(如果您不必这样做)订购)