I have HashMAp
items = new HashMap<String, String>();
items.put("A", "1");
items.put("B", "2");
items.put("C", "3");
I need for each last to first.
"C", "3"
"B", "2"
"A", "1"
You can use a NavigableMap (TreeMap is a NavigableMap), which is a SortedMap with navigation capabilities.
NavigableMap#descendingMap() returns a reverse order view (not a copy) of the mappings contained in 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 the keys in your Map. If you care about the insertion ordering or the access ordering, then have a look at LinkedHashMap.
Note 2 : In your question, you used a HashMap. Please note that HashMap doesn't guarantee any order for its elements. Actually, it doesn't even guarantee the order will remain constant over time. See the first paragraph of HashMap's javadoc for further references.
EDIT: Myself and Matthew clearly have different interpretations of what your question means. Do you mean reverse order that you inserted, or reverse order of keys?
If you mean reverse order of keys, here's how to do it:
Use an ordered Map, like TreeMap and then iterate over items.keySet().
TreeMap sorts by natural order of your key values, so you will need to pass in a comparator to the constructor 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));
}
Map<String, String> items = new TreeMap<String, String>(Collections.reverseOrder());HashMap doesn't guarantee any ordering. If you use LinkedHashMap, it will be ordered by insertion, but there is still no convenient way to go backwards.
One way would be to call items.entrySet(). That returns a Set<Map.Entry>. You can then get the size of the set, call toArray(), then do a descending for loop.
Another method - 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));
}
}
}
output:
key: C, value: 3 key: B, value: 2 key: A, value: 1
HashMapdoesn't guarantee any order for its elements. In particular, it doesn't guarantee that the order remains constant over time. Thus,HashMapis not what you want if you have to maintain any kind of order.