java
  • Introduction
  • Patrones de Diseño
    • Builder Design Pattern
  • Java 8
    • Ordenar List
    • Optional
    • Gradle
    • filter
    • Lambda y Stream
  • Java 9
  • Java 10
  • Java 11
  • Java 12
  • Performance
  • Modularidad
  • GraalVM
    • Graalvm
  • JUnit
  • Apache Archiva
    • Apache Archiva
  • Dropbox APi
  • JVM Configuracion
    • JVM Configuration
    • NetBeans.conf
    • Glassfish
  • Data Science in Pure Java
  • Reflection
    • Call Methods at Runtime Using Java Reflection
  • LambdaMetaFactory
Powered by GitBook
On this page

Was this helpful?

  1. Java 8

filter

PreviousGradleNextLambda y Stream

Last updated 6 years ago

Was this helpful?

Filter
Filtering a stream of data is the first natural operation that we would need. Stream interface exposes a filter method that takes in a Predicate (http://javadocs.techempower.com/jdk18/api/java/util/function/Predicate.html ) SAM that allows us to use lambda expression to define the filtering criteria:

List<Person> persons = …
Stream<Person> personsOver18 = persons.stream().filter(p -> p.getAge() > 18);
Now the thing is that often you want to filter a List just like in the example above. Then you can easily convert the list to a stream, filter and collect the results back to a list if necessary.

https://zeroturnaround.com/rebellabs/java-8-explained-applying-lambdas-to-java-collections/
https://www.mkyong.com/java8/java-8-streams-filter-examples/