【发布时间】:2021-10-11 04:40:36
【问题描述】:
我需要按列表中出现的次数 (DESC) 对 List<String> 进行排序,并从那里删除重复项。我用java写了这个工作算法:
private List<String> sortByFrequency(List<String> sequence) {
return
sequence.stream()
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
.entrySet().stream()
.sorted(Map.Entry.<String, Long>comparingByValue(Comparator.reverseOrder())
.thenComparing(Map.Entry.comparingByKey()))
.map(Map.Entry::getKey)
.collect(Collectors.toList());
}
但它不适用于 Kotlin: kotlin code
因为我得到以下异常:
Type inference failed. Expected type mismatch:
required:
Collector<in String!, Any!, Long!>!
found:
Collector<String!, *, Long!>!
并且无法弄清楚如何解决它。可以告诉我吗?
【问题讨论】:
-
如果我省略了收集器的显式输入,它对我有用。
标签: java algorithm sorting kotlin frequency