Map.Entry.comparingByValue() cannot serve as the comparator.
This is a comparator for Stream sorted(Comparator super T> comparator):
Stream>> sortedStream = index.entrySet().stream()
.sorted(new Comparator>>() {
@Override
public int compare(Entry> o1, Entry> o2) {
return o1.getValue().size()-o2.getValue().size();
}
});