New features in Java8
Java8 overview of
- Java8 is a major release of the Java language. It was released in March 2014 and is the most revolutionary release since Java5. This release contains more than a dozen new features in the language, compilers, libraries, tools, and JVM.
Functional interface
- A functional interface is an interface that contains only an abstract method, such as java.lang.Runnable and java.util.Comparator interfaces.
- Java8 provides the @functionalInterface annotation to define functional interfaces. An error will be reported if the defined interface does not conform to the functional specification.
- Java8 has added the java.util.function package, which contains common functional interfaces as follows:
The name of the interface | Method statement | Function is introduced |
---|---|---|
Consumer | void accept(T t) | Perform operations based on the specified parameters |
Supplier | T get() | Get a return value |
Function<T,R> | R apply(T t) | Performs an action based on the specified parameters and returns |
Predicate | boolean test(T t) | Checks whether the specified parameter meets the condition |
Lambda expressions
- Lambda expressions are an important way to instantiate functional interfaces, and use Lambda expressions to make code more compact.
- Lambda expressions consist of a list of arguments, an arrow symbol ->, and a method body that can be an expression or a block of statements.
- Syntax format :(parameter list) -> {method body; } – The (), parameter type, {}, and return keyword can be omitted.
Method references
- A method reference refers primarily to a method referred to by its name without having to provide the method body for the method reference, and the call to the method is handed over to the functional interface for execution.
- Method references use a pair of colons :: to connect a class or object to a method name, usually as follows:
- Object that references ObjectName :: MethodName
- The static method of the class references ClassName :: StaticMethodName
- Nonstatic methods of the class refer to ClassName :: MethodName
- Constructor reference ClassName :: new
- Array reference TypeName[] :: new
- Method reference is a simplified representation of lambda expressions in specific scenarios, which can further simplify the writing of code to make the code more compact and concise, thus reducing redundant code.
The Stream interface
The basic concept
- The java.util.stream. stream interface is an enhancement of collection functionality, allowing you to perform complex search, filtering, and filtering operations on collection elements.
- The Stream interface greatly improves the programming efficiency and program readability by means of Lambda expressions. Meanwhile, it provides serial and parallel modes for convergence operation, and the concurrent mode can make full use of the advantages of multi-core processors
Using the step
- Create a Stream to retrieve a Stream from a data source.
- Converts a Stream, returning a new Stream object each time.
- Aggregate the Stream and produce the result.
Create a way
- Method 1: Get the Stream by calling the collection’s default method, e.g. : Default Stream Stream ()
- Static IntStream stream(int[] array) static IntStream stream(int[] array)
- Static Stream of(T… values)
- Static Stream generate(Supplier
s)
In the middle of operation
- The common methods of screening and slicing are as follows:
Method statement | Function is introduced |
---|---|
Stream filter(Predicate<? super T> predicate) | Returns a stream containing matching elements |
Stream distinct() | Returns a stream that contains no repeating elements |
Stream limit(long maxSize) | Returns a stream that does not exceed a given number of elements |
Stream skip(long n) | Returns the stream after the first n elements are discarded |
- Common mapping methods are as follows:
Method statement | Function is introduced |
---|---|
Stream map(Function<? super T,? extends R> mapper) | Returns a stream composed of each processed element |
Stream flatMap(Function<? super T,? extends Stream<? extends R>> mapper) | Returns a stream composed of each replaced element and combines all streams into a single stream |
- Common sorting methods are as follows:
Method statement | Function is introduced |
---|---|
Stream sorted() | Returns a naturally ordered stream of elements |
Stream sorted(Comparator<? super T> comparator) | Returns a comparator sorted stream of elements |
Termination of operations
- The common methods of matching and searching are as follows:
Method statement | Function is introduced |
---|---|
Optional findFirst() | Returns the first element of the stream |
boolean allMatch(Predicate<? super T> predicate) | Returns whether all elements match |
boolean noneMatch(Predicate<? super T> predicate) | Returns no element matches |
Optional max(Comparator<? super T> comparator) | Returns the maximum element based on the comparator |
Optional min(Comparator<? super T> comparator) | Returns the smallest element based on the comparator |
long count() | Returns the number of elements |
void forEach(Consumer<? super T> action) | Perform operations on each element in the flow |
- Common methods of protocol are as follows:
Method statement | Function is introduced |
---|---|
Optional reduce(BinaryOperator accumulator) | Returns the combined element value |
- Common methods of collection are as follows:
Method statement | Function is introduced |
---|---|
<R,A> R collect(Collector<? super T,A,R> collector) | Elements are processed using collectors |
Optional class
The basic concept
- The java.util.Optional class can be understood as a simple container whose value may or may not be null, indicating the presence or absence of a value.
- The introduction of this class is a good solution to null-pointer exception without explicit null-value detection.
Common methods
Method statement | Function is introduced |
---|---|
static Optional ofNullable(T value) | Returns an object of type Optional based on the value specified by the argument |
Optional map(Function<? super T,? extends U> mapper) | Returns an object of type Optional based on the result of the parameter specifying rule |
T orElse(T other) | Return the value of other if it exists. |
New features in Java9
Java9 overview of
- Java9 was released in September 2017 and brought a number of new features, the main change being the modular system.
- A module is an encapsulation of code and data. The code of a module is organized into multiple packages, each containing Java classes and interfaces, and the data of a module includes resource files and other static information.
Use of modularity
Syntax format
- In the module-info.java file, we can declare a module with the new keyword module, as follows:
moduleModule name {}Copy the code
Advantages of modularity
- Reduce memory overhead.
- It can simplify the development and maintenance of various class libraries and large applications.
- Security, maintainability, and improved performance.
The use of the diamond operator is upgraded
- The diamond operator is allowed in Java9 for the use of anonymous inner classes.
Set factory method
The basic concept
- The static factory method of was added to the List, Set, and Map collections in Java9 to create immutable instances.
- Immutables now cannot add, modify, or delete their elements.
- Adding null element objects is not allowed.
Practical significance
- Ensure thread-safety: ensure thread-safety in concurrent programs, but also greatly enhance the efficiency of concurrent.
- It is safe when used by untrusted libraries.
- If an object does not need to support modification operations, it saves space and time.
- Can be treated as a constant, and the object will not change in the future.
InputStream enhancement
- The InputStream class provides a transferTo method to transfer data directly to the OutputStream.
New features in Java10
Java10 overview of
- Java10 was released in March 2018, with key improvements including a local type inference and an enhancement to garbage collection.
- Java10 is planned to be a short-term release, so public updates will be completed in six months, and The September release of Java11 will be the long-term Support (LTS) version of Java, which is released every three years.
Local variable type inference
The basic concept
- Java10 can use var as a local variable type inference identifier, which applies only to local variables, enhanced indexes for loops, and local variables for traditional for loops.
- It cannot be used for method formal arguments, constructor formal arguments, method return types, fields, catch formal arguments, or any other type of variable declaration.
Practical significance
- The var identifier is not a keyword, but a reserved type name. This means that var is used as a variable, and code with method or package names will not be affected, but var cannot be used as the name of a class or interface.
- Information redundancy is avoided.
- The variable names are aligned.
- Easier to read.
New features for Java11
Java11 overview of
- Java11 was officially released in September 2018, the first long-term support release after the change in Java’s big release cycle, and it’s worth noting.
Simplified compile-run operations
- In Java11, you can compile and run operations at once using Java commands.
- The first class in the execution source file must contain the main method.
- Custom classes from other source files cannot be used.
New String class method
Method statement | Function is introduced |
---|---|
boolean isBlank() | Determines whether the string is empty or contains only blank code points |
Optional map(Function<? super T,? extends U> mapper) | Returns an object of type Optional based on the result of the parameter specifying rule |
T orElse(T other) | Return the value of other if it exists. |