A Stream in Java is a sequence of elements that can be processed in a functional style. Streams are part of the Java 8 Stream API, allowing developers to perform operations on collections in a more expressive and declarative way.
Filtering is one of the most common operations performed on streams. It allows you to selectively process elements based on specific criteria using the filter
method.
filter
Method WorksStream<T> filter(Predicate<? super T> predicate)
Predicate
(a functional interface that returns a boolean) that defines the criteria for filtering elements.Let’s start with a simple example where we filter a list of integers to find only the even numbers.
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class FilterStreamExample {
public static void main(String[] args) {
List numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
// Filtering even numbers using a stream
List evenNumbers = numbers.stream()
.filter(n -> n % 2 == 0)
.collect(Collectors.toList());
System.out.println("Even numbers: " + evenNumbers);
}
}
numbers.stream()
creates a sequential stream from the list of integers.filter
method uses a lambda expression n -> n % 2 == 0
to keep only even numbers.collect(Collectors.toList())
method gathers the filtered results into a new list.Let’s consider an example where we filter a list of custom objects. Assume we have a Person
class and we want to filter out people older than 18.
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return name + " (" + age + ")";
}
}
public class FilterPersonExample {
public static void main(String[] args) {
List people = Arrays.asList(
new Person("Alice", 22),
new Person("Bob", 17),
new Person("Charlie", 19),
new Person("David", 15)
);
// Filtering people older than 18
List adults = people.stream()
.filter(person -> person.getAge() > 18)
.collect(Collectors.toList());
System.out.println("Adults: " + adults);
}
}
Person
class has a name
and age
.people.stream()
creates a stream from the list of Person
objects.person -> person.getAge() > 18
filters out people under 19.Streams support method chaining, allowing you to perform multiple operations in a single pipeline. Here’s an example that combines filtering with mapping.
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class FilterAndMapExample {
public static void main(String[] args) {
List people = Arrays.asList(
new Person("Alice", 22),
new Person("Bob", 17),
new Person("Charlie", 19),
new Person("David", 15)
);
// Filtering adults and collecting their names
List adultNames = people.stream()
.filter(person -> person.getAge() > 18)
.map(Person::toString) // Convert Person to String
.collect(Collectors.toList());
System.out.println("Adult Names: " + adultNames);
}
}
map
method converts each Person
object to its string representation.