简体   繁体   中英

Check if a List is not empty and not null simultaneously in java 8

I have a

List<Object> coins = exchange.getCoins();

I want to simplify the below code which is in java below 8.

if (coins != null && !coins.isEmpty()) {
    //perform logic
}

The thing is that after the check I have lot of operation to perform, so I want to have a stream approach.

Just try use:

Stream.ofNullable(coins).filter(...).map(...).collect(..)
Optional.ofNullable(coins).ifPresent(e-> e.stream() .filter(x-> x.length()>0) .map(...) .collect(...))

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