我有一个名为 map 的 HashMap,它将字符存储为键,将整数存储为值,然后使用以下代码将其存储到名为 entries 的 ArrayList 中:
Set<Entry<Character, Integer>> s = map.entrySet();
ArrayList<Entry<Character, Integer>> entries = new ArrayList<>(s);
现在我尝试根据整数值而不是键对这些条目进行排序。我尝试使用 lambda 表达式来实现 Comparator 接口(interface),但它不起作用。这是我的代码:
Collections.sort(sortedEntries, (sortedEntries.get(0), sortedEntries.get(1)) -> {
sortedEntries.get(0).getValue().compareTo(sortedEntries.get(1).getValue())
});
这些是我得到的错误:
Multiple markers at this line
Syntax error, insert ")" to complete Expression
The method sort(List, Comparator) in the type Collections is not applicable for the arguments (ArrayList>, Map.Entry, Map.Entry)
Syntax error on token "->", ; expected
最佳答案
您可以这样按值对列表进行排序:
list.sort(Comparator.comparing(Entry::getValue));
关于java - 如何对类型为 <Entry<Character, Integer>> 的 ArrayList 进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40243346/