How to Maintain Insertion Order of the Elements in Java HashMap?When elements get from the HashMap due to hashing the order they inserted is not maintained while retrieval. We can achieve the given task using LinkedHashMap. The LinkedHashMap class implements a doubly-linked list so that it can traverse through all the elements. Show Example: Input : HashMapInput = {c=6, a=1, b=2} Output: HashMapPrint = {c=6, a=1, b=2} Input : HashMapInput = {"first"=1, "second"=3} Output: HashMapPrint = {"first"=1, "second"=3}Syntax:
It creates an object of the LinkedHashMap class with the same mappings specified in the original Map object. Order Not Maintain Here: HashMap Implementation: Java
Output
Iterate over original HashMap
01 => : aaaaaaa
02 => : kkkkkkk
03 => : bbbbbbb
04 => : zzzzzzz
Here, the original insertion order of HashMap is [01, 03, 04, 02], but the output is different [01, 02, 03, 04]. It did not maintain the original insertion order of the elements. Order Maintain Here: LinkedHashMap implementation Java
Output
Iterate over LinkedHashMap
01 => : aaaaaaa
03 => : bbbbbbb
04 => : zzzzzzz
02 => : kkkkkkk
Article Tags :
Java Java Programs
Java-LinkedHashMap Practice Tags :
Java How to maintain the insertion order of the elements in Java HashMap?We cannot. The HashMap class does not maintain the order of the elements. This means that It might not return the elements in the same order they were inserted into it. If the application needs the elements to be returned in the same order they were inserted, LinkedHashMap should be used. The LinkedHashMap class implements a doubly linked list so that it can traverse through all the elements. As per LinkedHashMap Java Doc:
In the scenario wherein the HashMap is returned from the third-party library which cannot be changed and application needs ordering of the elements contained in it, a LinkedHashMap object can be created from the HashMap object. The LinkedHashMap class has a special constructor for that purpose which takes a Map as an argument.
The above given constructor creates an object of the LinkedHashMap class with the same mappings specified in the original Map object. Important Note: The LinkedHashMap object created in this way maintains the insertion order in which the elements were returned from the HashMap while the creation of it (and not the order in which elements were inserted into original HashMap). Let’s see a small example to demonstrate that.
Output
As you can see, we have inserted the Employee objects in HashMap in [emp01, emp03, emp04, emp02] order. However, when we printed the elements by iterating over HashMap, the order was changed to [emp01, emp03, emp02, emp04]. It did not maintain the original insertionorder of the elements “emp02” and “emp04”. Then we created a LinkedHashMap object from the original HashMap object and printed it. Which returned the elements in [emp01, emp03, emp02, emp04] order. This is the order in which HashMap returned the elements when LinkedHashMap was created from it. From that pointonwards, LinkedHashMap will keep this insertion order whenever it returns the element (not the original order [emp01, emp03, emp02, emp04]). Here is the example Employee class used in this example. This example is a part of the Java Hashmap tutorial. Please let me know your views in the comments section. Does TreeMap maintain insertion order?A TreeMap is a Map that maintains its entries in ascending order, sorted according to the keys' natural ordering, or according to a Comparator provided at the time of the TreeMap constructor argument. The TreeMap class is efficient for traversing the keys in a sorted order. Which maintains the insertion order?1) List is an ordered collection it maintains the insertion order, which means upon displaying the list content it will display the elements in the same order in which they got inserted into the list. Set is an unordered collection, it doesn't maintain any order. Java - The LinkedHashMap ClassAdvertisements Previous Page Next Page This class extends HashMap and maintains a linked list of the entries in the map, in the order in which they were inserted. This allows insertion-order iteration over the map. That is, when iterating a LinkedHashMap, the elements will be returned in the order in which they were inserted. You can also create a LinkedHashMap that returns its elements in the order in which they were last accessed. Following is the list of constructors supported by the LinkedHashMap class.
Apart from the methods inherited from its parent classes, LinkedHashMap defines the following methods −
|