15

Possible Duplicate:
Iterating through a LinkedHashMap in reverse order

How to traverse Linked Hash Map in a reverse order? Is there any predefined method in map to do that?

I'm creating it as follows:

LinkedHashMap<Integer, String> map = new LinkedHashMap<Integer,String>();
map.put(1, "one");
map.put(2, "two");
map.put(3, "three");
1

3 Answers 3

19

Try this, it will print the keys in reverse insertion order:

ListIterator<Integer> iter =
    new ArrayList<>(map.keySet()).listIterator(map.size());

while (iter.hasPrevious()) {
    Integer key = iter.previous();
    System.out.println(key);
}

You can also iterate by the reverse insertion order of entries:

ListIterator<Map.Entry<Integer, String>> iter =
    new ArrayList<>(map.entrySet()).listIterator(map.size());

while (iter.hasPrevious()) {
    Map.Entry<Integer, String> entry = iter.previous();
    System.out.println(entry.getKey() + ":" + entry.getValue());
}
4
  • I don't think this will work as the list iterator starts are the beginning of the list thus iter.hasPrevious() will be false on the very first call Jan 17, 2012 at 10:57
  • 3
    @GarethDavis you're wrong, the above list iterator starts at the end of the list - that's what listIterator(map.size()) does. Jan 17, 2012 at 11:06
  • listIterator is overloaded,one with no argument and one with position argument.listIterator() is same as listIterator(0) Jan 29, 2014 at 7:09
  • 1
    You might modify the first line to: ListIterator<Integer> iter = new ArrayList<Integer>(map.keySet()).listIterator(map.size()); for type safety reasons. Thank you for this answer, it fixed my problem.
    – kshepherd
    Nov 20, 2014 at 13:44
12
List<Entry<Integer,String>> list = new ArrayList<>(map.entrySet());

for( int i = list.size() -1; i >= 0 ; i --){
    Entry<Integer,String> entry = list.get(i);
}

Not really pretty and at the cost of a copy of the entry set, which if your map has a significant number of entries might be a problem.

The excellant Guava library have a [List.reverse(List<>)][2] that would allow you to use the Java 5 for each style loop rather than the indexed loop:

//using guava
for( Entry entry : Lists.reverse(list) ){
    // much nicer
}
1
  • Should be map.entrySet().
    – blackr1234
    Jun 20, 2020 at 16:24
4

Guava RULES:

List<Object> reverseList = Lists.reverse(
        Lists.newArrayList(map.keySet()));

Lists.reverse

Not the answer you're looking for? Browse other questions tagged or ask your own question.