简体   繁体   中英

Using streams return null if value is false else return true

I have the below code

 headers.stream().map(MyHeader::getIsCancelled).iterator(),

getIsCancelled returns boolean true/false.

I want to modify the above code to return null if the value is false, else return true. Should I use filter to filter for true value and automatically I will get true or null value?

By using filter , you can eliminate the false values instead of replacing them by null .

If you want to replace the false values by null , you should still use map :

headers.stream().map(h -> h.getIsCancelled() ? true : null).iterator()

I believe that should work, but if not, you can replace true with Boolean.TRUE .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM