在Java中的HashMap和ArrayList的区别? [英] difference between HashMap and ArrayList in java?

查看:642
本文介绍了在Java中的HashMap和ArrayList的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,ArrayList和HashMap的用作收藏。但我不明白其中的情况下,我们应该使用ArrayList和时间来使用HashMap中。什么是两者之间的主要区别?

In Java, ArrayList and HashMap are used as collections. But I couldn't understand in which situations we should use ArrayList and which time to use HashMap. What is the major difference between both of them?

推荐答案

您是专门关于ArrayList和HashMap的要求,但我觉得要充分了解正在发生的事情,你必须了解集合框架。因此,一个ArrayList实现了List接口和一个HashMap实现Map接口。所以真正的问题是,当你想使用一个List,做当你要使用地图。这就是Java API文档有很大帮助。

You are asking specifically about ArrayList and HashMap, but I think to fully understand what is going on you have to understand the Collections framework. So 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.

清单:

这是有序集合(也称为
  序列)。此接口的用户
  拥有凡在precise控制
  列出每个元素的插入。该
  用户可以通过访问他们的内容
  整数索引(在列表中的位置),
  并搜索列表中的元素

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

So as other answers have discussed, 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, but a lot of the details of dealing with the array are handled for you). 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).

另一方面的Map接受一个对象,并把它作为密钥(索引)到另一个对象(的值)。因此,可以说你必须具有唯一ID的对象,你知道你会想通过ID在某个时候访问这些对象,地图将使这个你很容易(和更快/更有效)。 HashMap中实现使用密钥对象以定位存储它的散列值,所以不存在的值的顺序的机制保障了。然而有其他类的Java API中,可以提供这一点,例如LinkedHashMap的,其中还有使用哈希表来存储键/值对,也保持了加入他们的顺序键列表(链表),让你随时可以重新访问中添加顺序的项目(如果需要的话)。

A Map on the other hand takes one object and uses that as a key (index) to another object (the value). So lets say you have objects which have unique IDs, and you know you are going to want to access these objects by ID at some point, the Map will make this very easy on you (and quicker/more efficient). The HashMap implementation uses the hash value of the key object to locate where it is stored, so there is no guarentee 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).

这篇关于在Java中的HashMap和ArrayList的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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