按成员过滤对象

Filter object by its members

我正在尝试过滤 Guava 中的对象。例如,我有一个 class 团队,并且希望获得所有排名低于 5 的团队。

    Iterable<Team> test = Iterables.filter(teams, new Predicate<Team>(){  
        public boolean apply(Team p) {  
            return p.getPosition() <= 5;  
        }  
    });  

我收到 2 个错误,Predicate 无法解析为类型并且 Iterables 类型中的方法过滤器(Iterable,Predicate)不适用于参数(List <'Team'>,new Predicate <'Team'>(){}).

我能够过滤 Integer 类型的 Iterable。

    Iterable<Integer> t6 = Iterables.filter(set1, Range.open(0, 3));

如何在 Guava 中根据对象的成员过滤对象?我想在我的 android 项目中使用这个库,并且有很多过滤条件。它可以用于 class 对象还是仅用于简单数据类型?

您需要一个 final 变量,例如本例中的 range

这是外参过滤的方式,Predicate是一个内参class。

final Range range = new IntRange(0, 3);

Iterable<Team> test = Iterables.filter(teams, new Predicate<Team>() {
    public boolean apply(Team p) {
        return range.containsInteger(p.getPosition());
    }
});