void (List<String> strings) { final Map<Boolean, Map<Character, Set<String>>> nestedMap = new HashMap<>(); nestedMap.put(false, new HashMap<>()); nestedMap.put(true, new HashMap<>()); for (String s : strings) { nestedMap.get(s.length() > 2).computeIfAbsent(s.charAt(0), k -> new HashSet<>()).add(s); } System.out.println(nestedMap); }