Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate hashmap in reverse order in Java

Tags:

java

hashmap

I am trying this for some hour but not finding any best approach to achieve iteration of hashmap in reverse order, this is the hashmap I have.

      Map<Integer, List<String>> map = new HashMap<Integer, List<String>>();

             for(Integer key : map.keySet()) {
                List<String> value = map.get(key);
                List<Map<String,?>> security = new LinkedList<Map<String,?>>();  
                for(int ixy = 0; ixy < value.size()-1; ixy++){
                    security.add(createItem(value.get(ixy), value.get(ixy+1))); 
                }
                adapter.addSection(Integer.toString(key), new SimpleAdapter(getApplicationContext(), security, R.layout.list_complex, new String[] { ITEM_TITLE, ITEM_CAPTION }, new int[] { R.id.list_complex_title, R.id.list_complex_caption }));  
            }

I have seen example of TreeMap as well,

             Map<Integer, List<String>> sortedMap = new TreeMap<Integer, List<String>>(map);

But treemap also gives in ascending order, what I want is in descending order.

like image 529
Programmer Avatar asked May 15 '12 07:05

Programmer


People also ask

Does HashMap iterate in order?

Unlike LinkedHashMap (insertion-order) and TreeMap (key-based order), HashMap does not provide any guarantees of ordering when iterating over the map, e.g. when using a for-each loop, or calling forEach() . Instead, the iteration order of a HashMap depends on internal implementation details.

How do you reverse a traverse map in Java?

There are three simple ways to iterate TreeMap in reverse order in Java: Using the reverseOrder() method. Using the descendingKeySet() method. Using the descendingMap() method.


3 Answers

best approach to acheive iteration of hashmap in reverse order

HashMap does not define any particular ordering of its element. Therefore the "reverse" order isn't defined either.

For a TreeMap, you can use descendingMap().

like image 138
NPE Avatar answered Oct 13 '22 03:10

NPE


Hashmap does not have specific order. But you can use TreeMap.

Perhaps this simple example can help you :

Map<Integer, String> map = new TreeMap<Integer, String>();
        map.put(1, "abc1");
        map.put(2, "abc2");
        map.put(3, "abc3");

        ArrayList<Integer> keys = new ArrayList<Integer>(map.keySet());
        for(int i=keys.size()-1; i>=0;i--){
            System.out.println(map.get(keys.get(i)));
        }
like image 38
Kshitij Avatar answered Oct 13 '22 03:10

Kshitij


A HashMap doesn't maintain eny order between keys.

A TreeMap orders its keys by their natural order, or by the order imposed by a comparator that you pass when constructing the map. So if you want to have Integer keys ordered in reverse order, construct the TreeMap this way:

Map<Integer, List<String>> sortedMap = 
    new TreeMap<Integer, List<String>>(Collections.reverseOrder());
like image 25
JB Nizet Avatar answered Oct 13 '22 01:10

JB Nizet