Home > Java > javaTutorial > body text

How to perform functional operations on collections using Stream API in Java?

WBOY
Release: 2023-08-02 15:13:09
Original
969 people have browsed it

How to use the Stream API in Java to perform functional operations on collections?

With the release of Java 8, the Stream API was introduced, making functional operations on collections more concise and efficient. The Stream API provides a new way of processing collections, which can operate on collections in a declarative manner, which can reduce the complexity of the code and improve the readability and maintainability of the code. This article will introduce in detail how to use the Stream API in Java to perform functional operations on collections.

First of all, we need to understand the basic concepts of Stream API. Stream represents a sequence of data elements, which can come from any data source, such as arrays, collections, etc. The difference between Stream and collection is that it does not save the data itself, but the data calculated on demand, which means that we can perform multiple operations on the data without creating a new collection. The Stream API provides various methods for filtering, mapping, sorting and other operations on data. These methods can be called in a chain to form a processing flow, and finally get the results we want.

The following uses several examples to demonstrate how to use the Stream API to perform functional operations on collections.

  1. Filter data

First, we can use the filter method to filter data that meets the conditions. For example, we have a set of integers and we want to find elements greater than 10.

List<Integer> numbers = Arrays.asList(1, 5, 10, 15, 20);
List<Integer> result = numbers.stream()
                              .filter(n -> n > 10)
                              .collect(Collectors.toList());
System.out.println(result); // 输出 [15, 20]
Copy after login
  1. Convert data

In addition to filtering data, we can also use the map method to map the data. For example, we have a collection of strings and we want to convert each string to uppercase.

List<String> words = Arrays.asList("hello", "world", "Java", "stream");
List<String> result = words.stream()
                           .map(String::toUpperCase)
                           .collect(Collectors.toList());
System.out.println(result); // 输出 [HELLO, WORLD, JAVA, STREAM]
Copy after login
  1. Sort data

In addition, we can also use the sorted method to sort the data. For example, we have a collection of integers and we want to sort them from smallest to largest.

List<Integer> numbers = Arrays.asList(5, 2, 10, 1, 8);
List<Integer> result = numbers.stream()
                              .sorted()
                              .collect(Collectors.toList());
System.out.println(result); // 输出 [1, 2, 5, 8, 10]
Copy after login
  1. Statistical data

In addition, the Stream API also provides some simple statistical methods, such as count, min, max, sum, etc. For example, we have a set of integers and we want to calculate their average.

List<Integer> numbers = Arrays.asList(5, 2, 10, 1, 8);
double average = numbers.stream()
                        .mapToDouble(Integer::doubleValue)
                        .average()
                        .orElse(0);
System.out.println(average); // 输出 5.2
Copy after login

Through the above example, we can see that using the Stream API can make our code clearer and more concise. The Stream API provides a wealth of methods for filtering, mapping, sorting, statistics and other operations. By chaining these methods, we can implement complex data processing logic. At the same time, the Stream API also provides support for parallel processing, which can make full use of the advantages of multi-core processors and improve processing efficiency.

To sum up, using the Stream API in Java to perform functional operations on collections can greatly simplify our code and improve the readability and maintainability of the code. I hope that through the introduction of this article, I will have a better understanding of how to use the Stream API. Let us make full use of the powerful functions provided by the Stream API to improve efficiency in actual development.

The above is the detailed content of How to perform functional operations on collections using Stream API in Java?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!