Java 集合和地图有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19550793/
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
What is the difference between a Collection and a Map?
提问by user1766555
What is the difference between a Collection and a Map?
集合和地图有什么区别?
Would Map be a subclass of Collection?
Map 会是 Collection 的子类吗?
采纳答案by matsev
From the JavaDoc of Map:
来自Map的 JavaDoc :
An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.
将键映射到值的对象。地图不能包含重复的键;每个键最多可以映射到一个值。
From the JavaDoc of Collection:
来自Collection的 JavaDoc :
A collection represents a group of objects, known as its elements. [...] The JDK [...] provides implementations of more specific subinterfaces like Set and List.
一个集合代表一组对象,称为它的元素。[...] JDK [...] 提供了更具体的子接口(如 Set 和 List)的实现。
The two interfaces are notrelated from a class hierarchical point of view, i.e. Map
does not extend Collection
, nor does Collection
extend Map
. That said, both interfaces are part of the Java Collection Framework.
从类层次结构的角度来看,这两个接口没有关联,即Map
不扩展Collection
,也不Collection
扩展Map
。也就是说,这两个接口都是Java Collection Framework 的一部分。
回答by Juan Jose Fidalgo
A collection represents a group of objects, known as its elements. Some collections allow duplicate elements and others do not. Some are ordered and others unordered.
一个集合代表一组对象,称为它的元素。一些集合允许重复元素,而另一些则不允许。有些是有序的,有些是无序的。
An object that maps keys to values. A map cannot contain duplicate keys. Each key can map to at most one value.
将键映射到值的对象。地图不能包含重复的键。每个键最多可以映射到一个值。
回答by Bogdan Balas
Map is a more specific version of a Collection that has a Key -> Data structure. Collection is just the interface that has data structures for storing data in Java.
Map 是 Collection 的更具体版本,它具有 Key -> Data 结构。集合只是具有用于在 Java 中存储数据的数据结构的接口。
回答by iluxa
The difference between Maps and Collections is that they are two distinct things with pretty much nothing in common. One is not a sub-class of another.
Maps 和 Collections 之间的区别在于它们是两个截然不同的事物,几乎没有任何共同之处。一个不是另一个的子类。
回答by Leviathan
A map is a collection.
地图是一个集合。
Collection includes Lists, Sets and Maps.
集合包括列表、集合和地图。
Lists are: Vector, ArrayList, LinkedList Sets are: HashSet and TreeSet Maps are: HashMap and TreeMap
列表是:Vector、ArrayList、LinkedList 集合是:HashSet 和 TreeSet 映射是:HashMap 和 TreeMap
A map is a list of key-value pairs.
映射是键值对的列表。
回答by Jason Braucht
Take a look at the freely available source codefrom the JDK or if you don't have that, JavaDoc for Collection
and Map
and you'll see that both are interfaces which define an API for objects which hold other objects.
查看JDK 中免费提供的源代码,或者如果您没有,则查看 JavaDoc for Collection
and Map
,您将看到它们都是为包含其他对象的对象定义 API 的接口。
The JavaDoc explains this better than I can...
From the Collection
JavaDoc: "A collection represents a group of objects, known as its elements."
JavaDoc 比我能更好地解释这一点......来自Collection
JavaDoc:“集合表示一组对象,称为它的元素。”
And from the Map
JavaDoc: "An object that maps keys to values."
来自Map
JavaDoc:“一个将键映射到值的对象。”
Contrary to what some of the other answers indicate, Map
has a different API and is not one of types that extend Collection
, like List
, Queue
and Set
.
与其他一些答案所表明的相反,Map
具有不同的 API,并且不是扩展的类型之一Collection
,例如List
,Queue
和Set
。