1. 方法引用
jdk8中使用了::的用法。就是把方法当做参数传到stream内部,使stream的每个元素都传入到该方法里面执行一下,双冒号运算就是Java中的[方法引用],[方法引用]的格式是
方法引用:若Lamdba体中的内容已经有方法已经实现了,我们可以使用方法引用
三种类型:对象::实例方法名 ,类::静态方法名,类::实例方法名
| 类型 | 语法 | 对应的Lambda表达式 |
|---|---|---|
| 静态方法引用 | 类名::staticMethod | (args) -> 类名.staticMethod(args) |
| 实例方法引用 | inst::instMethod | (args) -> inst.instMethod(args) |
| 对象方法引用 | 类名::instMethod | (inst,args) -> 类名.instMethod(args) |
| 构建方法引用 | 类名::new | (args) -> new 类名(args) |
person -> person.getAge();Person::getAgenew HashMap<>()//等价于HsahMap :: new
