The Stream function in Java is a new feature introduced in Java 8. It provides a new way to handle data sets such as collections and arrays. Stream provides a functional programming method to perform stream operations, and also greatly simplifies code writing. In this article, we will explore how to perform streaming operations using the Stream function in Java.
1. What is the Stream function
The Stream function is a new feature introduced in Java 8. It provides us with a functional programming way to perform stream operations to simplify our code. The Stream function can be used to operate on elements in data structures such as collections and arrays to achieve the functions we need.
The Stream function has two operation modes, namely intermediate operation and termination operation. Intermediate operations can be performed multiple times, while termination operations stop processing the stream and return a result or side effect. At the same time, Stream also has the capability of parallel processing.
2. Use the Stream function for stream operations
We can use the stream() method of the collection or the Stream.of( of the array ) method to obtain the stream object. For example:
List<Integer> list = Arrays.asList(1, 2, 3); Stream<Integer> stream1 = list.stream(); // 通过list对象获取stream对象 Stream<Integer> stream2 = Stream.of(1, 2, 3); // 通过Stream.of()方法获取stream对象
Stream provides many intermediate operation methods, which can perform filtering, mapping, slicing and other operations. Let’s take a look at these operation methods one by one:
(1) filter() method: filter elements
List<Integer> list = Arrays.asList(1, 2, 3, 4); Stream<Integer> stream = list.stream(); stream.filter(i -> i%2 == 0).forEach(System.out::println); // 输出2和4
(2) map() method: map elements
List<String> list = Arrays.asList("Java", "Python", "Ruby"); Stream<String> stream = list.stream(); stream.map(String::toLowerCase).forEach(System.out::println); // 输出java, python, ruby
(3) flatMap() method: flatten stream
List<List<Integer>> list = Arrays.asList(Arrays.asList(1, 2), Arrays.asList(3, 4), Arrays.asList(5, 6)); Stream<List<Integer>> stream = list.stream(); stream.flatMap(Collection::stream).forEach(System.out::println); // 输出1, 2, 3, 4, 5, 6
(4) distinct() method: deduplication
List<Integer> list = Arrays.asList(1, 2, 3, 1, 2, 3); Stream<Integer> stream = list.stream(); stream.distinct().forEach(System.out::println); // 输出1, 2, 3
(5) sorted() method: sort
List<Integer> list = Arrays.asList(3, 2, 1); Stream<Integer> stream = list.stream(); stream.sorted().forEach(System.out::println); // 输出1, 2, 3
(6) limit() method: limit the number of elements
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); Stream<Integer> stream = list.stream(); stream.limit(3).forEach(System.out::println); // 输出1, 2, 3
(7) skip() method: skip elements
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); Stream<Integer> stream = list.stream(); stream.skip(2).forEach(System.out::println); // 输出3, 4, 5
(8) peek() method: append elements
List<Integer> list = Arrays.asList(1, 2, 3); Stream<Integer> stream1 = list.stream(); Stream<Integer> stream2 = stream1.peek(System.out::println); stream2.count(); // 输出1, 2, 3
Stream provides many termination operation methods, which can return the results of stream processing. Let’s take a look at these operation methods one by one:
(1) forEach() method: iterate the elements in the stream
List<Integer> list = Arrays.asList(1, 2, 3); Stream<Integer> stream = list.stream(); stream.forEach(System.out::println); // 输出1, 2, 3
(2) count() method: count the number of elements
List<Integer> list = Arrays.asList(1, 2, 3); Stream<Integer> stream = list.stream(); stream.count(); // 输出3
(3) toArray() method: Convert the elements in the stream to an array
List<Integer> list = Arrays.asList(1, 2, 3); Stream<Integer> stream = list.stream(); Object[] array = stream.toArray(); System.out.println(Arrays.toString(array)); // 输出[1, 2, 3]
(4) reduce() method: Reduce the elements in the stream
List<Integer> list = Arrays.asList(1, 2, 3); Stream<Integer> stream = list.stream(); int result = stream.reduce(0, (x, y) -> x + y); System.out.println(result); // 输出6
(5) collect() method: collect elements in the stream
List<Integer> list = Arrays.asList(1, 2, 3); Stream<Integer> stream = list.stream(); List<Integer> newList = stream.collect(Collectors.toList()); System.out.println(newList); // 输出[1, 2, 3]
The Stream function also provides the function of parallel stream operation, which can Convert the stream to a parallel stream through the parallel() method. Parallel streams can make full use of the processing power of multi-core CPUs and improve the running efficiency of programs. For example:
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); Stream<Integer> stream = list.parallelStream(); stream.forEach(System.out::println); // 输出1, 2, 3, 4, 5
3. Summary
The Stream function is a new feature in Java 8. It provides a new way to process data sets such as collections and arrays. Through the intermediate operations and termination operations of the Stream function, we can easily filter, map, deduplicate, sort, limit the number of elements, skip elements, append elements and other operations. At the same time, through parallel stream operations, we can make full use of the processing power of multi-core CPUs and improve the running efficiency of the program. The above introduction is only part of the functions of the Stream function. Complex business logic needs to be implemented by applying various operation methods of the Stream function.
The above is the detailed content of How to use Stream function in Java for stream operations. For more information, please follow other related articles on the PHP Chinese website!