为什么guava的Iterables.filter想要谓词中的超类?

exp*_*ert 1 java guava

为什么guava Iterables.filter想在谓词中使用超类?

public static <T> Iterable<T> filter(final Iterable<T> unfiltered, final Predicate<? super T> predicate)
Run Code Online (Sandbox Code Playgroud)

你能告诉我如何过滤自定义类的对象吗?我试过这个

我试过这个

Iterator<TaskSchedule> it = tsp.getAllItems(getCustomerId(), "getId", filter);

com.google.common.collect.Iterables.filter(it, new Predicate<Object>() {
    @Override
    public boolean apply(Object input)
    {
        return false;
    }
});
Run Code Online (Sandbox Code Playgroud)

因以下编译错误而失败.我糊涂了.

错误:(132,44)java:C:\ work\sideprojects\zzzz\SVN\inlineschedule-feature\test\cucumber\java\zzzz\com\zzz\cucumber\DBTestDriver.java:132:找不到符号符号:方法filter(java.util.Iterator,>)location:class com.google.common.collect.Iterables

我正在使用JDK6

Col*_*inD 6

您的编译错误是由于您正在传递一个Iteratorto Iterables.filter,它希望将Iterable其作为其第一个参数.你想要的Iterators.filter.

这与你的实际问题无关,但是因为你问:Iterables.filter采取一个Predicate<? super T>例如,你可以使用a Predicate<Object>来过滤a List<String>.如果谓词可以处理您要过滤的类型的某些超类型,则可以使用该谓词.