Lambda表达式
 
package com.www.java1;
import org.junit.Test;
import java.util.Comparator;
import java.util.function.Consumer;
public class LambdaTest {
    
    @Test
    public void test(){
        Runnable runnable = new Runnable(){
            @Override
            public void run() {
                System.out.println("我爱我家");
            }
        };
        runnable.run();
        System.out.println("*************");
        Runnable runnable1 = () ->{
            System.out.println("我爱我家");
        };
        runnable.run();
    }
    @Test
    public void test1(){
        Comparator<Integer> com = new Comparator<Integer>(){
            @Override
            public int compare(Integer o1, Integer o2) {
                return Integer.compare(o1,o2);
            }
        };
        int compare = com.compare(11, 22);
        System.out.println(compare);
        System.out.println("**************");
        
        
        Comparator<Integer> com1 = (o1,o2) -> Integer.compare(o1,o2);
        int compare1 = com1.compare(11, 22);
        System.out.println(compare1);
        System.out.println("**************");
        
        Comparator<Integer> com2 = Integer :: compare;
        int compare2 = com2.compare(11, 22);
        System.out.println(compare2);
    }
    
    @Test
    public void test2(){
        Consumer<String> con = new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        };
        con.accept("我们都有一个家");
        Consumer<String> con1 = (String s) -> {
            System.out.println(s);
        };
        con1.accept("名字是中国");
    }
    
    @Test
    public void test3(){
        Consumer<String> con1 = (s) -> {
            System.out.println(s);
        };
        con1.accept("名字是中国");
    }
    
    @Test
    public void test4(){
        Consumer<String> con1 = s -> {
            System.out.println(s);
        };
        con1.accept("名字是中国");
    }
    
    @Test
    public void test5(){
        Comparator<Integer> com = new Comparator<Integer>(){
            @Override
            public int compare(Integer o1, Integer o2) {
                System.out.println(o1);
                return Integer.compare(o1,o2);
            }
        };
        int compare = com.compare(11, 22);
        System.out.println(compare);
        System.out.println("**************");
        Comparator<Integer> com1 = (o1,o2) -> {
            System.out.println(o1);
            return Integer.compare(o1,o2);
        };
        int compare1 = com1.compare(11, 22);
        System.out.println(compare1);
        System.out.println("**************");
    }
    
    @Test
    public void test6(){
        Consumer<String> con1 = s -> System.out.println(s);
        con1.accept("名字是中国");
    }
}
 
方法引用与构造器引用
 
package com.www.java1;
import org.junit.Test;
import java.io.PrintStream;
import java.util.Comparator;
import java.util.function.Consumer;
public class LambdaTest1 {
    
    
    
    @Test
    public void test(){
        Consumer<String> con1 = s -> System.out.println(s);
        con1.accept("名字是中国");
        PrintStream out = System.out;
        Consumer<String> con2 = out :: println;
        con1.accept("名字是中国");
    }
    
    
    
    @Test
    public void test1(){
        Comparator<Integer> com1 = (o1, o2) -> Integer.compare(o1,o2);
        int compare1 = com1.compare(11, 22);
        System.out.println(compare1);
        Comparator<Integer> com2 = Integer::compare;
        int compare2 = com2.compare(11, 22);
        System.out.println(compare2);
    }
    
    
    
    @Test
    public void test2(){
        Comparator<String> com1 = (o1, o2) -> o1.compareTo(o2);
        int compare1 = com1.compare("abd", "abc");
        System.out.println(compare1);
        Comparator<String> com2 = String::compareTo;
        int compare2 = com2.compare("abd", "abc");
        System.out.println(compare2);
    }
}
 
StreamAPI
 
Optional