Java 8 new features learning notes

Posted by raquelzinha on Sun, 16 Feb 2020 10:14:19 +0100

Lambda expression

To tell you the truth, when I first saw this word, it was very obscure. How to read it back and forth was very strange. Later, what you understand, of course, was to search it. Hahaha, it turned out to be that λ

It allows functions to be passed as arguments, ← to hit the blackboard! It solves the problem of whether a function can exist independently;

Also known as closure, it is the most important new feature to promote Java 8 release;

Java is a kind of approach to functional programming (functional programming, can connect methods to avoid multiple lines of code is not easy to read) = > java 8 supports functional programming

Grammatical format

(parameters) -> expression
or
(parameters) -> { statements; }

Can only be used when there is a functional interface

Four core functional interfaces:

  1. Consumer: consumer interface
    void accept(T t);
  2. Supplier: supply interface
    T get();
  3. Function: function interface
    R apply(T t);
  4. Predicate: assertion interface
    boolean test(T t);

Chestnut

Bronze:

 1. No parameter is required, and the return value is 9 / / if the parameter list is empty, just keep ();
    () -> 9  
2. Receive multiplication (numeric type)   //If there is only one parameter, () can be omitted, only the name of the parameter is required;
    x -> 3 * x  
3. Receive subtraction (numeric type) 
   (x, y) -> x – y 
 4. Receive2A certain type, Do arithmetic 
(int x, int y) -> x + y  
5. Receive one string object,And print,Does not return any value (looks like it doesvoid)  
(String s) -> System.out.print(s)

Specific:

package test;

public class lab001 {	
	public static void main(String[] args) {
  		lab001 tester = new lab001();
  
  		MathOperation addition = (int a, int b) -> a + b;  // Type declaration   
  
     		MathOperation subtraction = (a, b) -> a - b;    // No type declaration. It has been declared above. The compiler can recognize     
     
     		MathOperation multiplication = (int a, int b) -> { return a * b; };// Return statements in braces
            
     		MathOperation division = (int a, int b) -> a / b;// No braces and return statements
         
         	System.out.println("2 + 3 = " + tester.operate(2, 3, addition));
     		System.out.println("2 - 3 = " + tester.operate(2, 3, subtraction));
     		System.out.println("2 x 3 = " + tester.operate(2, 3, multiplication));
     		System.out.println("6 / 2 = " + tester.operate(6, 2, division));
         
         	zi greetService1 = message -> System.out.println("Hello " + message);// No brackets
         
     		zi greetService2 = (message) -> System.out.println("Hello " + message);// Parenthesis
         
       		greetService1.sayMessage("2020");
       		greetService2.sayMessage("hello");
  	}
	//There must be an interface
     	interface MathOperation {
     		int operation(int a, int b);
     	}
     
     	interface zi {
      		void sayMessage(String message);
     	}
     
     	private int operate(int a, int b, MathOperation mathOperation){
      		return mathOperation.operation(a, b);
     	}
}

6. Only outer local variables marked final can be referenced. If it is not final, it must not be modified by subsequent code (i.e. implicit with final semantics), or compilation error.

And it is not allowed to declare a local variable with the same name

public class test {
 
   final static String put= "Hello ";
   
   public static void main(String args[]){
      GreetingService zi = message -> System.out.println(put + message);
      zi.sayMessage("2020");
   }
    
   interface GreetingService {
      void sayMessage(String message);
   }
}

Output: Hello 2020
7.You can directly access the outer local variables

public class test {
    public static void main(String args[]) {
        final int num = 1;
        
        Converter<Integer, String> zi = (result) -> System.out.println(String.valueOf(result+ num));
        zi.convert(2);  // Output 3
    }
 
    public interface Converter<T1, T2> {
        void convert(int i);
    }
}

The reason why it is so called is vivid. Do you have it? Please look down obliquely for details:

Today's study is here, if there is something wrong, welcome to correct! Thank you very much!

Main reference blog:

Published 4 original articles, praised 0, visited 23
Private letter follow

Topics: Java Lambda Programming