문제

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.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top