In this example, we will show how to use the Java LinkedHashMap class. LinkedHashMap is an implementation of
java.util.Map interface with predictable iteration order (the order of insertion) i.e. LinkedHashMap will iterate in the order in which the entries were put into the map.
This implementation differs from
HashMap in the way that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map. The insertion order is not affected if a key is re-inserted into the map. Performance of LinkedHashMap is slightly below than that of HashMap, due to the added expense of maintaining the linked list.
The other two important implementations of Map interface are
java.util.TreeMap. They offer mostly the same functionality. The most important difference is the order in which iteration through the entries will happen. HashMap makes no guarantee about the order. The order can even change completely when new elements are added. TreeMap will iterate according to the “natural ordering” of the keys according to their
compareTo() method (or an externally supplied
java.util.Comparator). LinkedHashMap will iterate in the order in which the entries were put into the map.
2. Example of Java LinkedHashMap
Create a java class named LinkedHashMapExample.java with the following code:
Let’s explain the above code.
First, we create a LinkedHashMap object which will hold Company name as key and its share price as value. Now we insert the (key, value) pair to the map using the map’s put method. After that, we use some of the basic methods of Map interface to retrieve/manipulate the data. Brief description of the methods is given below:
V get(Object key): This method returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
int size(): Returns the number of key-value mappings in this map.
boolean isEmpty(): Returns true if this map contains no key-value mappings.
boolean containsKey(Object key): This method returns true if this map maps one or more keys to the specified value.
boolean containsValue(Object value): This method returns true if this map maps one or more keys to the specified value.
V remove(Object key): Removes the mapping for the specified key from this map if present.
void clear(): This method removes all of the mappings from this map.
If we run the above code, we will have the following results:
3. Scenarios of using each implementation
If performance is critical but ordering is not, go for HashMap. If sorting is important, for example showing values in a table, sorted in alphabetical order then go for TreeMap. If a value is added in the TreeMap or removed from the TreeMap, TreeMap will make sure the table is still sorted alphabetically. If the order of insertion is important, for example showing values in a shopping cart, go for LinkedHashMap.
4. Download the source code
This was an example of how to use the class
Download the Eclipse project of this tutorial: Java LinkedHashMap Example
Last updated on Jun. 01st, 2020