Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the pros and cons of LinkedHashMaps vs. LinkedHashSets?

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

like image 342
Bobby Avatar asked Jun 10 '09 21:06

Bobby


People also ask

What is the difference between LinkedHashMap and LinkedHashSet?

They solve different problems, LinkedHashMap does a mapping of keys to values, a LinkedHashSet simply stores a collection of things with no duplicates. 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.

What is the advantage of LinkedHashSet over HashSet?

HashSet is an unordered & unsorted collection of the data set, whereas the LinkedHashSet is an ordered and sorted collection of HashSet. HashSet does not provide any method to maintain the insertion order. Comparatively, LinkedHashSet maintains the insertion order of the elements.

Which is better LinkedHashMap or HashMap?

HashMap as do not maintain any insertion order of its elements hence is faster as compare to TreeMap also do not sort its elements on the basis of its value so also faster than LinkedHashMap. LinkedHashMap is faster as compare to TreeMap but is slower than HashMap.

Which is better HashSet or LinkedHashSet?

The performance of HashSet is better when compared to LinkedHashSet and TreeSet. TreeSet performance is better than LinkedHashSet except for insertion and removal operations because it has to sort the elements after each insertion and removal operation. HashSet allows only one null value.


3 Answers

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");
like image 189
Benson Avatar answered Oct 13 '22 06:10

Benson


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

like image 22
Roman Kagan Avatar answered Oct 13 '22 08:10

Roman Kagan


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.

like image 36
Rickster Avatar answered Oct 13 '22 08:10

Rickster