Java ArrayList-获取最高的int值? private static void theEnd(){ Map Score=newhashmap(); for(客户端播放器:getPlayers()){ 客户c=(客户)玩家; 添加(c,c.游戏分数); } }

Java ArrayList-获取最高的int值? private static void theEnd(){ Map Score=newhashmap(); for(客户端播放器:getPlayers()){ 客户c=(客户)玩家; 添加(c,c.游戏分数); } },java,collections,map,Java,Collections,Map,基本上,它遍历所有客户端,并将它们的游戏分数添加到我的新地图分数中。 现在,我已经准备好了带有值的arraylist,我想在游戏中分配一个赢家 要想成为赢家,你必须得最高的分数 我的问题: 如何在地图集合中找到最高的游戏分数?使用排序的集合,如TreeMap,该集合将其条目(键值映射)按其键的自然顺序保存。因为,您希望根据高分进行排序,将您的分数作为关键,将玩家作为他们的价值观 很有可能两个或两个以上的球员会有相同的高分。因此,我们不需要将分数映射到一个玩家,而需要将其映射到一个玩家列表(具有相

基本上,它遍历所有客户端,并将它们的游戏分数添加到我的新地图分数中。 现在,我已经准备好了带有值的arraylist,我想在游戏中分配一个赢家

要想成为赢家,你必须得最高的分数

我的问题:


如何在地图集合中找到最高的游戏分数?

使用排序的集合,如
TreeMap
,该集合将其条目(键值映射)按其键的自然顺序保存。因为,您希望根据高分进行排序,将您的分数作为关键,将玩家作为他们的价值观

很有可能两个或两个以上的球员会有相同的高分。因此,我们不需要将分数映射到一个玩家,而需要将其映射到一个玩家列表(具有相同的高分):

要直接打印出最高分数,请使用

for (Integer score : highestScores.descendingKeySet()) {
    for (Client player : highestScores.get(score)) { // iterate over player list
        System.out.println(player.getName() + ": " + score); // assuming "name" property
    }
}
Map.Entry highest=最高分.lastEntry();
System.out.println(highest.getKey()+“:”+highest.getValue());

如果您只想找到最高分数,而不想知道哪个
客户机获得了最高分数,那么您可以使用它

如果您确实关心客户机和分数,则可以使用匿名实例获取结果,该实例将根据地图条目的值对其进行比较

 int maxScore = Collections.max(Score.values());
Entry maxEntry=Collections.max(Score.entrySet(),
新比较器(){
@凌驾
公共整数比较(条目o1、条目o2){
返回o1.getValue().compareTo(o2.getValue());
}
}
);

另外,作为旁注,Java中的惯例是给变量命名,以小写字母开头。

创建一个名为maxSoFar的客户端。 在循环中: 如果maxSoFar==null,则替换它。 否则,如果c.gameScore()>maxSoFar.getScore() 更换maxSoFar

循环完成后,游戏分数最高的客户端位于maxSoFar变量中。

听起来您需要一个


确保将分数用作键,因为地图是按键而不是按值排序的。

向地图添加元素时,可以保存最大值,例如:

 Entry<Client, Integer> maxEntry = Collections.max(Score.entrySet(), 
       new Comparator<Entry<Client, Integer>>() {
            @Override
            public int compare(Entry<Client, Integer> o1, Entry<Client, Integer> o2) {
                return o1.getValue().compareTo(o2.getValue());
            }
        }
  );
private static void theEnd(){
int max=-Integer.max_值;
客户赢家=空;
Map Score=newhashmap();
for(客户端播放器:getPlayers()){
客户c=(客户)玩家;
添加(c,c.游戏分数);
如果(c.游戏分数>最大值){
max=c.gameScore;
获胜者=c;
}
}
}

然后通过变量
winner
访问winner,或者使用
TreeSet
覆盖compareTo方法。然后,您还可以根据比较结果选择第一个或最后一个条目。基本上,我更喜欢使用
树映射,因为如果
客户机
类实现了
可比
并且具有如下实现,则不需要额外的键或值,直接处理对象(省略冗余数据)

private static void theEnd() {
   int max = -Integer.MAX_VALUE;       
   Client winner = null;

   Map<Client, Integer> Score = new HashMap<Client, Integer>();
   for (Client player : getPlayers()) {
       Client c = (Client) player;
       Score.add(c, c.gameScore);

       if( c.gameScore > max ){
          max =c.gameScore;
          winner = c;
       }
   }
}
那你可以用

public int compareTo(Client that) {
     return Integer.compare(this.getScore(), that.getScore())
}

您已经在通过客户端运行,因此只需保存最高分键,然后您就有了您的客户端:)

Map Score=newhashmap();
整数最高分=0;
for(player:getPlayers()){
客户c=(客户)玩家;
如果(c.游戏分数>最高分数){
最高分数=c.gameScore;
}
添加(c.gameScore,c);
}
客户赢家=得分。获得(最高分);

尝试使用不同的数据结构来轻松解决问题。大概是这样的:

 Map<Integer, Client> Score = new HashMap<Integer, Client>();
 Integer highestScore = 0;
    for (player : getPlayers()) {
        Client c = (Client) player;
        if(c.gameScore > highestScore ){
            highestScore = c.gameScore;
        }
        Score.add(c.gameScore, c);
    }

 Client winner = score.get(highestScore );
Map Map=newhashmap();
for(客户端播放器:getPlayers()){
int score=player.gameScore;
if(地图容器(分数)){
地图。获取(分数)。添加(玩家);
}否则{
列表=新的ArrayList();
列表。添加(玩家);
地图。放置(分数、列表);
}
}
整数max=Collections.max(map.keySet());
系统输出打印项次(最大);//获得最高分
System.out.println(map.get(max));//获得所有得分最高的球员名单

这些代码甚至都不会编译。您缺少for循环变量的引用类型。现在编辑它就可以了。问题是,我需要知道是什么客户端实现了它。
private static void theEnd() {
   int max = -Integer.MAX_VALUE;       
   Client winner = null;

   Map<Client, Integer> Score = new HashMap<Client, Integer>();
   for (Client player : getPlayers()) {
       Client c = (Client) player;
       Score.add(c, c.gameScore);

       if( c.gameScore > max ){
          max =c.gameScore;
          winner = c;
       }
   }
}
public int compareTo(Client that) {
     return Integer.compare(this.getScore(), that.getScore())
}
Client maxScoreClient = Collections.max(getPlayers());
 Map<Integer, Client> Score = new HashMap<Integer, Client>();
 Integer highestScore = 0;
    for (player : getPlayers()) {
        Client c = (Client) player;
        if(c.gameScore > highestScore ){
            highestScore = c.gameScore;
        }
        Score.add(c.gameScore, c);
    }

 Client winner = score.get(highestScore );
Map<Integer, List<Client>> map = new HashMap<Integer, List<Client>>();

        for (Client player : getPlayers()) {

            int score = player.gameScore;

            if (map.containsKey(score)) {
                map.get(score).add(player);
            } else {
                List<Client> list = new ArrayList<Client>();
                list.add(player);
                map.put(score, list);
            }
        }
        Integer max = Collections.max(map.keySet());
        System.out.println(max); // Get the maximum score
        System.out.println(map.get(max)); // get the list of all the players with maximum score