Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Java by (10.2k points)

I try with a loop like that

// ArrayList tourists

for (Tourist t : tourists) {

    if (t != null) {     

        t.setId(idForm); 

    }   

}

But it isn't nice. Can anyone suggest me a better solution?

Some useful benchmarks to make better decision:

While loop, For loop and Iterator Performance Test

1 Answer

0 votes
by (46k points)

If you prefer immutable data objects, or if you just dont want to be destructive to the input list, you can use Guava's predicates.

ImmutableList.copyOf(Iterables.filter(tourists, Predicates.notNull())

Related questions

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.6k users

Browse Categories

...