7

Could someone explain the main benefits for choosing one over the other and the detriments that come with that choice?

Trott
  • 66,479
  • 23
  • 173
  • 212
Bobby
  • 18,217
  • 15
  • 74
  • 89

5 Answers5

22

They solve different problems, LinkedHashMap does a mapping of keys to values, a LinkedHashSet simply stores a collection of things with no duplicates.

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");
Benson
  • 22,457
  • 2
  • 40
  • 49
  • 1
    I was just looking the source code of the ArrayList just to make sure that I had to use a Set (which has good search performance) instead of an ArrayList (which has to traverse all the records to find something) – Ravi Wallau Jun 11 '09 at 03:54
  • 6
    The purpose of LinkedHashMap over HashMap is that LinkedHashMap also uses a LinkedList internally to store the order the key/value pairs have been added. So iterating over the LinkedHashMap will result in key/value pair in the order they were added while a classic HashMap will iterate based on the order of the hashcode of each key. – Vincent Robert Jun 13 '09 at 00:19
  • 1
    That's correct, but since the question was comparing LinkedHashMaps to LinkedHashSets I decided to focus on the differences, rather than the commonalities. :-) – Benson Jun 15 '09 at 22:21
  • 1
    Fine with me Benson, that's why I'm only adding a comment. I just thought the author was not that experienced with Java and wanted to add some more information :) – Vincent Robert Jun 17 '09 at 12:20
6

LinkedHashSet internally contain a doubly-linked list running through all of its entries that defines the order of elements. This class permits null elements.

This class implementation is not synchronized, so it must be synchronized externally. LinkedHashMap is not synchronized either and must be synchronized externally

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

Roman Kagan
  • 10,440
  • 26
  • 86
  • 126
3

A Set has just values, you cannot put duplicates in. a Map has a key/value pair. They have different uses.

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.

Rickster
  • 220
  • 1
  • 9
2

LinkedHashMap and LinkedHashSet has only one difference and that comes by HashMap and HashSet difference, their parents. Again, HashSet is just a variation of HashMap. You can say HashSet as a HashMap with all values pointing to a single final object. Therefore, both of them does not give you much differences.

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.

DKSRathore
  • 3,043
  • 6
  • 27
  • 36
2

One's a set, and one's a map. Choose the correct data structure for a given scenario.

Rob
  • 47,999
  • 5
  • 74
  • 91