for (Entry entry : map.entrySet()) {
Double key = entry.getKey();
String value = entry.getValue();
// double nextKe
You can use NavigableMap for this, which entrySet()'s iterator return entries in ascending key order:
NavigableMap<Double, String> myMap = new TreeMap<>();
//...
for (Map.Entry<Double, String> e : myMap.entrySet()) {
Map.Entry<Double, String> next = myMap.higherEntry(e.getKey()); // next
Map.Entry<Double, String> prev = myMap.lowerEntry(e.getKey()); // previous
// do work with next and prev
}
Every entry retrieval is O(logN), so for full iteration this is not the most effective approach. To be more effective, on iteration just remember last 3 entries, and use 1st as prev, 2nd as current and 3rd as next, as @Malt suggests.
A TreeMap is an OrderedMap and a NavigableMap and will allow you to iterate forward and backward, allowing you to access previous and next keys with lowerKey() and higherKey() respectively. However it might not be the best solution.
Can you describe the actual problem you're trying to solve, and we can give you a more fitting solution?