I have a class :
public class A {
String type;
List<String> names;
}
Given a list of objects of this class (List<A>
) how can I create a Map<String, A'>
in which :
- the key is the
type
String
. - the value
A'
is a new object of typeA
, formed by merging allA
instances of the input list having the sametype
- thenames
List
of that new object will contain all the names of the mergedA
instances'names
lists.
I tried using toMap
and groupingBy
from Java Collectors framework, but toMap
throws an IllegalStateException
since there are duplicate keys, whereas groupingBy
creates a map of the form Map<String, List<A>>
, whereas I want a Map<String, A>
.