gpt4 book ai didi

java - 如何获取列表中的所有玩家的最小值

转载 作者:行者123 更新时间:2023-11-30 01:59:29 24 4
gpt4 key购买 nike

我正在尝试以最少的尝试返回游戏的获胜者。但我不确定如何在平局的情况下执行此操作。

我的获胜者是

try(Scanner scan = new Scanner(new File("result.txt"))){
while(scan.hasNext()){
String[] s = scan.nextLine().split(" ");
players.add(new Players(s[0],s[1],Integer.valueOf(s[2])));
}
Collections.sort(players, Comparator.comparing((players1) -> players1.getAttempts()));
//test// System.out.println(players);
//Collections.sort(players, (a,b)->a.getAttempts().compareTo(b.getAttempts()));


System.out.println("The winner is: "+ players.get(0).getName() +", with "+players.get(0).getAttempts()+ " attempt(s)!");
}
result.close();
}

感谢任何帮助。

最佳答案

首先根据尝试次数对列表进行升序排序

List<Players> sortedList = players.stream().sorted((a,b)->a.getAttempts().compareTo(b.getAttempts())).collect(Collectors.toList())

然后打印所有尝试次数最少的获胜者

sortedList.stream().filter(i->i.getAttempts()==sortedList.stream().findFirst()
.get().getAttempts()).forEach(winner->System.out.println("The winners are "+winner.getvalues));

或者只是流式传输按升序排序的集合对象

players.filter(i->i.getAttempts()==players.get(0).getAttempts()).forEach(winner->System.out.println("The winners are "+winner.getvalues));

关于java - 如何获取列表中的所有玩家的最小值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53348059/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com