0

I have a List<StructureA>.

Now this structure comprises of a Type: typeA & typeB. Type here is an Enum.

StructureA comprises of fields : countryCode, Type, timeZone. StructureB comprises of fields: countryCode, timeZone

I want to convert this List into a Map<Type, Set<StructureB>>. Is this possible using streams?

4
  • What is the relationship between StructureA and Type ? Commented May 13, 2019 at 10:40
  • What is the data type of Type? Commented May 13, 2019 at 10:41
  • StructureA has multiple attributes e.g. timeZone, countryCode, type etc. Type here is an enum, which can either be typeA or typeB. Commented May 13, 2019 at 10:41
  • Related/duplicate stackoverflow.com/questions/56110564/… Commented May 13, 2019 at 11:13

1 Answer 1

2

use grouping by collector

Map<Type, Set<StructureB>> collect = list.stream()
            .collect(Collectors.groupingBy(StructureA::getType,
                Collectors.mapping(a -> new StructureB(a), Collectors.toSet())));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.