// "Replace Stream API chain with loop" "true" import java.util.*; import java.util.stream.Collectors; public class Main { private static TreeMap> getMap(List strings) { return strings.stream().collect( Collectors.groupingBy(String::length, () -> new TreeMap<>(Comparator.reverseOrder()), Collectors.toCollection(LinkedHashSet::new))); } public static void main(String[] args) { System.out.println(getMap(Arrays.asList("a", "bbb", "cccc", "dddd", "ee", "e"))); } }