How recursion works in Stream java8

  • A+

I have a method like this where I'm using recursion with Streams.

  private static List<Member> convertToFlatList(List<Member> memberList)   {     return -> Stream.concat(Stream.of(i), convertToFlatList(i.getChildren()).stream())).collect(Collectors.toList());   } 

Lets say a Member class has a children list of members that is always initialized to an empty list. Here what I'm doing is converting hierarchical list of members to a flat list. I understand that part. What I don't understand is how recursion works here.

In recursion, its terminated when a certain conditions are met. But here I'm not giving any condition for terminating intentionally. So how is the termination part works here?


The recursion will end when memberList will be empty, since at this case an empty List will be returned.

i.e. when i.getChildren() is an empty List, the recursive call convertToFlatList(i.getChildren()) will receive an empty List, so the Stream pipeline won't make another recursive call (since it has no elements to execute flatMap on), and will return an empty List.


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: