1

Is it possible to pass an argument to a lambdaj Predicate?

public static Matcher<SomeObject> isSpecialObject = new Predicate<SomeObject>() {
        public boolean apply(SomeObject specialObj) {
            return LIST_OF_SPECIAL_IDS.contains(specialObj.getTypeId());
        }
    };

I would like to alter the above predicate so I can pass in a list, rather than use the static list LIST_OF_SPECIAL_IDS. Is that possible?

Thanks.

HellishHeat
  • 2,280
  • 4
  • 31
  • 37

1 Answers1

4

I suspect you want something like:

public static Matcher<SomeObject> createPredicate(final List<String> ids) {
    return new Predicate<SomeObject>() {
        public boolean apply(SomeObject specialObj) {
            return ids.contains(specialObj.getTypeId());
        }
    };
}

You've got to make it a method rather than just a field, as otherwise you've got nowhere to pass the list. The parameter has to be final so that you can use it within the anonymous inner class.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194