background
At present, the JDK version used in production and development is already 11. Although I have read some blogs about the version after jdk8 and have some understanding of the new features, I have not fully studied them. So today I will take this time to systematically understand the new features from jdk8 to 11, salted fish
New features of JDK9
It's better to teach people to fish than to teach people to fish. Here is the address of the official website
Comprehensive introduction to the address on the official website
Changes in several common APIs
Here I would like to make some translation summary of the changes in common APIs
1. Factory Methods for Collections (JEP 269)
Collection factory method added
jdk8 creates an immutable collection code
List<Point> myList = new ArrayList<>); myList.add(new Point(1, 1)); myList.add(new Point(2, 2)); myList.add(new Point(3, 3)); myList.add(new Point(4, 4)); myList = Collections.unmodifiableList(myList);
As you can see, it's still very troublesome and convenient. We can use Guava
Lists.newArrayList();
Additional jar s need to be introduced, and the jdk9 native api now supports these operations
List<Point> list = List.of(new Point(1, 1), new Point(2, 2), new Point(3, 3), new Point(4, 4));
2. Optional Class Enhancements
Enhancements to the Optional class
Four new methods have been added to jdk9 Optional
- ifPresent(Consumer action): if there is a value, use it to perform the operation
public void ifPresent(Consumer<? super T> action) { if (value != null) { action.accept(value); } }
- Ifpresentoelse (consumer action, runnable emptyAction): if there is no value, execute emptyAction
public void ifPresentOrElse(Consumer<? super T> action, Runnable emptyAction) { if (value != null) { action.accept(value); } else { emptyAction.run(); } }
- or(Supplier supplier): if the value exists, return the value specified by Optional; otherwise, return a preset value.
public Optional<T> or(Supplier<? extends Optional<? extends T>> supplier) { Objects.requireNonNull(supplier); if (isPresent()) { return this; } else { @SuppressWarnings("unchecked") Optional<T> r = (Optional<T>) supplier.get(); return Objects.requireNonNull(r); } }
- stream(): returns a zero element or a stream of elements depending on whether a value exists
public Stream<T> stream() { if (!isPresent()) { return Stream.empty(); } else { return Stream.of(value); } }
It's very clear what these methods do based on Java 8, so I won't explain them too much here. If you don't understand them, you can directly check the comments on the source code when using them. They are very detailed
3.Stream API Enhancements
The enhancement of stream api adds four methods.
First look
TakeWhile (predict) and dropwhile (predict)
default Stream<T> takeWhile(Predicate<? super T> predicate) {// ...} default Stream<T> dropWhile(Predicate<? super T> predicate) {//...}
These two methods complement the limit () and skip () methods. The difference is that their parameters are very flexible, and a Predicate function is used
TakeWhile is very similar to the filter in Java 8. The difference is that the filter matches all, while takeWhile does not continue to match as long as one of the following is not satisfied. Take a look at the following example
Stream.of("a","b","c","","e","f").takeWhile(s->!s.isEmpty()) .forEach(System.out::print); //Output abc
dropWhile is the opposite of takeWhile
Stream.of("a","b","c","","e","f").dropWhile(s-> !s.isEmpty()) .forEach(System.out::print); // Output ef
It looks like a bit of chicken ribs. Ha ha, I haven't seen a suitable business scenario at present
ofNullable(T t)
If the specified element is non null, an element is obtained and a single element stream is generated. If the element is null, an empty stream is returned.
show code
long count = Stream.ofNullable(100).count(); // 1 System.out.println(count); count = Stream.ofNullable(null).count(); // 0 System.out.println(count);
iterate method
static <T> Stream<T> iterate(T seed, Predicate<? super T> hasNext, UnaryOperator<T> next)
To construct an infinite flow, the second parameter is the termination condition, and the third parameter is the parameter recursive to the next layer
show code
// Output: // 3 // 6 // 9 IntStream.iterate(3, x -> x < 10, x -> x+ 3).forEach(System.out::println);
summary
jdk9's new features are far more than these, but limited to space, let's introduce some common ones. If you want to learn more, it's recommended to study on the official website and get the rookie tutorial. I think the rookie tutorial is also very detailed, and it's in Chinese. Here is a list of rookie tutorials. It feels better than I said....
It is also given here Rookie tutorial link