Map<Integer,List<ItemTypeA>> list = data.stream().collect(groupingBy(ItemTypeA::getId));
I have a function that converts ItemTypeA to ItemTypeB .
public ItemTypeB convert (ItemTypeA);
How can I use that after groupingBy
here so that that the end result is as shown below.
Map<Integer,List<ItemTypeB>> map = data.stream().collect(groupingBy(ItemTypeA::getId),
How to invoke function to convert ItemTypeA
to ItemTypeB
?;
groupingBy. Returns a Collector implementing a cascaded "group by" operation on input elements of type T , grouping elements according to a classification function, and then performing a reduction operation on the values associated with a given key using the specified downstream Collector .
The groupingBy() method of Collectors class in Java are used for grouping objects by some property and storing results in a Map instance.
Given a list (ArrayList or LinkedList), convert it into a set (HashSet or TreeSet) of strings in Java. We simply create an list. We traverse the given set and one by one add elements to the list. // Set to array using addAll() method.
You can use Collectors.mapping
:
Map<Integer,List<ItemTypeB>> output =
data.stream()
.collect(Collectors.groupingBy(ItemTypeA::getId,
Collectors.mapping(a->convert(a),
Collectors.toList())));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With