Simple conversion of one type to another.

Is there a better way with streams :

    Map<String, Set<String>> result = new HashMap<>();
            for (Pair<String, String> pair : resultList) {
                Set<String> strSet = new HashSet<>();
                result.merge(pair.getLeft(), strSet, new BiFunction<Set<String>, Set<String>, Set<String>>() {
                    public Set<String> apply(Set<String> current, Set<String> additional) {
                        if (current == null) {
                            return additional;
                        return current;
return result;
  • \$\begingroup\$ Questions about how to do things are better suited in stackoverflow.com . \$\endgroup\$ – TorbenPutkonen May 14 '19 at 5:53

Sure. Use Collectors.groupingBy to build the Map<String, Set<String>> for you.

return resultList.stream()
        Collectors.mapping(Pair::getRight, Collectors.toSet())));
| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.