2

I have a List object which holds 10 elements, i have to do conditional check and remove some of the elements from the list. I have done as below, but it is not working as expected, any inputs?

List<MyDTO> myList = new ArrayList<MyDTO>(); //myist.size () is 10

I want to check as below:

for(MyDTO res : myList){
    if(res.getResult().equals("cancel")){
     myList.remove()
  }
}

As shown in above code, if res.getResult() is "cancel" i want to remove that particular object from the list(myList). Is it the correct way to remove an element completely from list based on conditional check?

scrit
  • 241
  • 1
  • 5
  • 12

4 Answers4

3

Simply use removeIf on your list, for example if you had list of Integers ranging from 1 to 10 and you wanted to remove Integers larger than 4, you would do:

yourListHere.removeIf(x -> x > 4);

Resulting list would contain: 1, 2, 3 and 4

Read here about removeIf:

https://docs.oracle.com/javase/8/docs/api/java/util/Collection.html#removeIf-java.util.function.Predicate-

BONUS

Additional resources if you are unfamiliar with Java 8 features:

Lambdas - https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html

Functional Interfaces - https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html

whatamidoingwithmylife
  • 1,119
  • 1
  • 16
  • 35
0

As an alternative to removeIf you can use the Java stream solution:

List<MyDTO> newList = myList.stream()
                            .filter(s -> !s.getResult().equals("cancel"))
                            .collect(Collectors.toList());

This might be more convenient when the length of the input list and the number of elements to remove is relatively large.

Valentin Ruano
  • 2,726
  • 19
  • 29
0

Use removeIf:

default boolean removeIf(Predicate<? super E> filter)

Removes all of the elements of this collection that satisfy the given predicate. Errors or runtime exceptions thrown during iteration or by the predicate are relayed to the caller.

In your example:

myList.removeIf(res -> res.getResult().equals("cancel"));
Max Vollmer
  • 8,412
  • 9
  • 28
  • 43
0

If use Java 8:

myList.removeIf(x -> x.getResult().equals("cancel"));

If older (6,7):

public class MyDTO {

    private String result;
    (...)

    public MyDTO(String result) {
        this.result = result;
    }

    public String getResult() {
        return result;
    }

    (...)

    @Override
    public int hashCode() {
        return 31;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final MyDTO other = (MyDTO) obj;

        return !((this.result == null) ? (other.result != null) : !this.result.equals(other.result));
    }

    (...)

}

then:

MyDTO toRemove = new MyDTO("cancel");

while(myList.contains(toRemove))
    myList.remove(toRemove);

or:

List<MyDTO> toRemove = new ArrayList<MyDTO>(); 

for(MyDTO res : myList){
    if(res.getResult().equals("cancel")){
     toRemove.add(res);
  }
}
myList.removeAll(toRemove);

Without overriding the equals method:

for(int i = 0; i < myList.size() ; i++)
    if(myList.get(i).getResult().equals("cancel"))
       myList.remove(i);

or:

Iterator<MyDTO> i = myList.iterator();

while (i.hasNext())
    if(i.next().getResult().equals("cancel"))
         i.remove();