Search code examples
javacollectionsapache-commons

CollectionsUtils filter collections of objects using an object element collection


I have a Collection of HOUSE objects and each HOUSE object has elements - ID, NAME. I get a another collection of IDs from another source. I want to remove the HOUSE objects with these IDs from the Original Collection of HOUSE objects. I am trying something like this..but I can not pass exclusionIDsList to inner class Predicate. Any alternate ideas?

List<HOUSE> originalHouses = ...//List of houses
List<ID> exclusionIDsList = ...//List of IDs to exclude

CollectionUtils.filter(originalHouses, new Predicate() {
    @Override
    public boolean evaluate(Object arg0) {
        if(exclusionIDsList.contains(((HOUSE)arg0).getID())
            return false;
        else return true;
    }
});

Solution

  • Declare exclusionIDsList final:

    final List<ID> exclusionIDsList = ...