Java在hashmap中找到多个最低的键/值对[重复]

时间:2022-11-26 19:17:13

This question already has an answer here:

这个问题在这里已有答案:

I have a small HashMap (less than 100 entries) that contains a unique object (of my design) as the key and a Double as the value.

我有一个小的HashMap(少于100个条目),它包含一个唯一的对象(我的设计)作为键,Double作为值。

I need to retrieve n number of objects that have the lowest values.

我需要检索n个具有最低值的对象。

So say my HashMap looked like this and I wanted the lowest 3.

所以说我的HashMap看起来像这样,我想要最低的3。

Object, 4.0

Object, 5.0

Object, 2.0

Object, 12.0

Object, 10.0

Object, 3.0

I would want to fetch the first, third, and last entries as those have the lowest values.

我想获取第一个,第三个和最后一个条目,因为它们具有最低值。

I know there are methods such as Collections.min which I could run on the HashMap but I need more than just the lowest value and I need to know the key it corresponds to as well. Research has also led me to come across Selection Algorithms but I am confused and not quite sure how to use these. I apologise if a question of this sort has been asked before, I searched for a long time and could not find anything. Thanks pre-emptively for your help.

我知道有一些方法,比如Collections.min,我可以在HashMap上运行,但我需要的不仅仅是最低值,我还需要知道它对应的键。研究也让我遇到了选择算法,但我很困惑,不太确定如何使用它们。如果之前有人询问过这类问题,我很抱歉,我搜索了很长时间但找不到任何东西。先谢谢你的帮助。

3 个解决方案

#1


3  

List<Entry<Key, Double>> lowestThreeEntries = map.entrySet()
        .stream()
        .sorted(Comparator.comparing(Entry::getValue))
        .limit(3)
        .collect(Collectors.toList());

#2


0  

Put all then entries of your map inside an array. Then, implement a Comparator<Map.Entry<Key,Value>> which compares the map entries according to the value they hold.

将地图的所有条目放在数组中。然后,实现Comparator >,根据它们保存的值比较映射条目。

And finally, sort the entries of the map according to your shiny comparator. (You can use Arrays.sort)

最后,根据闪亮的比较器对地图的条目进行排序。 (你可以使用Arrays.sort)

#3


0  

Consider inverting your map and using a 'MultiMap' (effectively a more user friendly implementation of Map<Key, List<Value>>), the Guava TreeMultiMap should do exactly what you're after.

考虑反转你的地图并使用'MultiMap'(实际上是一个用户友好的Map >的实现),Guava TreeMultiMap应该完全按照你所追求的去做。 ,list>

http://google.github.io/guava/releases/19.0/api/docs/com/google/common/collect/TreeMultimap.html

Edit - Added example (using String in place of the custom object for brevity)

编辑 - 添加了示例(为简洁起见,使用String代替自定义对象)

    TreeMultimap<Double, String> map = TreeMultimap.<Double, String>create();
    map.put(4.0, "a");
    map.put(5.0, "b");
    map.put(2.0, "c");
    map.put(12.0, "d");
    map.put(10.0, "e");
    map.put(3.0, "f");

    Iterator<String> iterator = map.values().iterator();
    while (iterator.hasNext()) {
        System.out.println(iterator.next());
    }

This will print out the strings in ascending order according to the double value (c, f, a, b, e, d)

这将根据double值(c,f,a,b,e,d)按升序打印出字符串

#1


3  

List<Entry<Key, Double>> lowestThreeEntries = map.entrySet()
        .stream()
        .sorted(Comparator.comparing(Entry::getValue))
        .limit(3)
        .collect(Collectors.toList());

#2


0  

Put all then entries of your map inside an array. Then, implement a Comparator<Map.Entry<Key,Value>> which compares the map entries according to the value they hold.

将地图的所有条目放在数组中。然后,实现Comparator >,根据它们保存的值比较映射条目。

And finally, sort the entries of the map according to your shiny comparator. (You can use Arrays.sort)

最后,根据闪亮的比较器对地图的条目进行排序。 (你可以使用Arrays.sort)

#3


0  

Consider inverting your map and using a 'MultiMap' (effectively a more user friendly implementation of Map<Key, List<Value>>), the Guava TreeMultiMap should do exactly what you're after.

考虑反转你的地图并使用'MultiMap'(实际上是一个用户友好的Map >的实现),Guava TreeMultiMap应该完全按照你所追求的去做。 ,list>

http://google.github.io/guava/releases/19.0/api/docs/com/google/common/collect/TreeMultimap.html

Edit - Added example (using String in place of the custom object for brevity)

编辑 - 添加了示例(为简洁起见,使用String代替自定义对象)

    TreeMultimap<Double, String> map = TreeMultimap.<Double, String>create();
    map.put(4.0, "a");
    map.put(5.0, "b");
    map.put(2.0, "c");
    map.put(12.0, "d");
    map.put(10.0, "e");
    map.put(3.0, "f");

    Iterator<String> iterator = map.values().iterator();
    while (iterator.hasNext()) {
        System.out.println(iterator.next());
    }

This will print out the strings in ascending order according to the double value (c, f, a, b, e, d)

这将根据double值(c,f,a,b,e,d)按升序打印出字符串