JAVA lambda表达式方法引用+构造器引用

若Lambda体中的内容有方法已经实现了,使用“方法引用”

注意:Lambda体中调用方法的参数列表与返回值类型,要与函数式接口中抽象方法的函数列表和返回值类型保持一致。

静态方法与实例方法的区别

1、静态方法属于整个类所有,因此调用它不需要实例化,可以直接调用(类.静态方法())。实例方法必须先实例化,创建一个对象,才能进行调用(对象.实例方法())。
2、静态方法只能访问静态成员,不能访问实例成员;而实例方法可以访问静态成员和实例成员。
3、在程序运行期间,静态方法是一直存放在内存中,因此调用速度快,但是却占用内存。实例方法是使用完成后由回收机制自动进行回收,下次再使用必须再实例化。
4、一般来说,公共的函数、经常调用的可以写成静态方法,比如数据连接等(SqlHelper)

原文链接:https://blog.csdn.net/g6485644/article/details/85157913

对象::实例方法名

    public static void test01(){
        PrintStream ps1 = System.out;
        Consumer<String> str = (x)->ps1.println(x);
        PrintStream ps2 = System.out;
        Consumer<String> str1 = ps2::println;
        Consumer<String> str2 = System.out::println;
        str.accept("abcdefg");
        str1.accept("abcdefg");
        str2.accept("abcdefg");
    }
    public static void test02(){
        employee emp1 = new employee("zzz",23,20000);
        Supplier<String> sp = ()->emp1.getName();
        String s = sp.get();
        System.out.println(s);

        Supplier<Integer> sp1 = emp1::getAge;
        System.out.println(sp1.get());
    }

类::静态方法名

        Comparator<Integer> com = (x,y)->Integer.compare(x,y);
        Comparator<Integer> com1 = Integer::compare;

类::实例方法名

   BiPredicate<String, String> bp0 = new BiPredicate<>() {
            
            public boolean test(String s, String s2) {
                return s.equals(s2);
            }
        };
        BiPredicate<String,String> bp1 = (x,y)->x.equals(y);
        BiPredicate<String,String> bp2 = String::equals;

注意:若Lambda参数列表中的第一个参数是实例方法的调用者,而第二个参数是实例方法的参数时,可以使用 类名::方法名

================================

构造器引用

格式:类名::new
注意:需要调用的构造器的参数列表要与函数式接口中抽象方法的参数列表保持一致

        Supplier<employee> sup = ()->new employee();
		//匹配无参构造
        Supplier<employee> sup1 = employee::new;
        employee emp = sup1.get();
        System.out.println(emp);
    //employee类中有一个参数的构造函数
    //public employee(int id){
        //this.id = id;
    //}
    public static void test06(){
        Function<Integer, employee> f0 = new Function<>() {
            
            public employee apply(Integer integer) {
                employee emp = new employee(integer);
                return emp;
            }
        };
        System.out.println(f0.apply(1));
        Function<Integer,employee> f1 = x->new employee(x);
        System.out.println(f1.apply(2));
        Function<Integer,employee> f2 = employee::new;
        System.out.println(f2.apply(3));
    }

================================

数组引用

格式:类型::new

    public static void test07(){
        Function<Integer, String[]> f1 = new Function<>() {
            
            public String[] apply(Integer integer) {
                return new String[integer];
            }
        };
        System.out.println(f1.apply(10).length);
        Function<Integer,String[]> f2 = x->new String[x];
        System.out.println(f2.apply(9).length);
        Function<Integer,String[]> f3 = String[]::new;
        System.out.println(f3.apply(8).length);
    }
  • 15
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值