To remove elements with all null values from an ArrayList or String array

Asked 2 years ago, Updated 2 years ago, 107 views

I'd like to remove all elements that have null values from the ArrayList or String array in Java For me,

for (Tourist t : tourists) {
    if (t != null) {     
        t.setId(idForm); 
    }   
}

I tried turning around the loop like this, but it didn't work out as I wanted. Is there a good way?

arraylist for-loop java loops performance

2022-09-22 22:10

1 Answers

tourists.removeAll(Collections.singleton(null));

Do it like this.


2022-09-22 22:10

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.