Java Programs›Java 8 Streams›Filter Even Numbers Using Streams

Filter Even Numbers Using Streams in Java

beginner·  Java 8 Streams  ·  Streams

Problem

A stream is a pipeline over a collection's elements; filter is an intermediate operation that passes along only the elements matching a condition.

Given a list of integers, build a new list containing only the even numbers, leaving the original list unchanged.

Input
[14, 3, 8, 21, 6, 17, 10]
Output
[14, 8, 6, 10]

Java Program

Java
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class FilterEvenNumbers { public static void main(String[] args) { List<Integer> numbers = Arrays.asList(14, 3, 8, 21, 6, 17, 10); List<Integer> evens = numbers.stream() .filter(n -> n % 2 == 0) // keep only the values that pass the test .collect(Collectors.toList()); System.out.println("All numbers: " + numbers); System.out.println("Even numbers: " + evens); } }

Output

All numbers: [14, 3, 8, 21, 6, 17, 10] Even numbers: [14, 8, 6, 10]

Core Logic

A stream pipeline reads like a description of the result: take the numbers, keep the even ones, collect them into a list.

How It Works
  1. 1numbers.stream() opens a stream over the list without modifying it.
  2. 2filter(n -> n % 2 == 0) passes along only values whose remainder by 2 is zero.
  3. 3collect(Collectors.toList()) is the terminal operation that runs the pipeline and gathers the results.
  4. 4Printing both lists shows the original is untouched.
3, 21 and 17 fail the test and are dropped; 14, 8, 6 and 10 pass and keep their original order.
💡

Key Point: Nothing happens until a terminal operation such as collect runs — intermediate steps like filter are lazy and only describe the work.

Complexity
Time Complexity: O(n)Space Complexity: O(k)

Why: Every element is tested once, and the new list holds the k elements that passed.

Key Concepts

stream()filter()lambda expressionCollectors.toList()

Approach 2: Without Streams (for-each Loop)

Java
import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class FilterEvenNumbersLoop { public static void main(String[] args) { List<Integer> numbers = Arrays.asList(14, 3, 8, 21, 6, 17, 10); List<Integer> evens = new ArrayList<>(); for (int n : numbers) { if (n % 2 == 0) { evens.add(n); // the same test, written as an if inside a loop } } System.out.println("Even numbers: " + evens); } }

Output

Even numbers: [14, 8, 6, 10]

Core Logic

The same filter written the pre-Java 8 way makes it clear what the stream version does for you.

How It Works
  1. 1An empty ArrayList is created to hold the results.
  2. 2A for-each loop visits every number.
  3. 3An if applies the same n % 2 == 0 test and adds passing values.
The loop adds 14, skips 3, adds 8, and so on, ending with [14, 8, 6, 10].
💡

Key Point: Both versions do identical work; streams win on readability when several steps are chained, such as filter then map then sort.

Complexity
Time Complexity: O(n)Space Complexity: O(k)

Why: One pass over n elements, with k of them copied into the result.

Key Concepts

for-each loopArrayListif statement

Interview Questions

Can a stream be reused after collect()?

No. A stream can be consumed only once; calling another terminal operation throws IllegalStateException. Create a new stream from the source instead.

What's the difference between filter and map?

filter decides which elements continue, without changing them. map transforms every element into something else.

Learn the Concept

Related Programs