How to iterate through LinkedHashMap in reverse order

In this article, we will discuss how to iterate through contents of LinkedHashMap in reverse order

LinkedHashMap maintains insertion order, as it uses doubly-linked list and hashtable to store key-value pairs

 

To iterate through LinkedHashMap contents in reverse order

Steps:

  1. Get all keys using keyset() method
  2. Convert to ArrayList with retrieved key set
  3. Use Collections class utility method : reverse() to reverse the order of keys (as keys are stored in LinkedHashMap as per insertion order)
  4. Then, again iterate through LinkedHashMap using ArrayList containing keys and retrieve respective values using get(key) method
  5. 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 the 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)

 

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/7/docs/api/java/util/HashMap.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/7/docs/api/java/util/Iterator.html
https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html
http://docs.oracle.com/javase/6/docs/api/java/util/LinkedHashMap.html
https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashMap.html
https://docs.oracle.com/javase/7/docs/api/java/util/TreeMap.html
https://docs.oracle.com/javase/8/docs/api/java/util/TreeMap.html

 

Happy Coding !!
Happy Learning !!