Back
I try with a loop like that
// ArrayList touristsfor (Tourist t : tourists) { if (t != null) { t.setId(idForm); } }
// 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
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())
31k questions
32.8k answers
501 comments
693 users