美文网首页
jdk新特性--方法引用

jdk新特性--方法引用

作者: 刘小刀tina | 来源:发表于2020-08-29 13:56 被阅读0次

1. 方法引用demo1


package com.tina.demolambda.demo0826;

import com.tina.demolambda.util.User;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.io.PrintStream;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Comparator;
import java.util.function.*;

/**
 * @program: demo-lambda
 * @description 方法的引用 :也是函数式接口的实例
 * <p>
 * 方法引用使用的要求:
 * 要求接口中的抽象方法的形参列表和返回值类型与发放引用的发放的形参列表和返回值类型相同
 * @author: tina.liu
 * @create: 2020-08-26 09:11
 **/

@Slf4j
public class Demo {


    /**
     * 情况一:  对象::实例发放
     * Consumer中的void accept(T t)
     * PrintStream中的void println(T t)
     */

    @Test
    public void test2() {
        User user = new User(1, "xiaokai");
        Supplier<String> sup1 = () -> user.getAccount();
        System.out.println(sup1.get());
        log.info("####################################");
        Supplier<String> sup2 = user::getAccount;
        System.out.println(sup2.get());


        Consumer<String> con1 = str -> System.out.println(str);
        con1.accept("北京");
        log.info("####################################");
        PrintStream ps = System.out;
        Consumer<String> con2 = ps::println;


    }


    /**
     * 情况二 类::静态方法
     * Comparator中的int compare(T t1, T t2)
     * Integer中的int compare(T t1, T t2)
     */
    @Test
    public void test1() {

        Comparator<Integer> com1 = (t1,t2) -> Integer.compare(t1,t2);
        //log.info("值为{}",com1.compare(2,3));

        Comparator<Integer> com2 = Integer::compare;
        log.info("值为{}",com1.compare(0,0));
    }


    /**
     * Function中的R apply(T t)
     * Math中的Long roud(Double d)  方法的引用
     *
     */
    @Test
    public void test3(){

        //1 第一种写法
        Function<Double,Long> function = new Function<Double, Long>() {
            @Override
            public Long apply(Double aDouble) {
                return Math.round(aDouble);
            }
        };

        // 2 第二种写法
        Function<Double,Long> function2 = aDouble -> Math.round(aDouble);

        //3 第三种写法

        Function<Double,Long> function3 = Math::round;
        log.info("值为{}",function3.apply(8.99));

    }


    /**
     *
     * 类::实例方法
     * Comparator 中的int compare(T1 t1,T2 t2)
     * String中的int t1.compareTo(t2)
     */
    @Test
    public void test4(){

        // 第一种写法
        Comparator<String> comparator = new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        };

        //第二种写法
        Comparator<String> comparator2 = ((o1, o2) -> o1.compareTo(o2));


        // 第三种写法
        Comparator<String> comparator3 = String::compareTo;

        log.info("值为{}",comparator3.compare("0","2"));


    }


    /**
     * BigPredicate中的boolean test(T t1,T t2)
     * String中的boolean  t1.equals(t2)
     *
     */
    @Test
    public void test5(){

        // 第一种写法
        BiPredicate<String,String> biPredicate = new BiPredicate() {
            @Override
            public boolean test(Object o, Object o2) {
                return o.equals(o2);
            }
        };
        //第二种写法
        BiPredicate<String,String> biPredicate2 = ((o, o2) -> o.equals(o2));
        // 第三种写法
        BiPredicate<String,String>  biPredicate3 = String::equals;
        log.info("返回的值为{}",biPredicate3.test("3","5"));

    }


    /**
     * Function中的R apply(T t)
     * Employee中的String getName()
     *
     */
    @Test
    public void test6(){

        User user = new User(1, "xiaokai");

        // 第一种写法
        Function<User,String> function = new Function<User, String>() {
            @Override
            public String apply(User user) {
                return user.getAccount();
            }
        };

        // 第二种写法
        Function<User,String> function2 = user1 ->  user.getAccount() ;


        //第三种写法
        Function<User,String> function3  = User::getAccount;

        log.info("返回值为{}",function3.apply(user));

    }

    ///////////////构造器方法的引用和,数组的引用////////////////


    /**
     *
     * 构造器的引用
     */
    @Test
    public void TEST1(){

        // 1 第一种方法

        Supplier<User> user = new Supplier<User>() {
            @Override
            public User get() {
                return new User();
            }
        };

        //2 第二种方法

        Supplier<User> user2 = ()->new User();


        //3 第三种方法

        Supplier<User> user3 = User::new ;

        User user1 = user3.get();
        log.info(" 返回值的为{}",user1);

    }


    // BiFunction 中的R apply(T t,U u)
    @Test
    public void test(){

        BiFunction<Integer,String,User> function1 = new BiFunction<Integer, String, User>() {
            @Override
            public User apply(Integer integer, String s) {
                User user = new User();
                user.setAccount(s);
                user.setId(integer);
                return user;
            }
        };
        //log.info("返回的值为{}",function1.apply(1,"tina"));

        BiFunction<Integer,String,User> function2 = (id,account) -> new User(id,account);
        log.info("返回的值为{}",function2.apply(1,"tina"));

        BiFunction<Integer,String,User> function = User::new;
        log.info("返回的值为{}",function.apply(1,"tina"));

    }


    /**
     * 数组的引用
     * Function中 R apply(T t)
     *
     */
    @Test
    public void TEST3(){
        Function<Integer,String[]> function = new Function<Integer, String[]>() {
            @Override
            public String[] apply(Integer integer) {
                return new String[integer];
            }
        };
        String[] arr = function.apply(5);
        //log.info("打印的值为{}", Arrays.toString(arr));


        Function<Integer,String[]> function1 = length -> new String[length];
        String[] arr1 = function1.apply(5);
        //log.info("打印的值为{}", Arrays.toString(arr1));

       Function<Integer,String[]> function3 = String[]::new ;
       String[] arr3 = function3.apply(10);
       log.info("打印的值为{}", Arrays.toString(arr3));
    }

}



