Iterables.filter和Collections2.filter是否已执行?

时间:2014-11-18 10:19:15

标签: java collections filter guava

我尝试过滤AddReport

类型的所有元素

但是当在Predicate体中放置一个断点时,我看到那里没有执行代码。

懒惰吗?我看到各种各样的子类。

不只是AddReport

    Iterable<BaseClass> filtered =
            Iterables.filter(myList,
                    new Predicate<BaseClass>() {
                        @Override
                        public boolean apply(@Nullable BaseClass input) {
                            AddReport addReport = (AddReport) input;
                            return ((addReport != null) && addReport.description.equals(messageBody));
                        }
                    });

    Collection<BaseClass> filtered2
            = Collections2.filter(myList,
            new Predicate<BaseClass>() {
                @Override
                public boolean apply(@Nullable BaseClass input) {
                    AddReport addReport = (AddReport) input;
                    return ((addReport != null) && addReport.description.equals(messageBody));
                }
            });

1 个答案:

答案 0 :(得分:0)

听起来我想你想要的是首先将你的迭代过滤到只有AddReport的实例,你可以这样做:

Iterable<AddReport> addReports = Iterables.filter(myList, AddReport.class);