Skip to content
Advertisement

how do I convert this piece of code to stream using java stream api

I am trying to convert below code into stream. It’s not that difficult but I am not sure how to handle null values in stream. I did checked out Optinal.ofNullable method but quite not sure if I have to use it 2-3 times to get the correct stream code. I can use the below code for now, but I wish to learn doing it in stream. Please help me learn.

JavaScript

Advertisement

Answer

Seems not related to Stream API, just use Map.getOrDefault to get rid of null return.

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