Java 在哈希映射中查找第一个、第二个和第三个最小值

Java 在哈希映射中查找第一个、第二个和第三个最小值,java,hash,hashmap,hashtable,Java,Hash,Hashmap,Hashtable,我试图记住哈希映射中的最小值,并在另一个方法中使用它的键。我还想重复这样做,并排除以前找到的最小值。谢谢大家! Entry<Integer, Integer> min = null; for (Entry<Integer, Integer> entry : graph.myMap.entrySet()) { if (min == null || min.getValue() > entry.getValue()) {

我试图记住哈希映射中的最小值,并在另一个方法中使用它的键。我还想重复这样做,并排除以前找到的最小值。谢谢大家!

    Entry<Integer, Integer> min = null;
    for (Entry<Integer, Integer> entry : graph.myMap.entrySet()) {
        if (min == null || min.getValue() > entry.getValue()) {
            min = entry;
            int minV = min.getKey();
            deleteVertex(graph, minV);
        }
    }
Entry min=null;
for(条目:graph.myMap.entrySet()){
如果(min==null | | min.getValue()>entry.getValue()){
最小值=输入;
int minV=min.getKey();
deleteVertex(图,minV);
}
}
tl;博士 现在循环三个元素的整个列表

for ( Integer targetKey : targetKeys )
{
    Integer value = myMap.get ( targetKey );
    System.out.println ( "value: " + value );
}
看这个

价值:111

价值:222

价值:333


或者,使用Java Streams使用一行程序来增加趣味性

Map < Integer, Integer > myMap = Map.of ( 1 , 111 , 4 , 444 , 2 , 222 , 3 , 333 , 5 , 555 );

myMap
.keySet ()
.stream ()
.sorted ()
.limit ( 3 )
.map ( key -> myMap.get ( key ) )
.forEach ( System.out :: println )
;
MapmyMap=Map.of(111444424222333555);
我的地图
.keySet()
.stream()
.已排序()
.限额(3)
.map(key->myMap.get(key))
.forEach(System.out::println)
;
看这个

价值:111

价值:222

价值:333


不太了解第二部分关于重复的内容,无论如何,这可能会有帮助:

 public static void main(String[] args) {
        Map<Integer, Integer> map = new HashMap<>();
        map.put(1,10);
        map.put(2,100);
        map.put(3,20);
        map.put(4,990);

        Map.Entry<Integer, Integer> integerIntegerEntry = map.entrySet()
                .stream().min(Comparator.comparing(Map.Entry::getKey))
                .orElseThrow(() ->new RuntimeException("Oops!"));
        System.out.println("min val is :" + integerIntegerEntry.getValue());
        anotherMethod(integerIntegerEntry.getKey());
    }
publicstaticvoidmain(字符串[]args){
Map Map=newhashmap();
图.put(1,10);
地图放置(2100);
地图.put(3,20);
map.put(4990);
Map.Entry integerintegerEnterEntry=Map.entrySet()
.stream().min(Comparator.comparing(Map.Entry::getKey))
.orelsetrow(()->newruntimeexception(“Oops!”);
System.out.println(“最小值为:“+integerintegrationtry.getValue()”);
anotherMethod(integerintegerEnterentry.getKey());
}

如果你有100人,你想找到最年轻的一个,在你询问每个人的年龄之前,你不可能知道他是谁。因此,只有在循环完成后,您才知道谁是最年轻的。但您的代码在找到第一个条目或任何低于前一个条目的条目时会立即删除顶点。最简单的解决方法是按值对条目进行排序,然后再找到第一个、第二个、第三个最年轻的人就很简单了。是的,谢谢,我已将最后两行放在for循环之外。另外,感谢michalk的建议,我应该根据值进行排序,这将使我的生活更轻松。您可能重复使用了错误的数据结构来完成此任务。@AndyTurner是的,确实如此。固定的。谢谢
List< Integer > targetKeys = new ArrayList< Integer >( sortedKeys.subList( 0 , 3 ) ) ;
for ( Integer targetKey : targetKeys )
{
    Integer value = myMap.get ( targetKey );
    System.out.println ( "value: " + value );
}
Map < Integer, Integer > myMap = Map.of ( 1 , 111 , 4 , 444 , 2 , 222 , 3 , 333 , 5 , 555 );

myMap
.keySet ()
.stream ()
.sorted ()
.limit ( 3 )
.map ( key -> myMap.get ( key ) )
.forEach ( System.out :: println )
;
 public static void main(String[] args) {
        Map<Integer, Integer> map = new HashMap<>();
        map.put(1,10);
        map.put(2,100);
        map.put(3,20);
        map.put(4,990);

        Map.Entry<Integer, Integer> integerIntegerEntry = map.entrySet()
                .stream().min(Comparator.comparing(Map.Entry::getKey))
                .orElseThrow(() ->new RuntimeException("Oops!"));
        System.out.println("min val is :" + integerIntegerEntry.getValue());
        anotherMethod(integerIntegerEntry.getKey());
    }