What are Java8 streams? What are the common operations?

Java 8 has been out for more than eight years, and many features released in 2014 have carried over into 2022, and one of the most prominent features is Java Streams. In this article I'll explain what Java streams are, when to use them, how to troubleshoot stream problems, and give a brief introduction to common Java stream operations.

What are streams in Java?

Java streams support functional manipulation of streams of elements. A stream is an abstraction of an immutable collection of functions applied to data in some order. A stream is not a collection that can store elements.

The most important difference between streams and structs is that streams don't hold data, for example, you can't point to where a particular element exists in the stream, you can only specify functions that operate on that data, and when operating on a stream, the Affects the original stream.

Note that the streams in this article are not to be confused with the streams in the Java I/O package (such as InputStream, OutputStream, etc.).

When to Use Java Streams

A Java stream represents a pipeline through which data flows and functions that operate on the data, a pipeline in this instance consisting of a stream source followed by zero or more intermediate and terminal operations.

Thus, streams can be used in any number of applications involving data-driven functions.

Features of Java Streams

  • Streams are not data structures, but take input from collections, arrays, or I/O channels.
  • Streams do not change the original data structure, they only provide results according to the pipelined method.
  • Each intermediate operation is executed lazily and returns a stream as a result, so various intermediate operations can be pipelined. A terminal operation marks the end of a stream and returns a result.

different operations on the stream

Intermediate operations:

  • map : The map method is used to return a stream consisting of the results of applying the given function to the elements of this stream.
  • filter : The filter method is used to select elements based on the Predicate passed as parameter.
  • sorted : The sorted method is used to sort the stream.

Terminal operation:

  • collect : The collect method is used to return the results of intermediate operations performed on the stream.
  • forEach : The forEach method is used to iterate over each element in the stream.
  • reduce : The reduce method is used to reduce the elements of a stream to a single value.

code example

1. Filter

  • Used to remove values ​​from a Collection based on a condition.
  • The filter() method is used to filter the elements of Collection based on conditions. Only matching elements are kept.

Filter out all odd numbers:

List<Integer> nums = numList.stream()
        .filter(n -> n % 2 != 0)
        .collect(Collectors.toList());

2. Pretreatment

  • Useful when every value in the collection needs to be changed in-place.
  • The map() method is used to apply a function to each element of the Collection and return a new Collection of the computed values.

Increment a number by 1:

List<Integer> newNums = nums.stream()
        .map(n -> n + 1)
        .collect(Collectors.toList());

3. Sorting

  • Sorts the elements of the Collection.
  • The sorted() method is used to sort the elements of the Collection.

Usually, Collections.sort() is sufficient to sort the collection. If we want to do another operation after it, we can use sorted() exclusively.

List<Integer> newNums = nums.stream()
        .sorted()
        .collect(Collectors.toList());

Guess you like

Origin blog.csdn.net/weixin_43025343/article/details/132356620