I need to write a method that returns a view of a List(java.util.List). The returned view should contain only those elements in the input List that returns true when tested with the isSuccessful() method.
List<Entity> getSuccessfulEntities(List<Entity> entities)
{
//Return a List view of entities, each element of which returns true for isSuccessful(entity)
}
I have looked at Collections and List API but couldn’t find any facility for this. Any out of box thought?
There’s nothing (at least that I know of) in the Java standard library, but you could use
com.google.common.collect.Collections2.filter()andPredicate, which is just defined as:Changes to the unfiltered and filtered
Collectionaffect one another. Sample code:Related: Java: What is the best way to filter a Collection?