Geo*_*hov 2 java sorting collections dictionary comparator
我有一个HashMap:
private Map<String,Integer> matchesWonByTeam= new HashMap<String,Integer>();
Run Code Online (Sandbox Code Playgroud)
使用集合和比较器的最简单和最简单的方法是什么?
这里有一些Java 8给你.
final Comparator<Map.Entry<String, Integer>> byMatchesWon =
Comparator.comparing(Map.Entry::getValue, Comparator.reverseOrder());
final Comparator<Map.Entry<String, Integer>> byTeamName =
Comparator.comparing(Map.Entry::getKey);
final List<String> hasToBeReturned = matchesWonByTeam
.entrySet().stream()
.sorted(byMatchesWon.thenComparing(byTeamName))
.map(Map.Entry::getKey)
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
注意 - 内联比较器在这里不起作用,编译器将无法推断出正确的实现.