Skip to content
Advertisement

Java-Stream – Grouping Lists based on several elements within each List

I have the following data stored as lists:

JavaScript

I need to group it as follows

JavaScript

What I have tried is the following:

JavaScript

But this doesn’t give the desired result.

Advertisement

Answer

Here’s how you can apply nested groupingBy

JavaScript

To collect it in the order encountered, you can use a LinkedHashMap,

JavaScript

Result:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement