Skip to content
Advertisement

Creating a map from nested lists

Suppose there are 3 classes:

JavaScript

Suppose there is a List of Level1 objects called initial state

JavaScript

I want to create a map from initialList where:

JavaScript

I am able to achieve this using for loops, but I want to achieve this in a more elegant way using Java 8 features (streams and the functions). I tried using Collectors.toMap() also tried grouping but I am unable to get the desired map.

Advertisement

Answer

By corresponding to idLevel1 I made the assumption that you wanted a list of all idlevel3 that were in the chain for the a particular idLevel1

So there could be a list of level3 ids for some level1 id and a different list of level3 ids for a different level1 id.

Based on that, this is what I came up with.

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