java LinkedHashMaps 与 LinkedHashSets 的优缺点是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/978257/
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 are the pros and cons of LinkedHashMaps vs. LinkedHashSets?
提问by soldier.moth
Could someone explain the main benefits for choosing one over the other and the detriments that come with that choice?
有人可以解释选择一个而不是另一个的主要好处以及该选择带来的损害吗?
回答by Benson
They solve different problems, LinkedHashMap does a mapping of keys to values, a LinkedHashSet simply stores a collection of things with no duplicates.
它们解决了不同的问题,LinkedHashMap 将键映射到值,LinkedHashSet 只是存储没有重复项的集合。
A linked hash map is for mapping key/value pairs -- for example, storing names and ages:
链接的哈希映射用于映射键/值对——例如,存储姓名和年龄:
Map<String,Integer> namesAndAges = new LinkedHashMap<String,Integer>();
namesAndAges.put("Benson", 25);
namesAndAges.put("Fred", 19);
On the other hand, a linked hash set is for storing a collection of one thing -- names, for example:
另一方面,链接散列集用于存储一个事物的集合——名称,例如:
Set<String> names = new LinkedHashSet<String>();
names.add("Benson");
names.add("Fred");
回答by Roman Kagan
LinkedHashSet internally contain a doubly-linked list running through all of its entries that defines the order of elements. This class permits null elements.
LinkedHashSet 内部包含一个双向链表,它贯穿其定义元素顺序的所有条目。此类允许空元素。
This class implementation is not synchronized, so it must be synchronized externally.LinkedHashMap is not synchronized either and must be synchronized externally
这个类的实现不是同步的,所以必须在外部同步。LinkedHashMap 也不同步,必须在外部同步
For example:
例如:
Map map = Collections.synchronizedMap(new LinkedHashMap());
Other than that LinkedHashSet stores single values per element and LinkedHashMap stores key/value pair.
In the diagram below you can see java.util.Collections. Solid boxes show concrete class implementation
alt text http://www.softfinity.com/diag1.png
除此之外,LinkedHashSet 存储每个元素的单个值,LinkedHashMap 存储键/值对。
在下图中,您可以看到 java.util.Collections。实心框显示具体的类实现
替代文本 http://www.softfinity.com/diag1.png
回答by Rickster
A Set has just values, you cannot put duplicates in. a Map has a key/value pair. They have different uses.
Set 只有值,不能放入重复项。 Map 有一个键/值对。它们有不同的用途。
A set will get used as a collection, passing in a group of objects, whereas a map is useful for when you have a unique key to identify each element and you want to be able to access it by that key.
一个集合将被用作一个集合,传递一组对象,而当你有一个唯一的键来标识每个元素并且你希望能够通过该键访问它时,映射很有用。
回答by DKSRathore
LinkedHashMapand LinkedHashSethas only one difference and that comes by HashMapand HashSetdifference, their parents. Again, HashSetis just a variation of HashMap. You can say HashSetas a HashMapwith all values pointing to a single final object. Therefore, both of them does not give you much differences.
LinkedHashMap而LinkedHashSet只有一个区别和随之而来的HashMap和HashSet差异,他们的父母。同样,HashSet只是 的变体HashMap。你可以说HashSeta HashMap,所有值都指向一个最终对象。因此,它们都没有给您带来太大差异。
Using LinkedHashSet, You shall be using only one final object other than your keys.
Using LinkedHashMap, if you set values as null for all keys, then its better than LinkedHashSet for Set purpose as well.
使用LinkedHashSet,您将只使用除密钥之外的一个最终对象。使用LinkedHashMap,如果您将所有键的值都设置为 null,那么它也比用于 Set 目的的 LinkedHashSet 更好。
回答by Rob
One's a set, and one's a map. Choose the correct data structure for a given scenario.
一个是一套,一个是地图。为给定的场景选择正确的数据结构。

