Iterate through LinkedHashMap in reverse order

I have a LinkedHashMap:

LinkedHashMap<String, RecordItemElement> 

that I need to go through the given position of the key back. Therefore, if I was assigned the key of the 10th item, I will need to repeat the iteration through the hash map 9, 8, 7, etc.

+32
java hashmap
Aug 24 2018-11-11T00:
source share
7 answers

You do not need to go through it. But it would be convenient to pull out the keys and save them in a list. This is the only way you can perform operations like indexOf ().

 List<String> keyList = new ArrayList<String>(map.keySet()); // Given 10th element key String key = "aKey"; int idx = keyList.indexOf(key); for ( int i = idx ; i >= 0 ; i-- ) System.out.println(map.get(keyList.get(i))); 
+7
Aug 24 2018-11-11T00:
source share

HashMap:

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

To iterate over the values ​​again:

 ListIterator<Sprite> iterator = new ArrayList<String>(map.values()).listIterator(map.size()); while (iterator.hasPrevious()) String value = iterator.previous(); 

To reverse iterate over the keys:

 ListIterator<Integer> iterator = new ArrayList(map.keySet()).listIterator(map.size()); while (iterator.hasPrevious()) Integer key = iterator.previous(); 

To reverse iterate over both:

 ListIterator<Map.Entry<Integer, String>> iterator = new ArrayList<Map.Entry<Integer, String>>(map.entrySet()).listIterator(map.size()); while (iterator.hasPrevious()) Map.Entry<Integer, String> entry = iterator.previous(); 
+9
Aug 12 '14 at 8:38
source share

The question requires LinkedHashMap in the reverse order; some answers suggest using a TreeSet, but this will change the order of the map based on the key.

This solution allows iterations over the original LinkedHashMap to not be a new ArrayList, as suggested:

 List<String> reverseOrderedKeys = new ArrayList<String>(linkedHashMap.keySet()); Collections.reverse(reverseOrderedKeys); for (String key : reverseOrderedKeys) { RecordItemElement line = linkedHashMap.get(key); } 
+9
Feb 17 '16 at 9:43
source share

I would say use TreeMap for this. Then you do not need to copy the entire collection if you want to access it in any order. Please note that the key must take care of the order.

 TreeMap map = new TreeMap(); map.put(1, "c"); map.put(2, "b"); map.put(3, "a"); NavigableSet set = m.descendingKeySet(); 

Or look at org.apache.commons.collections. *

https://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/OrderedMap.html

+2
May 7 '12 at 15:51
source share

Using the "user22745008" solution and labdas with some generics, you can have a very neat solution as a method:

  public static <T, Q> LinkedHashMap<T, Q> reverseMap(LinkedHashMap<T, Q> toReverse) { LinkedHashMap<T, Q> reversedMap = new LinkedHashMap<>(); List<T> reverseOrderedKeys = new ArrayList<>(toReverse.keySet()); Collections.reverse(reverseOrderedKeys); reverseOrderedKeys.forEach((key)->reversedMap.put(key,toReverse.get(key))); return reversedMap; } 
0
Feb 14 '17 at 4:02 on
source share

This is an old question, but I think it lacks an answer that requires a newer approach. The following uses Java 9 features:

 Deque<Map.Entry<String, RecordItemElement>> top = map.entrySet().stream() .takeWhile(e -> !givenKey.equals(e.getKey())) .collect(Collectors.toCollection(ArrayDeque::new)); 

The above code streams a set of map entries, storing entries until a key equal to that key is found. Then the entries are collected in ArrayDeque .

One detail is missing though. Depending on whether you need an entry that matches the specified key to include in the result or not, you may need to manually add it to the queue. If you do not want this to be added, then everything is ready. Otherwise, just do:

 top.add(Map.entry(givenKey, map.get(givenKey))); 

Now, to Deque over Deque in reverse, just use its descendingIterator() :

 Iterator<Map.Entry<String, RecordItemElement>> descIt = top.descendingIterator(); 

It is worth noting that this approach only works if the thread is sequential . In any case, we would not get anything using a parallel thread here.

0
Jan 21 '18 at 20:01
source share

In case you can accept the reverse order of the card itself, there is a constructor that allows you to do this.

0
Jan 30 '19 at 14:23
source share



All Articles