gpt4 book ai didi

java - Google guava iterables.filter,与泛型相关的类型错误?

转载 作者:行者123 更新时间:2023-11-29 09:00:58 24 4
gpt4 key购买 nike

我有一个自定义谓词,其签名如下:

public class ELPredicate<T> implements Predicate<T> 

我正尝试在类似于此的函数中过滤它们:

public List<ComponentDefinition> filterComponents(String expression, List<ComponentDefinition> definitions) {

if (definitions == null || definitions.size() == 0) {
return new ArrayList<ComponentDefinition>();
}

ELPredicate<ComponentDefinition> predicate = new ELPredicate<>(expression);
return Lists.newArrayList(Iterables.filter(definitions, predicate));
}

但是我得到这个错误:

The method filter(Iterable<T>, Predicate<? super T>) in the type Iterables is not applicable for the arguments (List<ComponentDefinition>, ELPredicate<ComponentDefinition>)

据我所知(我认为):

  • 列表是可迭代的,
  • <ComponentDefinition><? super ComponentDefinition>
  • 我在单元测试中这样做没有任何问题。

这是怎么回事?

最佳答案

事实证明,我的问题是访问问题而不是类型问题。我试图过滤一个私有(private)内部类(用于测试目的),而 java 显然不喜欢那样。我将其公开,一切都按预期进行。

关于java - Google guava iterables.filter,与泛型相关的类型错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17457922/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com