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)); }
iftheshoefritz
source share