Take the 2-minute tour ×

Say I have a collection of foodGroupIds and a collection of food. Using commons-collections, how can I filter out the food which does not belong to any of the food groups?

I might be on the right track, but can't figure out what predicate to use. Maybe have to create my own?

Collection<Long> ids = collect(

Collection<Food> food = getAllFood();
filter(food, transformedPredicate(

In C# this would be something like this:

var ids = FindGoodFoodGroups().Select(x => x.Id);
var goodFood = FindAllFood().Select(x => ids.Contains(x.FoodGroupId));

I want all objects whose property P of type T is equal to any T in a collection C with objects of type T.

share|improve this question
add comment

1 Answer

I think you could use Collection.removeAll(Collection).

Simple examples:

    Map food = new HashMap();
    food.put("appleKey", "apple");
    food.put("orangeKey", "orange");
    food.put("tomatoKey", "tomato");

    Set unmatchedKeys = new HashSet(food.keySet());

    Collection unmatchedValues = new ArrayList(food.values());
    unmatchedValues.removeAll(Arrays.asList("apple", "tomato"));

Output is:

[appleKey, tomatoKey]
share|improve this answer
Hm. With strings it would be more like if you had a list of arbitrary numbers and then wanted to remove those strings which does not have a length equal to any of those numbers. –  Svish Nov 21 '11 at 14:16
add comment

Your Answer


