How to iterate through LinkedHashMap in reverse order

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

Key points about LinkedHashMap:

  • 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:

  • 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)

 

References:

 

Happy Coding !!
Happy Learning !!

Converting Unordered HashMap into Sorted TreeMap
Adding one HashMap to another HashMap using putAll method