六、构造器引用
Supplier<Dept> supplier1 = ()->new Dept();
System.out.println(supplier1.get());
Supplier<Dept> deptSupplier = Dept::new;
System.out.println("构造器引用:"+deptSupplier.get());
五、类::实例方法名
//类::实例方法名 ,使用规则,x是调用方,y是传参
BiPredicate<String,String> predicate = (x,y)->x.equals(y);
System.out.println(predicate.test("hua", "hua"));
BiPredicate<String,String> pre = String::equals;
System.out.println(pre.test("hua", "hua"));
四、对象::实例方法名
//函数式接口表达式 lambda方法1
java.util.function.Consumer con = (x) ->System.out.println(x);
con.accept(4);
//函数式接口表达式2 lambda方法2——对象::实例方法名
java.util.function.Consumer con2 = System.out::println;
con2.accept(5);
注意:lambda体中调用方法的参数列表与返回值类型,要与函数式接口中抽象方法的函数列表和返回值类型保持一致。
三、方法引用
方法引用的理解,将lambda基础语法改为方法引用,即将()-> 改为方法引用的办法。
二、基础语法
()->{}
一、使用lambda表达式,无需导入依赖,只需指定jdk编译版本为1.8,会自动支持lambda
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>