Home > Java > javaTutorial > body text

How to use IntStream function in Java for numerical stream operations

WBOY
Release: 2023-06-26 20:15:08
Original
2389 people have browsed it

The IntStream function in Java is a newly introduced functional interface in Java 8. It can perform streaming operations on integer type data, including filtering, mapping, collection and other operations. This article will introduce how to use the IntStream function for numerical stream operations.

1. Introduction to IntStream function

IntStream is a functional interface in Java, which represents a stream of elements whose basic type is int. It inherits the Stream interface and adds methods for the basic type int.

The methods in IntStream are mainly divided into the following four categories:

  1. Create streams: range, rangeClosed, of, iterate, generate and other methods.
  2. Conversion stream: map, flatMap, filter, distinct, sorted and other methods.
  3. Terminate the stream: forEach, toArray, sum, average, reduce and other methods.
  4. Operation flow: min, max, count, anyMatch, allMatch, noneMatch and other methods.

2. Basic usage of the IntStream function

The usage of IntStream is basically the same as that of Stream. We can use the IntStream.range method to create an integer stream in a specified range:

IntStream intStream = IntStream.range(1, 10);
Copy after login

We can also use the IntStream.of method to create an integer stream of specified elements:

IntStream intStream = IntStream.of(1, 3, 5, 7, 9);
Copy after login

We can also use the IntStream.iterate method to create an iterative stream:

IntStream intStream = IntStream.iterate(1, n -> n + 2).limit(5);
intStream.forEach(System.out::println);//1 3 5 7 9
Copy after login

After creating the stream , we can use intermediate operations for conversion. For example, we can use the map method to multiply each element:

IntStream intStream = IntStream.range(1, 10);
intStream.map(n -> n * 2).forEach(System.out::println);//2 4 6 8 10 12 14 16 18
Copy after login

We can also use the filter method to filter out elements that meet the conditions:

IntStream intStream = IntStream.range(1, 10);
intStream.filter(n -> n % 2 == 0).forEach(System.out::println);//2 4 6 8
Copy after login

After completing the intermediate operation, We can use the termination operation to complete statistics, collection and other operations. For example, we can use the sum method to sum the stream:

IntStream intStream = IntStream.range(1, 10);
int sum = intStream.filter(n -> n % 2 == 0).sum();
System.out.println(sum);//20
Copy after login

We can also use the toArray method to convert the elements in the stream into an array:

IntStream intStream = IntStream.range(1, 10);
int[] arr = intStream.filter(n -> n % 2 == 0).toArray();
System.out.println(Arrays.toString(arr));//[2, 4, 6, 8]
Copy after login

3. Advanced usage of the IntStream function

IntStream also has more advanced uses, such as flatMap, reduce, collect and other methods. The use of these methods is introduced below.

  1. flatMap method

The flatMap method can map each element of a stream to a stream, and then merge these small streams into one large stream. For example, we can use the flatMap method to convert each number into the corresponding string, and then merge these strings into a stream:

IntStream intStream = IntStream.range(1, 5);
Stream<String> stream = intStream.flatMap(n -> Stream.of(String.valueOf(n)));
stream.forEach(System.out::println);//"1" "2" "3" "4"
Copy after login
  1. reduce method

reduce method The elements in the stream can be reduced to a value, such as summation, product, etc. We can use the reduce method to sum the stream of int type:

IntStream intStream = IntStream.range(1, 5);
int sum = intStream.reduce(0, (a, b) -> a + b);
System.out.println(sum);//10
Copy after login
  1. collect method

The collect method can collect the elements in the stream into a container, such as List , Set, Map, etc. We can use the collect method to collect int type streams into a List:

IntStream intStream = IntStream.range(1, 5);
List<Integer> list = intStream.boxed().collect(Collectors.toList());
System.out.println(list);//[1, 2, 3, 4]
Copy after login

4. Summary

This article introduces the basic and advanced usage of the IntStream function in Java. The IntStream function can perform streaming operations on elements of type int and provides a wealth of intermediate operations and termination operations, allowing us to easily complete various numerical calculations and data collection tasks. I hope that readers can master the use of the IntStream function through the introduction of this article, which will facilitate future Java development work.

The above is the detailed content of How to use IntStream function in Java for numerical stream operations. 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