简体   繁体   English

如何在哈希映射键中找到下一个最小值?

[英]How to find next minimum in a hash map key?

I have a scenario where I have a map: 我有一个有地图的场景:

BiMap <Integer, String> map; 

and a list which tracks the index which are processed. 以及跟踪所处理索引的列表。

List<Integer> filled;

While processing if the filled list contains the key then I should find the next min key from the map which is already not in the filled list. 在处理填充列表中是否包含键时,我应该从映射中找到下一个最小键,该键已不在填充列表中。

Can some one tell me is there an easy way to do this without doing many iterations? 有人可以告诉我是否有一种简单的方法可以进行多次迭代?

for (int i =0 ;i <size; i++) {
    if(map.contiansKey(i)) {
        Integer min = list.contains(i) ? getNextMinFrom(map, filled) : null;
        if min != null ?System.out.println(map.get(min)) : continue;
    }
}

For Eg: 例如:

Filled - 0, 4, 5, 6

Map - (1,dfs) (4,efs) (5,sdfs) 

in the 0 th iteration..  output - dfs (1 is min)
      in 4th iteration ... output shoudl be 4efs (4 is the next min) 
.... 

The issue is I can not remove entries from the list. 问题是我无法从列表中删除条目。

使用以map的“ KeySet”初始化的TreeSet ,并在添加“ Any key” filled将其从“ TreeSet”中删除,因此, TreeSet的第一个元素就是您想要的

Is this what you want to achieve? 这是您要达到的目标吗? Start from the front of the filled array and go until you find the first value that is in filled but not in the map? 从填充数组的开头开始,直到找到第一个在fill中但不在地图中的值?

public int get smallestUnusedValue() {
    for(int i = 0; i < filled.size(); i++)
        if(!map.containsKey(filled.get(i)))
            return i;
}

Do you need to use a HashMap specifically? 您是否需要专门使用HashMap Implementations of NavigableMap , such as TreeMap , provide methods to look up keys "adjacent to" a given value, without requiring an exact map. NavigableMap实现(例如TreeMap )提供了无需特定映射即可查找与给定值“相邻”的键的方法。 The higherEntry method seems to do what you want. higherEntry方法似乎higherEntry您的要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM