1
private static void theEnd() {
    Map<Client, Integer> Score = new HashMap<Client, Integer>();
    for (Client player : getPlayers()) {
        Client c = (Client) player;
        Score.add(c, c.gameScore);
    }
}

基本的に、すべてのクライアントをループし、ゲーム スコアを新しいマップ スコアに追加します。これで、配列リストに値を用意できたので、ゲームの勝者を割り当てたいと思います。

勝者になるには、最高のスコアを獲得する必要があります。

私の質問:

マップ コレクションで最高のゲーム スコアを見つけるにはどうすればよいですか?

4

9 に答える 9

2

最大スコアを見つけたいだけで、それを達成したものは気にClientしない場合は、使用できます Collections.max

 int maxScore = Collections.max(Score.values());

クライアントとスコアを気にする場合はComparator、値に基づいてマップ エントリを比較する匿名インスタンスで結果を取得できます。

 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());
            }
        }
  );

また、補足として、Java の規則では、小文字で始まる変数名を付けます。

于 2013-08-09T11:04:27.967 に答える
1

ソートされたマップが必要なようです

マップは値ではなくキーでソートされるため、必ずスコアをキーとして使用してください。

于 2013-08-09T11:06:19.180 に答える
0

またはTreeSet、compareTo メソッドを使用してオーバーライドします。次に、比較に基づいて最初または最後のエントリを取得することもできます。基本的にTreeMap、追加のキーや値を必要とせず、オブジェクトを直接操作する必要がないため、 よりもそれを好みます (冗長なデータは省略されます)。

于 2013-08-09T11:10:20.633 に答える
0

Clientクラスが実装し、次のような実装がある場合Comparable<Client>:

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

次に、使用できます

Client maxScoreClient = Collections.max(getPlayers());
于 2013-08-09T11:10:29.443 に答える
0

あなたはすでにトラフクライアントを実行しているので、最高スコアキーを保存するだけでクライアントを取得できます:)

 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 );
于 2013-08-09T11:12:32.287 に答える
0

TreeMapエントリ (キーと値のマッピング) をキーの自然な順序で保持するような、並べ替えられたコレクションを使用します。ハイスコ​​アでソートしたいので、スコアをキーとして、プレーヤーを値として保持します。

2 人以上のプレイヤーが同じハイ スコアを持つ可能性は非常に高いです。そのため、スコアをプレーヤーにマッピングする代わりに、List(同じ高スコアを持つ)プレーヤーにマッピングする必要があります。

// {key - value} = {high score - {list, of, players}}
TreeMap<Integer, List<Client>> highestScores =
                               new TreeMap<Integer, List<Client>>();

for (Client client : getPlayers()) {
    List<Client> playerList = null;

    // make gameScore private
    Integer score = client.getGameScore(); // using getters() recommended

    // check if a player with this score already exists
    if ((playerList = highestScores.get(score)) == null) { // if NOT,
        playerList = new ArrayList<Client>(1); // CREATE a new list
        playerList.add(client);
        highestScores.put(score, playerList);
    } else { // if YES, ADD to the existing list
        playerList.add(client);
    }
}

すべてのハイスコアを反復するには、次を使用します

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<Integer, List<Client>> highest = highestScores.lastEntry();
System.out.println(highest.getKey() + " : " + highest.getValue());
于 2013-08-09T11:03:05.280 に答える
0

maxSoFar というクライアントを作成します。ループ内: maxSoFar == null の場合、それを置き換えます。それ以外の場合 c.gameScore() > maxSoFar.getScore() maxSoFar を置き換えます

ループが完了すると、ゲームスコアが最も高いクライアントが maxSoFar 変数に格納されます。

于 2013-08-09T11:04:33.400 に答える
0

要素をマップに追加している間、次のような最大値を保存できます。

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;
       }
   }
}

次に、 variable で勝者にアクセスしますwinner

于 2013-08-09T11:06:44.763 に答える
0

問題を簡単に解決するために、別のデータ構造を使用してみてください。このようなもの :

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
于 2013-08-09T11:17:48.753 に答える