首页
学习
活动
专区
工具
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

Angular.js学习笔记(三)

1、uppercase,lowercase 大小写转换 {{ "lower cap string" | uppercase }} // 结果:LOWER CAP STRING {{ "TANK is GOOD" | lowercase }} // 结果:tank is good 2、date 格式化 {{1490161945000 | date:"yyyy-MM-dd HH:mm:ss"}} // 2017-03-22 13:52:25 3、number 格式化(保留小数) {{149016.1945000 | number:2}}//保留两位 {{149016.1945000 | number}}//默认为保留3位 4、currency货币格式化 {{ 250 | currency }} // 结果:$250.00 {{ 250 | currency:"RMB ¥ " }} // 结果:RMB ¥ 250.00 5、filter查找 输入过滤器可以通过一个管道字符(|)和一个过滤器添加到指令中,该过滤器后跟一个冒号和一个模型名称。 filter 过滤器从数组中选择一个子集 // 查找name为iphone的行 {{ [{"age": 20,"id": 10,"name": "iphone"}, {"age": 12,"id": 11,"name": "sunm xing"}, {"age": 44,"id": 12,"name": "test abc"} ] | filter:{'name':'iphone'} }} 同时filter可以自定义比较函数。 6、limitTo 截取 {{"1234567890" | limitTo :6}} // 从前面开始截取6位 {{"1234567890" | limitTo :6,6}} // 从第6位开始截取6位 {{"1234567890" | limitTo:-4}} // 从后面开始截取4位 7、orderBy 排序 // 根据id降序排 {{ [{"age": 20,"id": 10,"name": "iphone"}, {"age": 12,"id": 11,"name": "sunm xing"}, {"age": 44,"id": 12,"name": "test abc"} ] | orderBy:'id':true }}

02
领券