Java中的列表与地图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3770613/
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
List vs Map in Java
提问by JBeg
I didnt get the sense of Maps in Java. When is it recommended to use a Map instead of a List?
我没有理解 Java 中的 Maps。什么时候推荐使用 Map 而不是 List?
thanks in advance,
提前致谢,
nohereman
异乡人
采纳答案by Chris
Java map: An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.
Java 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.
Java 映射:将键映射到值的对象。地图不能包含重复的键;每个键最多可以映射到一个值。
Java 列表:有序集合(也称为序列)。此界面的用户可以精确控制每个元素在列表中的插入位置。用户可以通过它们的整数索引(在列表中的位置)访问元素,并在列表中搜索元素。
The difference is that they are different. Map is a mapping of key/values, a list of a list of items.
不同之处在于它们是不同的。Map 是键/值的映射,一个项目列表的列表。
回答by Colin Hebert
A map is used as an association of a key and a value. With a list you have basically only values.
The indexes in List are always int, whereas in Map you can have another Object as a key.
映射用作键和值的关联。使用列表,您基本上只有值。
List 中的索引始终是 int,而在 Map 中,您可以将另一个 Object 作为键。
Resources :
资源 :
回答by Sheldon L. Cooper
When you want to map instead of list. The names of those interfaces have meaning, and you shouldn't ignore it.
当您想要映射而不是列表时。这些接口的名称是有意义的,您不应该忽略它。
Use a map when you want your data structure to represent a mapping for keys to values. Use a list when you want your data to be stored in an arbitrary, ordered format.
当您希望数据结构表示键到值的映射时,请使用映射。当您希望以任意、有序的格式存储数据时,请使用列表。
回答by amorfis
Map
and List
serve different purpose.
Map
并List
服务于不同的目的。
List
holds collection of items. Ordered (you can get item by index).
List
持有项目的集合。已订购(您可以按索引获取项目)。
Map
holds mapping key -> value. E.g. map person to position: "JBeg" -> "programmer". And it is unordered. You can get value by key, but not by index.
Map
保存映射键 -> 值。例如将人员映射到位置:“JBeg”->“程序员”。而且是无序的。您可以通过键获取值,但不能通过索引获取值。
回答by Woot4Moo
Depends on your performance concerns. A Map more explicitly a HashMap will guarantee O(1) on inserts and removes. A List has at worst O(n) to find an item. So if you would be so kind as to elaborate on what your scenario is we may help more.
取决于您的性能问题。Map 更明确的 HashMap 将保证插入和删除的 O(1)。一个列表最坏的 O(n) 找到一个项目。因此,如果您愿意详细说明您的情况,我们可能会提供更多帮助。
回答by rkg
Its probably a good idea to revise Random AccessVs Sequential AccessData Structures. They both have different run time complexities and suitable for different type of contexts.
修改Random AccessVs Sequential Access Data Structures可能是个好主意。它们都有不同的运行时复杂性,适用于不同类型的上下文。
回答by Tony Ennis
Say you have a bunch of students with names and student IDs. If you put them in a List, the only way to find the student with student_id = 300 is to look at each element of the list, one at a time, until you find the right student.
假设您有一群有姓名和学生证的学生。如果你把它们放在一个列表中,找到 student_id = 300 的学生的唯一方法是查看列表的每个元素,一次一个,直到找到合适的学生。
With a Map, you associate each student's ID and the student instance. Now you can say, "get me student 300" and get that student back instantly.
使用 Map,您可以将每个学生的 ID 与学生实例相关联。现在你可以说,“给我学生 300”并立即让那个学生回来。
Use a Map when you need to pick specific members from a collection. Use a List when it makes no sense to do so.
当您需要从集合中选择特定成员时,请使用 Map。当这样做没有意义时使用列表。
Say you had exactly the same student instances but your task was to produce a report of all students' names. You'd put them in a List since there would be no need to pick and choose individual students and thus no need for a Map.
假设您有完全相同的学生实例,但您的任务是生成所有学生姓名的报告。您将它们放在一个列表中,因为不需要挑选个别学生,因此不需要地图。
回答by InsertNickHere
I thinks its a lot the question of how you want to access your data. With a map you can "directly" access your items with a known key, in a list you would have to search for it, evan if its sorted.
我认为很多问题是您希望如何访问您的数据。使用地图,您可以使用已知键“直接”访问您的项目,在列表中您必须搜索它,如果它已排序,则evan。
Compare:
相比:
List<MyObject> list = new ArrayList<MyObject>();
//Fill up the list
// Want to get object "peter"
for( MyObject m : list ) {
if( "peter".equals( m.getName() ) {
// found it
}
}
In a map you can just type
在地图中,您只需键入
Map<String, MyObject> map = new HashMap<String, MyObject>();
// Fill map
MyObject getIt = map.get("peter");
If you have data to process and need to do it with all objects anyway, a list is what you want. If you want to process single objects with well known key, a map is better. Its not the full answer (just my 2...) but I hope it might help you.
如果您有数据要处理并且无论如何都需要对所有对象进行处理,那么列表就是您想要的。如果要处理具有众所周知的键的单个对象,则地图更好。这不是完整的答案(只是我的 2 ...),但我希望它可以帮助您。
回答by Rizwan Mushtaq
Maps store data objects with unique keys,therefore provides fast access to stored objects. You may use ConcurrentHashMap in order to achieve concurrency in multi-threaded environments. Whereas lists may store duplicate data and you have to iterate over the data elements in order to access a particular element, therefore provide slow access to stored objects. You may choose any data structure depending upon your requirement.
Maps 使用唯一键存储数据对象,因此提供对存储对象的快速访问。您可以使用 ConcurrentHashMap 来实现多线程环境中的并发。而列表可能存储重复数据,您必须遍历数据元素才能访问特定元素,因此提供对存储对象的缓慢访问。您可以根据需要选择任何数据结构。