MirZaffer
MirZaffer

Reputation: 1

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.

Upvotes: 0

Views: 703

Answers (2)

Aman Shivhare
Aman Shivhare

Reputation: 76

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

Upvotes: 1

Łukasz Olszewski
Łukasz Olszewski

Reputation: 915

Just try use:

Stream.ofNullable(coins).filter(...).map(...).collect(..)

Upvotes: 1

Related Questions