Java 12 Features
Java 12 Features:
- Teeing Collectors in Stream API
Teeing Collector is the new collector utility introduced in the Streams API.
This collector has three arguments – Two collectors and a Bi-function.
- Java Strings New Methods
4 new methods have been introduced in Java 12 which are:
- indent(int n)
- transform(Function f)
- Optional describeConstable()
- String resolveConstantDesc (MethodHandles.Lookup lookup)
- Pattern Matching for instanceof
- Compact Number Formatting
- Switch Expressions enhancements
Java 12 has enhanced Switch expressions for Pattern matching.
Introduced in JEP 325, as a preview language feature, the new Syntax is L ->.
Following are some things to note about Switch Expressions:
- The new Syntax removes the need for break statement to prevent fallthroughs.
- Switch Expressions don’t fall through anymore.
- Furthermore, we can define multiple constants in the same label.
- default case is now compulsory in Switch Expressions.
- break is used in Switch Expressions to return values from a case itself.
- File.mismatch method
Java 12 added the following method to compare two files:
public static long mismatch(Path path, Path path2) throws IOException
This method returns the position of the first mismatch or -1L if there is no mismatch.
- JMH (Java Microbenchmark Harsnessing)
- Shenandoah GC
Teeing Collector:
It is a new static method teeing to java.util.stream.Collectors interface which allows to collect using two independent collectors, then merge their results using the supplied BiFunction.
Every element passed to the resulting collector is processed by both downstream collectors, then their results are merged using the specified merge function into the final result.
public static Collector teeing (Collector downstream1,
Collector downstream2,
BiFunction merger);
Comments
Post a Comment