-1
\$\begingroup\$

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<>();
                strSet.add(pair.getRight());
                result.merge(pair.getLeft(), strSet, new BiFunction<Set<String>, Set<String>, Set<String>>() {
                    @Override
                    public Set<String> apply(Set<String> current, Set<String> additional) {
                        if (current == null) {
                            return additional;
                        }
                        current.addAll(additional);
                        return current;
                    }
                });
            }
return result;
\$\endgroup\$

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
1
\$\begingroup\$

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

return resultList.stream()
    .collect(Collectors.groupingBy(Pair::getLeft,
        Collectors.mapping(Pair::getRight, Collectors.toSet())));
\$\endgroup\$

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