我有一个场景,我有一张 map :
BiMap <Integer, String> map;
和一个跟踪已处理索引的列表。
List<Integer> filled;
在处理时,如果填充列表包含 key ,那么我应该从 map 中找到下一个最小 key ,该 key 已经不在填充列表中。
有人可以告诉我是否有一种简单的方法可以做到这一点而无需进行多次迭代?
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;
}
}
例如:
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)
....
问题是我无法从列表中删除条目。
最佳答案
使用使用 map
的“KeySet”初始化的TreeSet
,并且在将任何键添加到filled
时将其从“TreeSet”中删除,结果,TreeSet
的第一个元素就是你想要的
关于java - 如何找到 HashMap 键中的下一个最小值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43647282/