Iterating through a LinkedHashMap in reverse order

后端 未结 6 656
粉色の甜心
粉色の甜心 2020-11-28 12:06

I have a LinkedHashMap:

LinkedHashMap

that I need to iterate through from a given key\'s position, backwar

6条回答
  •  生来不讨喜
    2020-11-28 12:36

    You don't have to iterate through it. But it would be handy to pull the keys off and store it in a list. Thats the only way you can do indexOf() type operations.

    List keyList = new ArrayList(map.keySet());
    // Given 10th element's key
    String key = "aKey";
    int idx = keyList.indexOf(key);
    for ( int i = idx ; i >= 0 ; i-- ) 
     System.out.println(map.get(keyList.get(i)));
    

提交回复
热议问题