public static void main(String[] args) { Map<Integer, HashSet<String>> withSet = Stream.of("jon", "andrew", "harvey", "bil") .collect(Collectors.groupingBy(String::length, HashSet::new)); out.println(withSet); }