Reputation: 25
I have a list
List<Person> persons =
Arrays.asList(
new Person("Max", 4),
new Person("Peter", 2),
new Person("Pamela", 3),
new Person("David", 9));
I'd like to create Stream and filter the list by a field, something like in java 8:
persons
.stream()
.filter(p -> p.age >= 18)
Upvotes: 2
Views: 1888
Reputation: 2932
Create a Stream
is impossible but you could still use Guava's filter (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);
If you'd like more info about Guava's filter and transform, I got my source from here : https://www.baeldung.com/guava-filter-and-transform-a-collection
Upvotes: 4