Java基础——Java8新特性

一、Lambda表达式

前提:是函数式接口(只有一个抽象方法)@FunctionalInterface

写法:拷贝小括号(),写死右箭头->,落地大括号{...}

本质:作为函数式接口的实例。

举例:(o1,o2) -> Integer.compare(o1,o2);

package cn.wyu.lambda;

import org.junit.Test;

import java.util.Comparator;
import java.util.function.Consumer;

/**
 * @author linwillen
 * @create 2020-04-26-18:44
 */
public class LambdaTest {

    //语法格式一:无参,无返回值
    @Test
    public void test01(){
        Runnable r1 = new Runnable() {
            @Override
            public void run() {
                System.out.println("我爱Java。。。");
            }
        };
        r1.run();
        System.out.println("***************");
        Runnable r2 = () -> System.out.println("我爱Java。。。");
        r2.run();
    }
    //语法格式二:lambda有一个参数,无返回值
    @Test
    public void test2(){
        Consumer<String> con1 = new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        };
        con1.accept("我爱Spring。。。");
        System.out.println("***************");

        Consumer<String> con2 = (s) -> {
            System.out.println(s);
        };
        con2.accept("我爱SpringBoot。。。");
    }

    @Test
    public void test3(){
        Comparator<Integer> com1 = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                System.out.println(o1);
                System.out.println(o2);
                return o1.compareTo(o2);
            }
        };
        System.out.println(com1.compare(2,6));
        System.out.println("***************");
        Comparator<Integer> com2 = (o1, o2) -> {
            System.out.println(o1);
            System.out.println(o2);
            return o1.compareTo(o2);
        };
        System.out.println(com2.compare(6, 2));
    }
    //语法格式三:lambda体只有一条语句时,若return和大括号都有,都可以省略
    @Test
    public void test4(){
        Comparator<Integer> com1 = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1.compareTo(o2);
            }
        };
        System.out.println(com1.compare(2,6));//-1
        System.out.println("***************");
        Comparator<Integer> com2 = (o1, o2) -> o1.compareTo(o2);
        System.out.println(com2.compare(6, 2));//1
    }
}

总结:

->左边:如果lambda表达式左边的形参列表只有一个参数时,其数据类型和()可以省略

->右边:lambda表达式的右边应该用{}包裹,如果右边只有一条执行语句(可能是return语句)的时候,省略return和这一对{}

二、方法引用

import org.junit.Test;

import java.io.PrintStream;
import java.util.Comparator;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * 方法引用:三种使用情况
 * 对象::实例方法
 * 类::静态方法
 * 
 * 类::非静态方法
 *
 * @author linwillen
 * @create 2020-06-14-23:17
 */
public class MethodRefTest {

    //对象::实例方法
    //Consumer中的void accept(T t);
    //PrintStream中的void println(T x);
    @Test
    public void test01(){
        Consumer<String> con1 = str -> System.out.println("北京");
        con1.accept("北京");

        System.out.println("================");

        PrintStream ps = System.out;
        Consumer<String> con2 = ps::println;
        //Consumer<String> con2 = System.out::println;
        con2.accept("beijing");
    }

    //对象::实例方法
    //Supplier中的T get();
    //Employee中的T getName();
    @Test
    public void test02(){
        Employee emp = new Employee(1001,"zhangsna",0,2.1);

        Supplier<String> sup1 = () -> emp.getName();
        System.out.println(sup1.get());

        System.out.println("================");

        Supplier<String> sup2 = emp::getName;
        System.out.println(sup2.get());
    }

    //类::静态方法
    //Comparator中的int compare(T o1, T o2);
    //Integer中的static int compare(T x, T y);
    @Test
    public void test03(){
        Comparator<Integer> com1 = (t1,t2)->Integer.compare(t1,t2);
        System.out.println(com1.compare(16, 76));

        System.out.println("================");

        Comparator<Integer> com2 = Integer::compare;
        System.out.println(com2.compare(9, 2));
    }

    //类::静态方法
    //Function中的R apply(T t);
    //Math中的static long round(double a);
    @Test
    public void test04(){
        Function<Double,Long> fun1 = d -> Math.round(d);
        System.out.println(fun1.apply(12.7));

        System.out.println("================");

        Function<Double,Long> fun2 = Math::round;
        System.out.println(fun2.apply(12.1));
    }

    //类::非静态方法
    //Comparator中的int compare(T o1, T o2);
    //String中的int o1.compareTo(o2);
    @Test
    public void test05(){
        Comparator<String> com1 = (s1,s2)->s1.compareTo(s2);
        System.out.println(com1.compare("abc", "abd"));

        System.out.println("================");

        Comparator<String> com2 = String::compareTo;
        System.out.println(com2.compare("abc", "abc"));
    }


    @Test
    public void test06(){
        BiPredicate<String,String> pre1 = (s1,s2)->s1.equals(s2);
        System.out.println(pre1.test("abc", "abc"));

        System.out.println("================");

        BiPredicate<String,String> pre2 = String::equals;
        System.out.println(pre2.test("abc", "abf"));

    }

    //Function中的R apply(T t);
    //Employee中的String getName();
    @Test
    public void test07(){
        Employee employee = new Employee(2,"jgoag",2,25.5);

        Function<Employee,String> fun1 = e->e.getName();
        System.out.println(fun1.apply(employee));
        
        System.out.println("================");

        Function<Employee,String> fun2 = Employee::getName;
        System.out.println(fun2.apply(employee));

    }
}

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值