In this article, we will discuss how to iterate through contents of LinkedHashMap in reverse-order
1. Key points about LinkedHashMap:
- LinkedHashMap maintains insertion-order
- as it uses doubly-linked list and hashtable to store key-value pairs
2. Iterate LinkedHashMap in reverse-order:
- Get all keys using keyset() method
- Convert to ArrayList with retrieved key set
- Use Collections class utility method : reverse(); to reverse the order of keys (as keys are stored in LinkedHashMap as per insertion-order)
- Then, again iterate through LinkedHashMap using ArrayList containing keys and retrieve respective values using get(key) method
- Display all entries: as per insertion-order and reverse of insertion-order
- Method signature: public static void reverse(List list);
- This method is used to reverse order of ArrayList contents i.e.; reverse-order of insertion-order
IterateLinkedHashMapInReverseOrder.java
package in.bench.resources.java.map;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;
public class IterateLinkedHashMapInReverseOrder {
public static void main(String[] args) {
// creating HashMap object of type <String, String>
LinkedHashMap<String, String> lhm =
new LinkedHashMap<String, String>();
// adding key-value pairs to HashMap object
lhm.put("Microsoft", "Bill Gates");
lhm.put("Apple", "Steve Jobs");
lhm.put("Google", "Sundar Pichai");
lhm.put("LinkedIn", "Reid Hoffman");
lhm.put("Facebook", "Mark Zuckerberg");
// Insertion Order iterating
System.out.println("Insertion Order of LinkedHashMap:"
+ " iterating \n");
// getting keySet() into Set
Set<String> set = lhm.keySet();
// get Iterator from key set
Iterator<String> itr = set.iterator();
// iterating as per Insertion Order
while(itr.hasNext()) {
String key = itr.next();
System.out.println("Key : " + key + "\t\t"
+ "Value : " + lhm.get(key));
}
// Reverse of Insertion Order iterating
System.out.println("\n\nReverse of Insertion Order:"
+ " iterating \n");
// convert to ArrayList of key set
List<String> alKeys = new ArrayList<String>(lhm.keySet());
// reverse order of keys
Collections.reverse(alKeys);
// iterate LHM using reverse order of keys
for(String strKey : alKeys){
System.out.println("Key : " + strKey + "\t\t"
+ "Value : " + lhm.get(strKey));
}
}
}
Output:
Insertion Order of LinkedHashMap: iterating
Key : Microsoft Value : Bill Gates
Key : Apple Value : Steve Jobs
Key : Google Value : Sundar Pichai
Key : LinkedIn Value : Reid Hoffman
Key : Facebook Value : Mark Zuckerberg
Reverse of Insertion Order: iterating
Key : Facebook Value : Mark Zuckerberg
Key : LinkedIn Value : Reid Hoffman
Key : Google Value : Sundar Pichai
Key : Apple Value : Steve Jobs
Key : Microsoft Value : Bill Gates
Note: When we speak about insertion and reverse order, all are with respect to keys (and not values)
Related Articles:
- Various ways to iterate through ArrayList
- Various ways to iterate through Vector – 5 ways
- Various ways to iterate through LinkedList – 5 ways
- Various ways to iterate through HashSet – 3 ways
- How to reverse LinkedHashSet contents
- Various ways to iterate through TreeSet – 3 ways
- Various ways to iterate over List of HashMap in Java
- Ways to iterate over HashMap of ArrayList in Java
- Various ways to iterate Arrays in Java – 5 ways
- Iterating List using forEach() in Java 8
- Iterating Set using forEach() in Java 8
- Iterating Map using forEach() in Java 8
References:
- https://docs.oracle.com/javase/tutorial/collections/intro/
- https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Map.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Map.Entry.html
- https://docs.oracle.com/javase/tutorial/collections/interfaces/map.html
- https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html
- https://docs.oracle.com/javase/tutorial/collections/interfaces/set.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Set.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html
- https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashMap.html
- https://docs.oracle.com/javase/8/docs/api/java/util/TreeMap.html
Happy Coding !!
Happy Learning !!