Java 8 in Action (3) -- Lambda的简单使用

来源:互联网 发布:嵌入式linux stm32 编辑:程序博客网 时间:2024/06/05 11:56

Lambda及其等效方法引用 ::

例子

Lambda 等效方法引用 (Apple a)->a.getWeight() Apple::getWeight ()->Thread.CurrentThread().dumpStack() Thread.currentThread()::dumpStack (str,i)->str.substring(i) String:: substring (String s)->System.out.pringln(s) System.out::println
方法引用主要有三类
1. 指向静态方法的引用

eg: Integer的parseInt方法,写作Integer::parseInt

2. 指向任意类型实例方法的方法引用

eg: String的length方法,写作String::length

3. 指向现有对象的实例方法的引用

eg: 假设你有一个局部变量aStudent用于存放Student类型的对象,它支持实例方法getAge,那么你就可以写成aStudent::getAge
ps: 第3条表示不是很懂
还有构造方法的引用 ClassName::new

0 0
原创粉丝点击