I need to sort List<String> by the number of occurrences in my list (DESC), and to remove repetitions from there. I wrote this working algorithm in 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());
}
but it doesn't work for Kotlin: kotlin code
because I get the following exception:
Type inference failed. Expected type mismatch:
required:
Collector<in String!, Any!, Long!>!
found:
Collector<String!, *, Long!>!
and can't figure out how to resolve it. maybe can you tell me?