(new features in java8, 9, 10, 11)

A, Java8

1. Lambda expressions

Lambda is an anonymous function, similar to a piece of code that can be passed, concise and flexible

  • “->” is called the Lambda operator or arrow operator:
  • Left: Specifies the arguments required by the Lambda expression
  • On the right: specifies the Lambda body, which is the implementation logic of the abstract method, that is, the concrete functionality to be implemented

Syntax format

()->{system.out. print(“lala”); }; 2. One argument, no return value :(String STR) ->{system.out.priont (); }; Type inference :(STR) ->{system.out.print (); }; 4. When a parameter, the parentheses can be omitted: STR ->{system.out.print (); }; 5. Multiple parameters, multiple statements, can return values :(x,y) ->{System…. ; return Integer.compare(x,y); }; (x,y) ->Integer(x,y);

Type inference Lambda expressions do not need to specify a type because JavAC infer the parameter types behind the scenes, based on the program context. (Inferred by the compiler)

2. Functional interfaces

An interface that contains only one abstract method needs to use the @functionalinterface annotation on an interface to check if it is a FunctionalInterface, and Javadoc will also declare that it is a FunctionalInterface. Lambda expressions are objects, not functions, and need to be attached to a special object — the FunctionalInterface

Previously anonymous implementation classes can now be written in Lambda expressions

3. Method references and constructor references

Use :: to separate classes from method names. Object :: Instance method name Class :: Static method name Class :: Instance method name

// Method reference
Comparator<Integer> com = (x,y)->Integer.compare(x,y);
||
Comparator<Integer> com2 = Integer::compare;
// The constructor references ClassName::new
Function fun = (n)->new MyClass(n);
||
Function fun2 = MyClass::new;
Array reference type[]::new
function fun = (n)->new Integer[n];
||
function fun2 = Integer[]::new

Copy the code

4. Powerful Stream API

A Collection is static memory data, and a Stream is about computation. A Stream is a data channel used to manipulate the sequence characteristics of elements generated by the data source

  • The Stream itself does not store elements
  • The source object is not changed
  • Deferred action, meaning they wait until the result is needed

Step create Stream, intermediate action, terminate action

Create stream 1.Collection fetch:

  • Stream () : Returns a sequential stream
  • ParallelStream () : Returns parallel streams

Stream (T[] array) : returns a stream 3. 4. Create unlimited stream.iterate and stream.generate ()

In the middle of the method: filtering slice, mapping, sorting termination operations: matching search, reduction, collection

1. Three ways to generate a Stream

  • Collection system

    Generate streams using the default stream() method, default stream stream()

  • Set of Map systems

    Turn a Map into a Set, indirectly generating a stream

  • An array of

    Static method of(T… Generate flow values)

3.Stream Intermediate operation method

instructions
filter Filter the data
limit Before intercepting specified parameter data, return the stream
skip Skips the specified number of arguments and returns the stream
concat Merge two streams A and B into one stream
distinct Returns a stream composed of different streams
sorted Natural sequence
sorted(Comparator comparator) Sort by the provided Comparator
map Return flow
mapToInt Returns a stream of IntStream elements

4.Stream Termination operation method

After executing this method, the Stream does no more operations

methods instructions
forEach Perform operations on each element of the flow
count Returns the number of elements in this stream

5.Stream Collection operation

methods instructions
collect Collect the results into the collection
static Collector toList() Collect elements into the List collection
static Collector toSet() Collect elements into the Set collection
static Collector toMap(keyMapper,valueMapper) Collect elements into the Map collection

5. Optional classes

This is a container class that holds values of type T, which can be arbitrary, including NULL.

Second, the Java9

Modular system Jigsaw

  • Exports: Controls which packages can be accessed by other modules. All packages that are not exported are wrapped in modules by default (SRC where module-info.java is needed).
  • Requires: specifies dependencies for other modules (declare module-info.java under the used module SRC)

JShell command

  • Write it now! Run fast!

Interface private methods

Third, Java10

Local variable type inference collection new methods to create immutable collection of of, copyOf

Four, Java11

ZGC: Concurrent, compressed garbage collector added string handling Optiona enhanced HTTP client API…

As the free JDK version stays in JDK8, we will focus on the new features of JDK8.0.Copy the code