How to iterate hashmap in reverse order in Java

后端 未结 12 1172
别那么骄傲
别那么骄傲 2020-12-30 01:08

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

        
12条回答
  •  Happy的楠姐
    2020-12-30 02:01

    You can use TreeMap#descendingKeySet method.

    Map> map = new TreeMap>();
    
    for(Integer key : map.descendingKeySet()) {
        List value = map.get(key);
        List> security = new LinkedList>();  
        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 }));
    } 
    

    Reference:

    https://docs.oracle.com/javase/8/docs/api/java/util/TreeMap.html#descendingKeySet--

提交回复
热议问题