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;

closed as too broad by yuri, Mathias Ettinger, Graipher, Toby Speight, esote May 14 at 12:00

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ Questions about how to do things are better suited in stackoverflow.com . \$\endgroup\$ – TorbenPutkonen May 14 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())));

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