是否可以在 java 中创建流和过滤器 7

Is it possible to Create stream and filter in java 7

我有一个列表

List<Person> persons =
    Arrays.asList(
        new Person("Max", 4),
        new Person("Peter", 2),
        new Person("Pamela", 3),
        new Person("David", 9));

我想创建 Stream 并按字段过滤列表,类似于 java 8:

persons
    .stream()
    .filter(p -> p.age >= 18)

创建 Stream 是不可能的,但您仍然可以使用 Guava 的过滤器 (Collections2.filter() or Iterables.filter() depending on what you'll do with the result) combined with Guava's Predicate

Predicate<Person> predicate = new Predicate<Person>() {
        @Override
        public boolean apply(Person person) {
            return person.age >= 18;
        }
    };
Collection<Person> result = Collections2.filter(persons, predicate);

如果您想了解有关 Guava 的过滤器和转换的更多信息,我从这里获取源代码:https://www.baeldung.com/guava-filter-and-transform-a-collection