简体   繁体   English

如何在哈希图中找到最低值?

[英]How to find the lowest value in a hashmap?

I am unable to find the lowest value in a hashmap. 我无法在哈希图中找到最小值。 There is only one hashmap and in it the lowest value of players must be found. 只有一个哈希图,并且必须在其中找到玩家的最低价值。

                for (Map.Entry<Player, Integer> entry1 : lowestTrump.entrySet()) {
                    Player key1 = entry1.getKey();
                    int value1 = entry1.getValue();

                    for (Map.Entry<Player, Integer> entry2 : lowestTrump.entrySet()) {
                        Player key2 = entry2.getKey();
                        int value2 = entry2.getValue();

                        if(value1 == -1 || value2 == -1){
                            break;
                        }else if(value1 < value2 && (value1 != 1 || value2 != 1)) {
                            attackerFound = key1.getName();
                        }
                    }
                }

The output should assign the lowest value among Players into attackerFound variable. 输出应将Players中的最低值分配给AttackerFound变量。

Simply use a stream() with min() to get the lowest value 只需将stream()min()即可获得最小值

lowestTrump.values().stream().min(Integer::compareTo);

if the lowestTrump can be null you can also wrap lowestTrump.values() with apache CollectionUtils.emptyIfNull()) or use some another null-safe approach 如果lowestTrump可以为null,则还可以使用apache CollectionUtils.emptyIfNull())包装lowestTrump.values() CollectionUtils.emptyIfNull())或使用其他一些空值安全的方法

You can get the minimum player in a single step like this: 您可以像这样一步获得最低的玩家:

Player lowestPlayer = lowestTrump.entrySet()
        .stream()
        .min(Comparator.comparingInt(Map.Entry::getValue))
        .map(Map.Entry::getKey)
        .orElse(null);

String attackerFound = lowestPlayer != null ? lowestPlayer.getName() : null;

Be aware that if lowestTrump was empty then attackerFound will be null . 请注意,如果lowestTrump是空的,然后attackerFound将是null

You can use treemap. 您可以使用树形图。 So that when you add any value to that map, It will already be sorted. 这样,当您向该地图添加任何值时,它就会被排序。 You can get first value and that will be the lowest or wise Versa. 您可以获得第一个值,它将是最低的或明智的Versa。

Map <Player, Integer> playerMap = new TreeMap<Player, Integer>();

For Java 8+ you can use below example : 对于Java 8+,您可以使用以下示例:

playerMap.values().stream().min(Integer::compareTo);

That's it. 而已。

Here is my actual working code 这是我的实际工作代码

                Optional<Player> result = lowestTrump.entrySet()
                        .stream()
                        .filter(c -> c.getValue() != -1)
                        .min(Comparator.comparingInt(Map.Entry::getValue))
                        .map(Map.Entry::getKey);

                if(result != null && result.get().getSuitInHand() != -1) {
                    System.out.println("Player : " + result.get().getName() + " is the first attacker");
                }

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

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