Lambda表达式

概要:lambda表达式是一个匿名函数,简单理解是可以传递的代码
lambda操作符:->:
左侧:Lambda表达式的参数列表,对应接口中抽象方法和参数列表
右侧:Lambda表达式所执行的功能,即Lambda体,对应接口中抽象方法的实现

	 /**
	     * lambda 表达式基础语法
	     * 1.无参数,无返回值
	     */
	    @Test
	    public void test01() {
	        int num = 0;
	        Runnable r = new Runnable() {
	            @Override
	            public void run() {
	                System.out.println("Hello world" + num);
	            }
	        };
	        r.run();
	        System.out.println("--------------------------");
	
	        Runnable r2 = () -> System.out.println("Hello Lambda!");
	        r2.run();
	    }
	
	    /**
	     * 有一个参数,无返回值
	     */
	    @Test
	    public void test2() {
	        Consumercon = (t) -> System.out.println(t);
	        con.accept("Hello Lambda!");
	    }
	
	    /**
	     * 只有一个参数,小括号可以省略不写
	     */
	    @Test
	    public void test3() {
	        Consumercon = t -> System.out.println(t);
	        con.accept("Hello Lambda");
	    }
	
	    /**
	     * 有两个以上的参数,有返回值,并且Lambda体中有多条语句
	     */
	    @Test
	    public void test4() {
	        Comparatorcom = (x, y) -> {
	            //多个语句必须要{}
	            System.out.println("Hello Lambda!");
	            return Integer.compare(x, y);
	        };
	        System.out.println(com.compare(9,5)); //第一个参数大返回1小返回-1
	
	    }
	
	    @Test
	    /**
	     * 若Lambda体中只有一条语句,return和大括号都可以省略不写
	     */
	    public void test5() {
	        Comparatorcom = (x,y) -> Integer.compare(x,y);
	
	        System.out.println(com.compare(9,5)); //第一个参数大返回1小返回-1
	    }

函数式接口: 接口中只有一个抽象方法的接口,称为函数式接口
1.通过注解@FunctionalInterface修饰

package com.zyd;
@FunctionalInterface
public interface MyFunction{
    public T getValue(T o);
}


  /**
     *对一个数进行运算
     */
    @Test
    public void test6(){
        Integer result = operation(100,(x) -> x*x);
        System.out.println(result);//10000

        result = operation(200,(y) -> y - 100);
        System.out.println(result);//100
    }
    public Integer operation(Integer num,MyFunctionmf){
        return mf.getValue(num);
    }

四大内置核心函数式接口
简化匿名内部类方法.只有一个接口

Consumer:消费类型接口

 @Test
public void test1() {
    Consumercon = (m) -> System.out.println("第一份工作:" + m + "元");
    con.accept(1000.00);
}


Supplier供给型接口

 /**
     * Supplier供给型接口
     * T get()
     * 需求:产生指定个数的整数,并放入集合中
     */
    @Test
    public void test2(){
      ListnumList = getNumList(10,() -> (int)(Math.random()*100));

      for (Integer integer:numList){
          System.out.println(integer);
      }
    }
    public ListgetNumList(int num,Suppliersup){
        Listlist = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            Integer n = sup.get();
            list.add(n);
        }
        return list;
    }

 Function函数型接口
 
 /**
     * Function函数型接口
     * R apply(T t)
     */
    //需求:用于处理字符串
    @Test
    public void test3(){
        //去除首尾空格
        String newStr = strHandler("  s  hang hai    ",(str) -> str.trim());
        System.out.println(newStr);
        //截取字符串
        System.out.println(strHandler("shanghai",(str) -> str.substring(2,5)));

    }
    public String strHandler(String str, Functionfun){
        return fun.apply(str);
    }

Predicate断言型接口

   /***
     * Predicate断言型接口
     *      boolean test(T t)
     */
    //需求;将满足条件的字符串,放入集合中
    @Test
    public void test4(){
        Listlist = Arrays.asList("hello","shanghai","songjiang","jdk8");
        ListstrList = filterStr(list,(s) -> s.length()>5);
        for (String string : strList){
            System.out.println(string);
        }
    }

    private ListfilterStr(Listlist, Predicatepre) {
        ListstrList = new ArrayList<>();
        for (String string : list){
            if (pre.test(string)){
                strList.add(string);
            }
        }
        return strList;
    }

方法引用

若Lambda体中的内容有方法已经实现了,可以使用"方法引用"(可以立即为方法引用是Lambda表达式的另一种表现形式)

1. 对象::实例方法名

 //对象 :: 实例方法名
    @Test
    public void test1(){
        Consumercon = (x) -> System.out.println(x);

        PrintStream ps = System.out;
        ps.println("帅");
        Consumercon1 = ps::println;
        con1.accept("shuai");
        Consumercon2 = System.out::println;
        con2.accept("hello");
    }


@Test
    public void test2(){
        Employee emp = new Employee();
        emp.setName("张三");
        emp.setAge(18);
        //获取名字
        Suppliersup = () -> emp.getName();
        String str = sup.get();
        System.out.println(str);
        //对象::实例方法
        Suppliersup2 = emp::getAge;
        int age = sup2.get();
        System.out.println(age);
    }

2. 类::静态方法名

//类::静态方法名
    @Test
    public void test3(){
     //   Comparatorcom = (x,y) -> Integer.compare(x,y);
        Comparatorcom1 = Integer::compare;
        System.out.println(com1.compare(8,9)); //-1
    }

3.类:实例方法名:

   //类::实例方法名
    @Test
    public void test4(){
        //BiPredicatebp = (x,y) -> x.equals(y);
        BiPredicatebp2 = String::equals;
       System.out.println( bp2.test("56","56"));
    }

构造器引用

 //构造器引用
    @Test
    public void test5() {
        // 构造器引用方式
        Suppliersup1  = Demo02::new;
        Demo02 demo02 = sup1.get();// new Demo02();
    }

方法引用

@Test
    public void test01(){
        System.out.println("hello");
        Consumercon2 = System.out::println;
        con2.accept("world");
    }

数组引用

@Test
    public void test6() {
        // 数组引用
        Functionfunc2 = String[]::new;
        String[] str2 = func2.apply(20);
        System.out.print(str2.length);
    }