Java ArrayMap 与 HashMap
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19441407/
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
ArrayMap versus HashMap
提问by Sachin M
what is the main difference with org.apache.myfaces.trinidad.util.ArrayMap
and java.util.HashMap
?
与org.apache.myfaces.trinidad.util.ArrayMap
和的主要区别是什么java.util.HashMap
?
Is ArrayMap is thread safe?
ArrayMap 是线程安全的吗?
In the documentation it is mentioned that Array is best in performance wise.
在文档中提到 Array 在性能方面是最好的。
I don't want to use hashmap or concurrent hashmap. I want to try other like below. which one is best alternative If I consider Thread safety and performance?
我不想使用哈希图或并发哈希图。我想尝试其他如下。如果我考虑线程安全和性能,哪一个是最好的选择?
ArrayMap<String,String> var= new ArrayMap<String,String>();
采纳答案by GrIsHu
HashMapuses an array underneath so it can never be faster than using an array correctly.
HashMap在底层使用数组,因此它永远不会比正确使用数组更快。
Random.nextInt()
is many times slower than what you are testing, even using array to test an array is going to bias your results.The reason your array is so slow is due to the equals comparisons, not the array access itself.
Random.nextInt()
比您正在测试的慢很多倍,即使使用数组来测试数组也会使您的结果产生偏差。您的数组如此缓慢的原因是由于相等比较,而不是数组访问本身。
An ArrayList
implements the List
interface and a HashMap
implements the Map
interface. So the real question is when do you want to use a List and when do you want to use a Map. This is where the Java API documentation helps a lot.
一个ArrayList
实现List
接口,一个HashMap
实现Map
接口。所以真正的问题是你什么时候想用 List 什么时候用 Map。这是 Java API 文档大有帮助的地方。
List:
列表:
An ordered collection (also known as a sequence). The user of this interface has precise control over where in the list each element is inserted. The user can access elements by their integer index (position in the list), and search for elements in the list.
有序集合(也称为序列)。此界面的用户可以精确控制每个元素在列表中的插入位置。用户可以通过它们的整数索引(在列表中的位置)访问元素,并在列表中搜索元素。
Map:
地图:
An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.
将键映射到值的对象。地图不能包含重复的键;每个键最多可以映射到一个值。
The list interface (ArrayList)
is an ordered collection of objects that you access using an index, much like an array (well in the case of ArrayList
, as the name suggests, it is just an array in the background. You would use an ArrayList
when you want to keep things in sorted order (the order they are added, or indeed the position within the list that you specify when you add the object).
list 接口(ArrayList)
是一个有序的对象集合,您可以使用索引访问这些对象,很像数组(就 而言ArrayList
,顾名思义,它只是后台的一个数组。ArrayList
当您想保留排序顺序的事物(添加它们的顺序,或者实际上是您在添加对象时指定的列表中的位置)。
The HashMap
implementation uses the hash value of the key object to locate where it is stored, so there is no guarantee of the order of the values anymore. There are however other classes in the Java API that can provide this, e.g. LinkedHashMap
, which as well as using a hash table to store the key/value pairs, also maintains a List (LinkedList) of the keys in the order they were added, so you can always access the items again in the order they were added (if needed).
该HashMap
实现使用键对象的哈希值来定位它的存储位置,因此不再保证值的顺序。然而,Java API 中还有其他类可以提供此功能,例如LinkedHashMap
,除了使用哈希表来存储键/值对之外,还按照键的添加顺序维护键的列表 (LinkedList),因此您始终可以按照添加顺序再次访问这些项目(如果需要)。
When to use Arrays?
什么时候使用数组?
Never underestimate arrays. Most of the time, when we have to use a list of objects, we tend to think about using vectors or lists. However, if the size of collection is already known and is not going to change, an array can be considered as the potential data structure. It's faster to access elements of an array than a vector or a list. That's obvious, because all you need is an index. There's no overhead of an additional get method call.
永远不要低估数组。大多数时候,当我们必须使用对象列表时,我们倾向于考虑使用向量或列表。但是,如果集合的大小已知且不会改变,则可以将数组视为潜在的数据结构。访问数组元素比访问向量或列表更快。这很明显,因为您只需要一个索引。没有额外的 get 方法调用的开销。
Sometimes, it may be best to use a combination of the above approaches. For example, you could use a ArrayList of HashMap to suit a particular need.
有时,最好结合使用上述方法。例如,您可以使用 HashMap 的 ArrayList 来满足特定需求。