ArrayMap与HashMap [英] ArrayMap versus HashMap

查看:193
本文介绍了ArrayMap与HashMap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

org.apache.myfaces.trinidad.util.ArrayMapjava.util.HashMap的主要区别是什么?

what is the main difference with org.apache.myfaces.trinidad.util.ArrayMap and java.util.HashMap?

ArrayMap是线程安全的吗?

Is ArrayMap is thread safe?

在文档中提到Array在性能方面是最好的.

In the documentation it is mentioned that Array is best in performance wise.

我不想使用哈希图或并发哈希图.我想尝试其他类似下面的方法.如果考虑线程安全性和性能,哪一种是最佳选择?

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

推荐答案

HashMap 使用下面的数组,因此它永远不会比正确使用数组快.

HashMap uses an array underneath so it can never be faster than using an array correctly.

Random.nextInt()比测试的速度慢很多倍,即使使用数组测试数组也会使结果产生偏差.数组之所以这么慢的原因是由于equals比较,而不是数组访问本身.

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.

ArrayList实现List接口,而HashMap实现Map接口.因此,真正的问题是何时要使用列表以及何时要使用地图.这是Java API文档的有用之处.

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.

列表:

有序集合(也称为序列).该界面的用户可以精确控制列表中每个元素的插入位置.用户可以通过其整数索引(列表中的位置)访问元素,并在列表中搜索元素.

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.

地图:

一个将键映射到值的对象.映射不能包含重复的键;每个键最多可以映射到一个值.

An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.

列表接口(ArrayList)是使用索引访问的对象的有序集合,非常类似于数组(顾名思义,对于ArrayList而言,它只是背景中的一个数组).想要按排序顺序(事物的添加顺序,或者实际上是添加对象时指定的列表位置)使用ArrayList.

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).

HashMap实现使用键对象的哈希值来定位它的存储位置,因此不再保证值的顺序.但是,Java API中还有其他类可以提供此功能,例如LinkedHashMap以及使用哈希表存储键/值对的方法,还按键的添加顺序维护键的列表(LinkedList),因此您始终可以按其顺序再次访问这些项.添加(如果需要).

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).

何时使用阵列?

永远不要低估数组.大多数时候,当我们不得不使用对象列表时,我们倾向于考虑使用向量或列表.但是,如果集合的大小已知并且不会改变,则可以将数组视为潜在的数据结构.访问数组的元素比向量或列表要快.这很明显,因为您只需要一个索引.没有额外的get方法调用的开销.

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.

有时,最好结合使用以上方法.例如,您可以使用HashMap的ArrayList来满足特定需求.

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.

这篇关于ArrayMap与HashMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