P28 { public static List<List<String>> lfsort(List<List<String>> list) { Map<Integer, Integer> freqs = new HashMap<>(); list.stream().map(List::size).forEach(l -> freqs.put(l, freqs.compute(l, (k, v) -> v == null ? 1 : v + 1))); return list .stream() .sorted((xs1, xs2) -> freqs.get(xs1.size()) - freqs.get(xs2.size())) .collect(toList()); }  static List<List<T>> lsort(List<List<T>> list); static List<List<String>> lfsort(List<List<String>> list); static List<List<String>> lfsort1(List<List<String>> list);  }
@Test public void shouldSortByLengthFrequency() throws Exception { List<List<String>> input = Arrays.asList(Arrays.asList("a", "b", "c"), Arrays.asList("d", "e"), Arrays.asList("f", "g", "h"), Arrays.asList("d", "e"), Arrays.asList("i", "j", "k", "l"), Arrays.asList("m", "n"), Arrays.asList("o")); List<List<String>> result = P28.lfsort(input); assertThat(result, is(equalTo(Arrays.asList(Arrays.asList("i", "j", "k", "l"), Arrays.asList("o"), Arrays.asList("a", "b", "c"), Arrays.asList("f", "g", "h"), Arrays.asList("d", "e"), Arrays.asList("d", "e"), Arrays.asList("m", "n"))))); }