This example shows how to iterate LinkedHashMap in reverse order in Java. This example also shows various ways to iterate LinkedHashMap in backward direction using ListIterator and descendingIterator method.
How to iterate LinkedHashMap in reverse order in Java?
There are a couple of ways using which you can iterate the LinkedHashMap in reverse or backward direction in Java.
1. Using the ListIterator
In this approach, we will first get all the keys from the LinkedHashMap object using the keySet
method. We will then convert the Set to ArrayList using the ArrayList constructor. Once we get the list, we will use the ListIterator to iterate the keys in reverse order using the hasPrevious
and previous
methods of the ListIterator and get corresponding values from the LinkedHashMap object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.ListIterator; import java.util.Set; public class LinkedHashMapIterateReverseExample { public static void main(String[] args) { LinkedHashMap<Integer, String> lhmap = new LinkedHashMap<Integer, String>(); lhmap.put(1, "One"); lhmap.put(2, "Two"); lhmap.put(3, "Three"); lhmap.put(4, "Four"); lhmap.put(5, "Five"); //1. get all keys from the LinkedHashMap Set<Integer> setKeys = lhmap.keySet(); //2. convert set to list List<Integer> listKeys = new ArrayList<Integer>(setKeys); /* * 3. get a ListIterator for the ArrayList and position * it at the end to iterate backwards */ ListIterator<Integer> iterator = listKeys.listIterator( listKeys.size() ); //4. Iterate in reverse order using the hasPrevious and previous methods while(iterator.hasPrevious()){ //get value mapped to the key System.out.println( lhmap.get( iterator.previous() ) ); } } } |
Output
1 2 3 4 5 |
Five Four Three Two One |
2. Using the descendingIterator method
This approach is similar to the above one, but instead of ArrayList, we will convert Set to a LinkedList object. The descendingIterator
method of the LinkedList class will allow us to iterate the keys in the reverse order as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
import java.util.Iterator; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.Set; public class LinkedHashMapIterateReverseExample { public static void main(String[] args) { LinkedHashMap<Integer, String> lhmap = new LinkedHashMap<Integer, String>(); lhmap.put(1, "One"); lhmap.put(2, "Two"); lhmap.put(3, "Three"); lhmap.put(4, "Four"); lhmap.put(5, "Five"); //1. get all keys from the LinkedHashMap Set<Integer> setKeys = lhmap.keySet(); //2. convert set to LinkedList LinkedList<Integer> listKeys = new LinkedList<Integer>(setKeys); //3. get descending iterator Iterator<Integer> iterator = listKeys.descendingIterator(); //4. iterate the keys and get the values from the map while(iterator.hasNext()){ System.out.println( lhmap.get( iterator.next() ) ); } } } |
Output
1 2 3 4 5 |
Five Four Three Two One |
This example is a part of the LinkedHashMap in Java Tutorial.
Please let me know your views in the comments section below.
References:
Java 8 LinkedHashMap