Using Apache Commons Collections I found the OrderedMapIterator
interface to navigate back and forth in OrderedMap
. Iterating to the next entry works as expected. Going to the previous element doesn't return the previous element but the current element instead.
OrderedMap<String, String> linkedMap = new LinkedMap<>();
linkedMap.put("key 1", "value 1");
linkedMap.put("key 2", "value 2");
linkedMap.put("key 3", "value 3");
OrderedMapIterator<String, String> iterator = linkedMap.mapIterator();
while (iterator.hasNext()) {
String key = iterator.next();
System.out.println(key);
if (key.endsWith("2") && iterator.hasPrevious()) {
System.out.println("previous: " + iterator.previous());
iterator.next(); // back to current element
}
}
I expected the output
key 1
key 2
previous: key 1
key 3
but got
key 1
key 2
previous: key 2
key 3
Do I use OrderedMapIterator
wrong or is this a bug?