Java中HashMap和ArrayList的区别? [英] Difference between HashMap and ArrayList in Java?

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

问题描述

在 Java 中,ArrayListHashMap 被用作集合.但是我不明白在什么情况下我们应该使用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 times to use HashMap. What is the major difference between both of them?

推荐答案

您专门询问 ArrayList 和 HashMap,但我认为要完全了解正在发生的事情,您必须了解 Collections 框架.因此,ArrayList 实现了 List 接口,而 HashMap 实现了 Map 接口.所以真正的问题是你什么时候想用 List 什么时候用 Map.这就是 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.

列表:

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

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 访问这些对象,Map 将使您非常容易(并且更快/更高效).HashMap 实现使用键对象的哈希值来定位它的存储位置,因此不再保证值的顺序.但是,Java API 中还有其他类可以提供此功能,例如LinkedHashMap 除了使用哈希表来存储键/值对之外,还按照键的添加顺序维护了一个键的列表(LinkedList),因此您始终可以按照添加的顺序再次访问这些项(如果需要).

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天全站免登陆