Besides the excellent solutions offered here, I would like to propose another solution.
I'm not sure you can add dependencies, but if you can, you can add https://code.google.com/p/guava-libraries/ as a dependency. This library adds support for many basic functional operations in Java and can make it easier to work and work with collections.
In the code, I replaced the List type with T, since I donβt know what your list is printed on.
This problem can be solved with guava as follows:
List<T> filteredList = new Arraylist<>(filter(list, not(XXX_EQUAL_TO_AAA)));
And somewhere else you define XXX_EQUAL_TO_AAA as:
public static final Predicate<T> XXX_EQUAL_TO_AAA = new Predicate<T>() { @Override public boolean apply(T input) { return input.getXXX().equalsIgnoreCase("AAA"); } }
However, this is probably too large in your situation. This is what becomes more powerful the more you work with collections.
Ohw, you also need these static imports:
import static com.google.common.base.Predicates.not; import static com.google.common.collect.Collections2.filter;
Bart Enkelaar Jun 24 '13 at 16:09 2013-06-24 16:09
source share