This is the 17th day of my participation in Gwen Challenge

preface

Java 8 (also known as JDK1.8) is an important release in Java programming. It was released on March 18, 2014, and includes a number of new apis.

A, Optional classes

Everyone should have encountered a “null pointer” in the process of programming code, I do not believe that no one has encountered.

Java 8 introduces a new container to address the null pointer problem: the Optional class.

The Optional class is a container object that can be null. The isPresent() method returns true if the value exists, and calling the get() method returns the object.

Optional is a container: it can hold values of type T, or just NULL. Optional provides a number of useful methods so that we don’t have to explicitly null-check.

 // optional. ofNullable - Allows passing null arguments
Optional<Integer> a = Optional.ofNullable(value1);
Copy the code

With this statement, it is no longer possible to pass the value null, even if null, can also catch.

 // optional. orElse - Allows passing as null, and returns 0L if null is passed.
Optional<Integer> a = Optional.ofNullable(value1).orElse(0L);
Copy the code
// optional. of - Throws a NullPointerException if the passed argument is null
Optional<Integer> b = Optional.of(value2);
Copy the code
Java 8 sorting

In contrast to Java 7’s sorting, the syntax is fairly brief.

  // Sort using Java 7
private void sortUsingJava7(List<String> names){         
Collections.sort(names, new Comparator<String>() {         
        @Override         
	public int compare(String s1, String s2) {            
            returns1-s2; }}); }Copy the code
// Sort using Java 8
private void sortUsingJava8(List<String> names){ Collections.sort(names, (s1, s2) -> s1.compareTo(s2)); }}Copy the code
Lambda expressions

Lambda allows functions to be passed as arguments to a method.

Two features: simplified syntax and simple interface.

Syntax format:

(parameters) -> expression

or

(parameters) ->{ statements; }

The syntax is () -> {}, where () describes the argument list, {} describes the method body, and -> is the lambda operator, read (goes to).

Traverse the collection

Lambda expressions can make collection traversal code more concise, as shown below by the forEach method traversal collection.

   ArrayList<Integer> list = new ArrayList<>();       
   Collections.addAll(list, 1.2.3.4.5);       //lambda expression method references
   list.forEach(System.out::println);       
   list.forEach(element -> {        
       if (element % 2= =0) { System.out.println(element); }});Copy the code

Interface implementation

Lambda expressions can also be used to do simple implementations of interfaces.

public class Test1 {    
public static void main(String[] args) {         
    // No return with no parameters
    NoReturnNoParam noReturnNoParam = () -> {            
        System.out.println("NoReturnNoParam");        
};        
    noReturnNoParam.method();         
    // No return for an argument
    NoReturnOneParam noReturnOneParam = (int a) -> { 
        System.out.println("NoReturnOneParam param:" + a);        
};        
    noReturnOneParam.method(6);         
    // No return for multiple parameters
    NoReturnMultiParam noReturnMultiParam = (int a, int b) -> { 
        System.out.println("NoReturnMultiParam param:" + "{" + a +"," + + b +"}"); 
};        
    noReturnMultiParam.method(6.8);
}
Copy the code
@FunctionalInterface 
public interface NoReturnMultiParam {    
    void method(int a, int b); 
} 
/** No parameter no return value */ 
@FunctionalInterface 
public interface NoReturnNoParam {    
    void method(a); 
} 
/** No return */ for one argument 
@FunctionalInterface 
public interface NoReturnOneParam {    
    void method(int a); 
} 
/** Multiple arguments have return values */ 
@FunctionalInterface 
public interface ReturnMultiParam {    
    int method(int a, int b); 
}
Copy the code