-
largeQ
像这样的东西if (players == null || players.size() == 0) { throw new RuntimeException("You had no players!!");}Player winner = players.get(0);for (Player p: players) { if (p.getScore() > winner.getScore) { winner = p; }}winner; // here you got player with the most score.
-
慕尼黑的夜晚无繁华
由于出于某种原因您获得了某种多个获胜者,我认为获胜者是比某些宝藏更多积分的玩家,并且可能只有固定数量的此类玩家(例如第 1、第 2、第 3 名)。 int winningScoreTreashold=5///somethis; int winningPlayersCount=3; List<Player> winners = players.stream() .sorted(Comparator.comparing(Player::getScore)) .filter(p -> p.getScore() > winningScoreTreashold) .limit(winningPlayersCount) .collect(Collectors.toList()); winners//here you got winners;删除任何不符合您需求的条件。
-
慕慕森
像这样的事情可以工作:import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.List;import java.util.stream.Collectors;class Main { public static void main(String[] args) { List<Player> players = new ArrayList<>(); players.add(new Player("Adam", 1)); players.add(new Player("Bob")); players.add(new Player("Cameron", 2)); players.add(new Player("Daniel", 2)); System.out.printf("Players: %s%n", players); players.sort(Comparator.comparing(Player::getPoints)); System.out.printf("Sorted Players: %s%n", players); List<Player> winners = getWinners(players); System.out.printf("Winners: %s%n", winners); } public static List<Player> getWinners(List<Player> players) { int maxPoints = Collections.max(players, Comparator.comparing(Player::getPoints)).getPoints(); return players.stream().filter(p -> p.getPoints() == maxPoints).collect(Collectors.toList()); }}假设你的Player类看起来像这样:class Player { private String name; private int points; Player(String name, int points) { this.name = name; this.points = points; } Player(String name) { this.name = name; this.points = 0; } public void updatePoints(int newPoints) { this.points = newPoints; return; } public int getPoints() { return this.points; } public String toString() { return String.format("%s: %d", this.name, this.points); }}输出:Players: [Adam: 1, Bob: 0, Cameron: 2, Daniel: 2]Sorted Players: [Bob: 0, Adam: 1, Cameron: 2, Daniel: 2]Winners: [Cameron: 2, Daniel: 2]
-
守着一只汪
使用以下解决方案怎么样Streams:private static List<Player> getWinners(List<Player> players) { // Use TreeMap with reverse order comparator; the highest point will be the first Supplier<Map<Integer, List<Player>>> mapFactory = () -> new TreeMap<>(Comparator.reverseOrder()); Map<Integer, List<Player>> map = Optional.of(players).orElse(Collections.emptyList()).stream() .collect(Collectors.groupingBy(Player::getPoints, mapFactory, Collectors.toList())); // map.keySet() is sorted desc order all points return map.isEmpty() ? Collections.emptyList() : map.get(map.keySet().iterator().next());}
-
MYYA
您需要首先对玩家数组进行排序。返回与第一项的分数匹配的所有内容。private static ArrayList<Player> getWinners(ArrayList<Player> players) { if (players.isEmpty()) { throw new RuntimeException("players list is empty"); } // sort the array (desending) Comparator<Player> comparator = (Player t, Player t1) -> t1.getPoints() - t.getPoints(); Collections.sort(players, comparator); // filter the array Predicate<Player> prdct = (Player t) -> t.score == players.get(0).getPoints(); List<Player> winners = players.stream().filter(prdct).collect(Collectors.toList()); return new ArrayList<>(winners); }希望这可以帮助!
-
翻阅古今
private static ArrayList<Player> getWinners(ArrayList<Player> players){ ArrayList<Player> l = new ArrayList<Player>(); int maxPoints = -1; //Assuming it is not possible to have negative points for (int i = 0; i < players.size(); i++) { int points = players.get(i).getPoints(); if (points >= maxPoints) { if (points > maxPoints) { l.clear(); //Clear the return list, since a new "best score" was found } maxPoints = points; l.add(players.get(i)); } } return l; }