I plan to write a series of articles on Java 9 over the next few months. While Java 9 is not a core Version of Java like Java 8 or Java 11, it does have a number of features worth noting. I look forward to your attention as THIS article is the sixth in a series of ten articles on Java 9.
- Java9 article series access address
This article takes you through a quick look at what useful changes and enhancements have taken place in the 9 Java collection subclasses Colleaction.
The syntax of Java Util Stream has been optimized and enhanced in Java 9, so let’s take a look at some useful ways to use it.
1. Stream.takeWhile(Predicate)
When the Stream is piped, the Predicate condition returns false and the rest of the data elements are skipped. In the example below, once the Predicate condition!” Orange “.equals(s) returns false, the other elements will be skipped:
String[] fruits = {"apple", "banana", "orange", "mango", "peach"}; Stream<String> stream = Arrays.stream(fruits) .takeWhile(s -> !" orange".equals(s)); stream.forEach(System.out::println);Copy the code
The console output will filter the elements in the array until the orange element is satisfied! Orange “.equals(s) === false, stream processing does not continue to return directly.
apple
banana
Copy the code
Note that for an unordered Stream, the return value from this operation is indeterminate if there are multiple elements (multiple oranges) that match the provided Predicate.
This method looks similar to stream.filter () in Java 8, except that the filter() method simply skips the mismatched elements and continues processing. However, the takeWhile() method skips all remaining elements after the presence of a match, sort of like a continue versus a break. Here is an example of a filter() method with the same flow and Predicate:
String[] fruits = {"apple", "banana", "orange", "mango", "peach"}; Stream<String> stream = Arrays.stream(fruits).filter(s -> !" orange".equals(s)); stream.forEach(System.out::println);Copy the code
The console output is as follows, with orange filtered out.
apple
banana
mango
peach
Copy the code
2.Stream.dropWhile(Predicate)
After the supplied Predicate condition returns false in the pipe flow, all data elements following this element are returned as return values.
String[] fruits = {"apple", "banana", "orange", "mango", "peach"}; Stream<String> stream = Arrays.stream(fruits) .dropWhile(s -> !" orange".equals(s)); stream.forEach(System.out::println);Copy the code
In the above example, once the Predicate condition! Orange “.equals(s) returns false, and the remaining elements in the pipe stream will be accepted (unfiltered) as the return value:
orange
mango
peach
Copy the code
3.Iterate (T, Predicate, UnaryOperator)
Once the Predicate condition returns false, this method returns a sequential stream that stops iterating. T is the initial value, and the iteration is provided by the UnaryOperator
Iterate = Stream. Iterate ("-", s -> s.length() < 5, //Predicate condition s -> s + "-"); // Iterate iterate. ForEach (system.out ::println);Copy the code
The console prints the result with only four dashes, stopping at the fifth. This is determined by the Predicate condition.
-
--
---
----
Copy the code
4.Stream Stream.ofNullable(T)
This method returns a sequential Stream containing a single element. If the supplied element is NULL, this method returns an empty Stream. This approach is useful when we want to attach a non-empty single element to a stream. Such as:
String nullableItem = "peach";
Stream<String> stream = Stream.of("apple", "banana", "orange");
Stream<String> stream2 = Stream.concat(stream, Stream.ofNullable(nullableItem));
stream2.forEach(System.out::println);
Copy the code
The console output is as follows:
apple
banana
orange
peach
Copy the code
5.IntStream, LongStream, and DoubleStream methods
The following XxxStream classes also have Stream equivalents (except for the ofNullable() method).
IntStream.of(2, 4, 6, 8, 9, 10, 11) .takeWhile(i -> i % 2 == 0) .forEach(System.out::println); // 2,4,6,8Copy the code
IntStream.of(2, 4, 6, 8, 9, 10, 11) .dropWhile(i -> i % 2 == 0) .forEach(System.out::println); / / 9, 11Copy the code
IntStream.iterate(0, i -> i < 10, i -> i + 1) .forEach(System.out::print); / / 0123456789Copy the code
Welcome to my blog, where there are many fine collections
- This article is reprinted with a credit (must be accompanied by a link, not only the text) : Antetokounmpo blog.
Feel helpful to you, help me like, share! Your support is my inexhaustible creative power! . In addition, the author recently a period of time output as follows boutique content, looking forward to your attention.
- Spring Boot2.0 by Hand
- Spring Security- JWT-OAUTH2
- RBAC Authority Management System for Actual Combat Front-end and Back-end Separation
- “Actual SpringCloud Micro-service from Bronze to King”
- VUE Series