Home Java 8 - Stream Operations
Post
Cancel

Java 8 - Stream Operations

Java 8 - Stream Operations

Streams allow you to perform aggregate operations on collections of elements using functional-style operations. Streams support two types of operations: intermediate and terminal. Intermediate operations transform a stream into another stream, while terminal operations produce a result or a side-effect.

Intermediate Operations

filter()

The filter() operation returns a new stream that contains only the elements that satisfy a given predicate.

1
2
3
4
5
List<String> names = Arrays.asList("John", "Mary", "Alice", "Bob", "Mike");
List<String> filteredNames = names.stream()
                                  .filter(name -> name.startsWith("M"))
                                  .collect(Collectors.toList());

map()

The map() operation returns a new stream that contains the results of applying a given function to each element in the original stream.

1
2
3
4
5
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> squaredNumbers = numbers.stream()
                                       .map(n -> n * n)
                                       .collect(Collectors.toList());

flatMap()

The flatMap() operation returns a new stream by applying a function to each element in the original stream, which returns a stream of elements. The resulting streams are then concatenated into a single stream.

1
2
3
4
5
6
7
8
List<List<Integer>> nestedNumbers = Arrays.asList(
    Arrays.asList(1, 2, 3),
    Arrays.asList(4, 5),
    Arrays.asList(6, 7, 8, 9)
);
List<Integer> flatNumbers = nestedNumbers.stream()
                                          .flatMap(Collection::stream)
                                          .collect(Collectors.toList());

distinct()

The distinct() operation returns a new stream that contains only the distinct elements of the original stream.

1
2
3
4
5
List<Integer> numbers = Arrays.asList(1, 2, 3, 3, 4, 5, 5);
List<Integer> distinctNumbers = numbers.stream()
                                        .distinct()
                                        .collect(Collectors.toList());

sorted()

The sorted() operation returns a new stream that contains the elements of the original stream sorted in natural order.

1
2
3
4
5
List<Integer> numbers = Arrays.asList(5, 4, 3, 2, 1);
List<Integer> sortedNumbers = numbers.stream()
                                      .sorted()
                                      .collect(Collectors.toList());

Terminal Operations

forEach()

The forEach() operation applies a given function to each element in the stream.

1
2
3
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
numbers.stream().forEach(System.out::println);

count()

The count() operation returns the number of elements in the stream.

1
2
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
long count = numbers.stream().count();

reduce()

The reduce() operation performs a reduction on the elements of the stream using a given function and returns an Optional object that contains the result.

1
2
3
4
5
6
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);

int totalSum = numbers
  .stream()
  .reduce(0, (subtotal, element) -> subtotal + element);

This post is licensed under CC BY 4.0 by the author.