Home  >  Article  >  Java  >  How to use Stream functions for functional operations in Java

How to use Stream functions for functional operations in Java

王林
王林Original
2023-10-19 11:19:43577browse

How to use Stream functions for functional operations in Java

How to use the Stream function for functional operations in Java

Introduction:
With the release of Java 8, the Stream API was introduced, making Functional programming becomes more convenient. The Stream API provides an efficient and easy-to-use way to process collection data. This article will introduce how to use the Stream function for functional operations in Java and provide specific code examples.

  1. Introduction to Stream
    Stream is an API for processing collection data, which provides a streaming processing method. Stream can be used to filter, map, sort, and aggregate collections. The characteristics of Stream include:
  2. The data source can be a collection, array, I/O channel, etc.
  3. Able to perform a series of intermediate operations, such as filtering, mapping, sorting, etc.
  4. Can perform terminal operations, such as aggregation, collection, traversal, etc.
  5. Provides the characteristics of lazy evaluation and short-circuit evaluation, which can be calculated only when needed, improving efficiency.
  6. Creation of Stream
    Before using the Stream function for functional operations, you first need to create a Stream object. A Stream object can be created by calling the stream method of a collection or array. For example:
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
Stream<Integer> stream = list.stream();
  1. Intermediate operations of Stream
    Stream provides a series of intermediate operations for processing data. Commonly used intermediate operations include filtering, mapping, sorting, etc. The following are some commonly used intermediate operations and how to use them:
  • Filter: used to filter elements in a collection. For example, filter out elements greater than or equal to 3:
List<Integer> result = list.stream()
    .filter(n -> n >= 3)
    .collect(Collectors.toList());
System.out.println(result); // 输出:[3, 4, 5]
  • Mapping (map): used to map elements in the collection to another type. For example, squaring the elements in the set and collecting them into a new set:
List<Integer> result = list.stream()
    .map(n -> n * n)
    .collect(Collectors.toList());
System.out.println(result); // 输出:[1, 4, 9, 16, 25]
  • Sort (sorted): used to sort the elements in the set. For example, to sort a collection in ascending order:
List<Integer> result = list.stream()
    .sorted()
    .collect(Collectors.toList());
System.out.println(result); // 输出:[1, 2, 3, 4, 5]
  1. Terminal operations of Stream
    Terminal operations of Stream are used to summarize, collect, and traverse data. Commonly used terminal operations include aggregation, collection, and traversal. The following are some commonly used terminal operations and how to use them:
  • Aggregation (reduce): used to perform aggregation operations on elements in a collection. For example, to calculate the sum of elements in a set:
int result = list.stream()
    .reduce(0, (a, b) -> a + b);
System.out.println(result); // 输出:15
  • Collect (collect): Used to collect elements in a set into a new set. For example, collect the odd numbers in the set into a new list:
List<Integer> result = list.stream()
    .filter(n -> n % 2 != 0)
    .collect(Collectors.toList());
System.out.println(result); // 输出:[1, 3, 5]
  • Traversal (forEach): used to traverse the elements in the set. For example, printing the elements in the collection:
list.stream()
    .forEach(System.out::println);

Summary:
Functional operations through the Stream function can improve the readability and maintainability of the code. This article explains how to use Stream functions for functional operations in Java and provides specific code examples. Using the Stream API can process collection data more concisely and efficiently, improving development efficiency. It is recommended to make full use of the powerful functions of the Stream API when using Java for collection processing.

The above is the detailed content of How to use Stream functions for functional operations in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn