首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往
您找到你想要的搜索结果了吗?
是的
没有找到

jdk8新特性之双冒号 :: 用法及详解

jdk8的新特性有很多,最亮眼的当属函数式编程的语法糖,本文主要讲解下双冒号::的用法。 概念 类名::方法名,相当于对这个方法闭包的引用,类似js中的一个function。比如: Function<String,String> func = String::toUpperCase; (Function在java.util.function包下,也是jdk8新加入的类,同级目录下有很多函数式编程模型接口,比如Consumer/Predicate/Operator等) func相当于一个入参和出参都为String的函数,可以直接 func.apply("abc") 接收一个参数,返回一个结果("ABC")。也可以用于代替下面的Lambda表达式: List<String> l = Arrays.asList("a","b","c"); l.stream().map(s -> s.toUpperCase()); l.stream().map(func); 下面自定义一个函数式接口 public class MyConsumer<String> implements Consumer<String> { @Override public void accept(String s) { System.out.println(s); } } 下面这俩种写法等价: List<String> l = Arrays.asList("a","b","c"); l.forEach(new MyConsumer<>()); l.forEach(s -> System.out.println(s)); 但是,这种写法却不行,编译失败: l.forEach(MyConsumer::accept); 因为MyConsumer的accept方法不是静态的,如果想使用这个方法,需要一个实例,还需要一个入参,共俩个参数。而List.forEach中需要的是consumer类型,相当于s -> {...},只有一个参数。 下面详细分析双冒号使用的各种情况 新建一个类,里面声明四个代表各种情况的方法: public class DoubleColon { public static void printStr(String str) { System.out.println("printStr : " + str); } public void toUpper(){ System.out.println("toUpper : " + this.toString()); } public void toLower(String str){ System.out.println("toLower : " + str); } public int toInt(String str){ System.out.println("toInt : " + str); return 1; } } 把它们用::提取为函数,再使用: Consumer<String> printStrConsumer = DoubleColon::printStr; printStrConsumer.accept("printStrConsumer"); Consumer<DoubleColon> toUpperConsumer = DoubleColon::toUpper; toUpperConsumer.accept(new DoubleColon()); BiConsumer<DoubleColon,String> toLowerConsumer = DoubleColon::toLower; toLowerConsumer.accept(new DoubleColon(),"toLowerConsumer"); BiFunction<DoubleColon,String,Integer> toIntFunction = DoubleColon::toInt; int i = toIntFunction.apply(new DoubleColon(),"toInt"); 非静态方法的第一个参数为被调用的对象,后面是入参。静态方法因为jvm已有对象,直接接收入参。 再写一个方法使用提取出来的函数: public class TestBiConsumer { public void test(BiConsumer<DoubleColon,String> consumer){ System.out.println("do s

01
领券