【发布时间】:2019-01-22 10:26:15
【问题描述】:
我想在 Java 11 中对 TreeSet 进行排序。 我尝试使用比较器,但问题是 lambda 表达式不将 args 视为条目。
我想这样做:
SortedSet<Entry<Character, Long>> sortedSet = new TreeSet<>(map.entrySet(),
((o1, o2) -> (int) (o1.getValue() - o2.getValue())));
但问题是 TreeSet 中没有这样的构造函数。所以我尝试了另一个程序:
SortedSet<Entry<Character, Long>> sortedSet = new TreeSet<>(map.entrySet())
.comparator()
.compare(o1,o2)
比较方法需要的参数:
compare(capture of ? super Entry<Character, Long> o1, capture of ? super Entry<Character, Long> o1)
但我不知道我必须传递哪些参数而不是 o1,o2。
【问题讨论】:
-
o1.getValue() AND o2.getValue() ???
-
@nafas o1 和 o2 未在此范围内定义。 compare 方法给出两个参数: compare(capture of ? super Entry
o1, capture of ? super Entry o1)
标签: java sorting generics comparator treeset