Java 8 introduced the Stream API to process collections of objects functionally and declaratively. It's not a data structure—it's a view of a collection.
Used for performing an action for each element in the stream. Perfect for iterating with clean and readable code
list.stream().forEach(System.out::println);
Filters elements based on a predicate (boolean condition).
Used for performing an action for each element in the stream. Perfect for iterating with clean and readable code
list.stream().forEach(System.out::println);
list.stream().filter(s -> s.startsWith("A")) .collect(Collectors.toList());
Transforms each element using a function. Useful for data transformation.
Sorts the stream elements. Use a comparator for custom sorting.
list.stream() .sorted() .forEach(System.out::println);
list.stream() .map(String::toUpperCase) .collect(Collectors.toList());
Performs aggregation operations like sum, average, etc.
Converts stream elements into a collection or other data type.
List<String> result = list.stream() .collect(Collectors.toList());
int sum = list.stream() .reduce(0, Integer::sum);