How does iteration go to first in HashMap?

I have a hashmap

items = new HashMap<String, String>(); items.put("A", "1"); items.put("B", "2"); items.put("C", "3"); 

I need for each last one first.

 "C", "3" "B", "2" "A", "1" 
+6
java
source share
4 answers

EDIT: Matthew and I have different interpretations of what your question means. Do you mean the reverse order you inserted, or the reverse order of keys?

If you mean the reverse order of keys, here's how to do it:

Use an ordered map like TreeMap and then iterate through items.keySet ().

TreeMap sorts in the natural order of your key values, so you will need to pass a constructor to the comparator to sort the keys in reverse order:

 Map<String, String> items = new TreeMap<String, String>(new Comparator<String>() { public int compare(String a, String b) { return b.compareTo(a); } }); items.put("A", "1"); items.put("B", "2"); items.put("C", "3"); for (String s: items.keySet()) { System.out.println(s + " " + items.get(s)); } 
+4
source share

You can use NavigableMap ( TreeMap is NavigableMap ), which is a SortedMap with navigation capabilities.

NavigableMap#descendingMap() returns the reverse order (not copy) of the mappings contained on this map.

Example:

 NavigableMap<String, String> items = new TreeMap<String, String>(); items.put("B", "2"); items.put("A", "1"); items.put("C", "3"); for (Map.Entry<String, String> e : items.entrySet()) { System.out.println(e); } // gives // A=1 // B=2 // C=3 for (Map.Entry<String, String> e : items.descendingMap().entrySet()) { System.out.println(e); } // gives // C=3 // B=2 // A=1 

Note. This answer is valid if you care about the natural ordering of keys in Map . If you need to arrange the insertion order or access order, check out LinkedHashMap .

Note 2: In your question, you used HashMap . Please note that HashMap does not guarantee the order for its elements. In fact, this does not even guarantee that the order will remain constant over time. See the first paragraph of the HashMap javadoc for details.

+6
source share

HashMap does not guarantee any order. If you use LinkedHashMap , it will be streamlined by the insert, but there is still no convenient way to go back.

One way is to call items.entrySet() . This returns a Set<Map.Entry> . Then you can get the size of the set, call toArray() , then do a downward loop.

+1
source share

Another method is to create a SortedSet of your keys:

 import java.util.*; class MyComparator implements Comparator<String> { public int compare(String a, String b) { return -a.compareTo(b); } public boolean equals(String a, String b) { return a.equals(b); } } public class test { public static void main(String[] args) { HashMap<String, String> items = new HashMap<String, String>(); items.put("A", "1"); items.put("B", "2"); items.put("C", "3"); TreeSet<String> ts = new TreeSet<String>(new MyComparator()); ts.addAll(items.keySet()); for(Iterator<String> i = ts.iterator(); i.hasNext(); ) { String key = i.next(); System.out.println("key: " + key + ", value: " + items.get(key)); } } } 

exit:

  key: C, value: 3
 key: B, value: 2
 key: A, value: 1
0
source share

All Articles