Java
Stream API
Process collections using the Stream API.
By EZ4Code Team
streamcollectionfunctional
Code
import java.util.*;
import java.util.stream.*;
List<Integer> numbers = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
// Filter even numbers and sum squares
int sumOfSquares = numbers.stream()
.filter(n -> n % 2 == 0)
.mapToInt(n -> n * n)
.sum();
// Grouping
Map<Boolean, List<Integer>> partitioned = numbers.stream()
.collect(Collectors.partitioningBy(n -> n % 2 == 0));
// String join
String joined = numbers.stream()
.map(String::valueOf)
.collect(Collectors.joining(", ", "[", "]"));Explanation
The Stream API provides a declarative way to process collections, supporting filter, map, reduce, etc.
More Java Snippets
String Operations and StringBuilder
Manipulate strings with split, join, substring, and StringBuilder in Java.
Lambda Expressions
Simplify code with Lambda expressions.
Optional
Handle null values elegantly.
Collection Operations
Common operations on List, Set, and Map.
Exception Handling
try-catch-finally and custom exceptions.
File IO
Read and write file operations.