Skip to content
Advertisement

How to combine two lists from the same relation in Java-8

I have below entities Manager and Colleague

Manager Entity

JavaScript

Colleague Entity

JavaScript

Above relation can be represented in JSON as

JavaScript

I am retrieving the result on managerReposiotry.findAll() as

JavaScript

I want to create a super list of all names from Manager and Colleague

What I am currently doing is

JavaScript

Is there any other way in Java-8 to improve the above code?

Thank you !!

Advertisement

Answer

You can use flatMap to flatten all Colleague of Manager then map Colleague name only and collect as list.

JavaScript

But the better way is directly fetched from the database if all colleagues have manager.

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