简体   繁体   English

通过 1 个值获取 2 个键 java hashmap

[英]get 2 keys by 1 value java hashmap

i make top players in real time and I need to get the key (player's nickname) by the value of his points我实时制作顶级球员,我需要根据他的积分值获得钥匙(球员的昵称)

I get the key using this:我使用这个获取密钥:

HashMap<Player, Integer> score  = new HashMap<>();

    for(Player key: score.keySet()) {
        if(score.get(key).equals(VALUE OF POINT)) {
            Player = key;
        }
    }

everything works fine, but it can happen that the players have the same number of points and if you search, you will find only one key一切正常,但可能发生玩家拥有相同点数的情况,如果您搜索,您只会找到一个键

how to get the second key?如何获得第二把钥匙?

Create a player list that will gather the players that share the same score points创建一个玩家列表,该列表将收集共享相同分数的玩家

List<Player> sameScorePlayers = new ArrayList<>();
for(Player key: score.keySet()) {
    if(score.get(key).equals(VALUE OF POINT)) {
        sameScorePlayers.add(key);
    }
} 

This way sameScorePlayers will hold not only two but all players that have the same points.通过这种方式, sameScorePlayers将不仅拥有两个拥有相同积分的玩家,而且拥有所有拥有相同积分的玩家。

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

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