如何获取地图中的上一个键/值和下一个键/值

时间:2022-10-26 13:30:50
for (Entry<Double, String> entry : map.entrySet()) { 
        Double key = entry.getKey(); 
        String value = entry.getValue(); 

        // double nextKey = ?
        // String nextvalue = ?

        // double prevKey = ?
        // String prevValue = ?
    } 

is it possible to know what the previous element and the next element while iterating the map?

是否有可能在迭代地图时知道前一个元素和下一个元素是什么?

2 个解决方案

#1


You can use NavigableMap for this, which entrySet()'s iterator return entries in ascending key order:

您可以使用NavigableMap,其中entrySet()的迭代器按升序键顺序返回条目:

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.

每个条目检索都是O(logN),因此对于完整迭代,这不是最有效的方法。为了更有效,在迭代时只需记住最后3个条目,并使用1st作为prev,第2个作为current,第3个作为next,正如@Malt建议的那样。

#2


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.

TreeMap是OrderedMap和NavigableMap,允许您向前和向后迭代,允许您分别使用lowerKey()和higherKey()访问上一个和下一个键。然而,它可能不是最好的解决方案。

Can you describe the actual problem you're trying to solve, and we can give you a more fitting solution?

你能描述一下你想解决的实际问题,我们可以给你一个更合适的解决方案吗?

#1


You can use NavigableMap for this, which entrySet()'s iterator return entries in ascending key order:

您可以使用NavigableMap,其中entrySet()的迭代器按升序键顺序返回条目:

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.

每个条目检索都是O(logN),因此对于完整迭代,这不是最有效的方法。为了更有效,在迭代时只需记住最后3个条目,并使用1st作为prev,第2个作为current,第3个作为next,正如@Malt建议的那样。

#2


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.

TreeMap是OrderedMap和NavigableMap,允许您向前和向后迭代,允许您分别使用lowerKey()和higherKey()访问上一个和下一个键。然而,它可能不是最好的解决方案。

Can you describe the actual problem you're trying to solve, and we can give you a more fitting solution?

你能描述一下你想解决的实际问题,我们可以给你一个更合适的解决方案吗?