void (List<String> strings) { Map<Integer, Set<String>> map = new HashMap<>(); for (String string : strings) { if (string != null) { map.computeIfAbsent(string.length(), k -> new HashSet<>()).add(string); } } System.out.println(map); }