Java Streams API
Java Streams API (Java 8+) provides a functional way to process collections of data. Streams let you filter, map, sort, and reduce data with clean, readable code — replacing verbose loops with elegant operations. This is a foundational concept in enterprise application development that professional developers rely on daily. The explanations below are written to be beginner-friendly while covering the depth and nuance that comes from real-world Java experience. Take your time with each section and practice the examples
45 min•By Priygop Team•Last updated: Feb 2026
Streams API
Example
import java.util.*;
import java.util.stream.*;
public class StreamsDemo {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
// Filter: keep only even numbers
List<Integer> evens = numbers.stream()
.filter(n -> n % 2 == 0)
.collect(Collectors.toList());
System.out.println("Evens: " + evens);
// Map: transform each element
List<Integer> squares = numbers.stream()
.map(n -> n * n)
.collect(Collectors.toList());
System.out.println("Squares: " + squares);
// Reduce: combine into single result
int sum = numbers.stream()
.reduce(0, Integer::sum);
System.out.println("Sum: " + sum);
// Chaining operations
double avgOfSquaredEvens = numbers.stream()
.filter(n -> n % 2 == 0)
.map(n -> n * n)
.mapToInt(Integer::intValue)
.average()
.orElse(0);
System.out.println("Avg of squared evens: " + avgOfSquaredEvens);
// String streams
List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "Anna", "Alex");
List<String> aNames = names.stream()
.filter(name -> name.startsWith("A"))
.sorted()
.map(String::toUpperCase)
.collect(Collectors.toList());
System.out.println("A-names (sorted, uppercase): " + aNames);
// Count, min, max
System.out.println("\nCount > 5: " + numbers.stream().filter(n -> n > 5).count());
System.out.println("Max: " + numbers.stream().max(Integer::compare).orElse(0));
System.out.println("Min: " + numbers.stream().min(Integer::compare).orElse(0));
}
}Try It Yourself: Student Analytics
Try It Yourself: Student AnalyticsJava
Java Editor
✓ ValidTab = 2 spaces
Java|48 lines|2160 chars|✓ Valid syntax
UTF-8