Is it possible to filter out empty Optionals and map over the present ones in a single line with Java streams? -


i find myself writing code this:

return collectionofoptionals.stream()     .filter(optional::ispresent)     .map(optional::get)     .collect(tolist()); 

but there way compress middle 2 lines single operation?

i can this, feels less satisfactory:

return collectionofoptionals.stream()     .flatmap(o -> o.map(stream::of).orelseget(stream::empty))     .collect(tolist()); 

it seems possible in java 9, based on this question.

return collectionofoptionals.stream()     .flatmap(optional::stream)     .collect(tolist()); 

this nicer. have have in java 8.


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -