void (List<String> strings) { Map<Integer, Map<Character, String>> map = new HashMap<>(); for (String s : strings) { if (map.computeIfAbsent(s.length(), k -> new HashMap<>()).put(s.charAt(0), s) != null) { throw new IllegalStateException("Duplicate key"); } } System.out.println(map); }