Java 8 Stream API Methods

JAVA 8

What is Stream API?

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.

forEach()

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);

filter()

Filters elements based on a predicate (boolean condition).

forEach()

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());

map()

Transforms each element using a function. Useful for data transformation.

sorted()

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());

reduce()

Performs aggregation operations like sum, average, etc.

collect()

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);

Explore the full guide on Java 8 Streams Clink the Link Below

Off-White Arrow