1. 方法引用demo


 /**
     * 方法引用
     * 对象::静态方法名
     * 类::静态方法名
     * 类::实例方法名
     *
     */
    @Test
    public void test3(){


        //数组引用
        Function<Integer,String[]> function3 = (x) ->new String[x];
        Function<Integer,String[]> function4 = String[]::new ;
        

        //对象:: 静态方法名字
        Supplier<Student> supplier3 = ()->new Student() ;
        Supplier<Student> supplier4 = Student::new; //构造器引用 (具体调用哪个构造器,取决传入的参数)

        Function<Integer,Student> function1 = (x)-> new Student(x);
        Function<Integer,Student> function2 = Student::new;//构造器引用 (具体调用哪个构造器,取决传入的参数)



        //类::静态方法
        Comparator<Integer> comparator1 = (x,y) ->Integer.compare(x,y);
        Comparator<Integer> comparator2 = Integer::compareTo;


        //对象::实例方法名字
        Consumer con2 = System.out::println;
        Student student = new Student();
        Supplier getAge = student::getAge;

    }


相关文章

  • jdk新特性--方法引用

    1. 方法引用demo1 1. 方法引用demo

  • JDK新特性(三)——方法引用

    前言 JDK8在推出Lambda表达式的同时,还推出了方法引用这个概念,利用方法引用我们可以让我们的代码更加简洁,...

  • JDK8新特性 方法引用

    1. Lambda的冗余场景 首先可以直接调用getSum方法就可以完成这个操作了。 而方法引用更简洁 其实方法引...

  • jdk8新特性-方法引用

    简单实例,解决Lambda表达式的冗余 定义函数式接口 调用函数式接口 使用对象名引用成员变量 函数式接口 创建对...

  • Java8(JDK1.8)新特性

    一、Java8(JDK1.8)新特性 1、Lamdba表达式 2、函数式接口 3、方法引用和构造引用 4、Stre...

  • JDK新特性书目录

    JDK1.4新特性 JDK1.5新特性 JDK1.6新特性 JDK1.7新特性 JDK1.8新特性

  • JDK1.8 新特性

    JDK1.8 新特性 Lambda表达式函数式接口*方法引用和构造器调用Stream API接口中的默认方法和静态...

  • JDK8新特性之方法引用

    什么是方法引用 方法引用是只需要使用方法的名字,而具体调用交给函数式接口,需要和Lambda表达式配合使用。 如:...

  • java stream使用指南-------sorted使用及进

    引入 用了一段时间的jdk8的新特性,lambda表达式、方法引用、stream流,用起来是真的顺手啊,最近碰到了...

  • jdk13特性

    JDK 13 新特性讲解课程 第一章 JDK 13新特性介绍 1.1 JDK 各版本主要特性回顾 JDK Vers...

网友评论

      本文标题:jdk新特性--方法引用

      本文链接:https://www.haomeiwen.com/subject/pnzdsktx.html