获取列表中重复次数最多的值并按降序排序的最简单方法是什么... 例如:
List<String> list = new ArrayList<>(List.of("Renault","BMW","Renault","Renault","Toyota","Rexon","BMW","Opel","Rexon","Rexon"));
` “renault”和“rexon”是最重复的,如果按字母顺序降序排序,我想得到 rexon。
最佳答案
我认为最具可读性和优雅的方式之一是使用 Streams API
strings.stream()
.collect(Collectors.groupingBy(x -> x, Collectors.counting()))
.entrySet().stream()
.max(Comparator.comparingLong((ToLongFunction<Map.Entry<String, Long>>) Map.Entry::getValue).thenComparing(Map.Entry::getKey))
.map(Map.Entry::getKey)
.ifPresent(System.out::println);
关于java - 按字母顺序排序后,如何获取列表中最重复值的计数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66215714/