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

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