(parameters) -> expression
或(parameters) ->{ statements; }
"->"
,该操作符称为箭头操作符或者Lambda操作符,箭头操作符将Lambda表达式拆分成两部分;使用匿名内部类:
Comparatorcom = new Comparator() {
@Override
public int compare(Integer o1, Integer o2) { //降序排列
return Integer.compare(o2,o1);
}
};
使用Lambda表达式
Comparator com = (x, y) -> Integer.compare(y, x);
Runnable run = new Runnable() {
@Override
public void run() {
System.out.println("hello");
}
}
// lambda形式
Runnable runnable = () -> System.out.println("hello");
// lambda形式
Consumer con = (x) -> System.out.println(x);
con.accept("hello");
Comparator comparator = (x,y) -> {
System.out.println("函数式接口");
return Integer.compare(x,y);
};
Integer[] nums = {4,2,8,1,5};
Arrays.sort(nums,com);
System.out.println(Arrays.toString(nums));
Comparator comparator = (x,y) -> Integer.compare(x,y);
Integer[] nums = {4,2,8,1,5};
Arrays.sort(nums,com);
System.out.println(Arrays.toString(nums));
(Integer x,Integer y ) -> Integer.compare(x,y)
可以简写成(x,y) -> Integer.compare(x,y)
;@FunctionlInterface
来标识,可以检查是否为函数式接口; @FunctionalInterface
public interface MyFunction {
String getValue(String str);
}
如果使用Lambda还要自己写一个接口的话太麻烦,所以Java自己提供了一些接口:
Consumer< T >con
消费性接口: void accept(T t)
;Supplier< T >sup
供给型接口 : T get()
;Function< T , R >fun
函数式接口 : R apply (T t)
;Predicate< T >
断言形接口 : boolean test(T t)
;Consumer< T >con
消费性接口有参数,没有返回值
@Test
public void test(){
this.happy(1000,(m) -> System.out.println("消费" + m + "元"));
}
private void happy(double money, Consumer consumer){
consumer.accept(money);
}
Supplier< T >sup
供给型接口无参数,有返回值
需求:产生指定个数随机数放入集合中
@Test
public void test(){
List list = this.getNumList(10,() -> (int)(Math.random() * 100));
// 遍历,lambda消费性写法
list.forEach((num) -> System.out.println(num));
/*for (Integer num : list) {
System.out.println(integer);
}*/
}
private List getNumList(int num, Supplier supplier){
List list = new ArrayList<>(num);
for (int i = 0; i < num; i++) {
Integer n = supplier.get();
list.add(n);
}
return list;
}
Function< T , R >fun
函数式接口有参数,有返回值
需求:字符串去空格
@Test
public void test3(){
String str = this.strHandler(" xyn ", s -> s.trim());
System.out.println(str);
}
private String strHandler(String str, Function function){
return function.apply(str);
}
Predicate< T >
断言形接口有参数,有返回值(布尔型)
需求:取出长度大于3的字符串放入新集合
@Test
public void test4(){
List list = Arrays.asList("xyn", "zyn", "abcde", "a", "bb");
list = this.filterStr(list, s -> s.length() > 3);
list.forEach(action -> System.out.println(action));
}
private List filterStr(List strList, Predicate predicate){
List list = new ArrayList<>(strList.size());
strList.forEach(action -> {
if(predicate.test(action)){
list.add(action);
}
});
return list;
}
使用前提: Lambda体中调用方法的参数列表和返回值类型,要和函数式接口中抽象方法的参数列表和返回值类型保持一致;
PrintStream ps = System.out;
Consumer con = x -> ps.println(x);
con.accept("xyn");
// 简写
Consumer con1 = ps::println;
con1.accept("xyn");
// 更加简写
Consumer con2 = System.out::println;
con2.accept("zyn");
实例2
@Test
public void test(){
User user = new User("zyn",20);
Supplier sup = () -> user.getName();
String name = sup.get();
System.out.println(name);
// 简写
Supplier sup2 = user::getAge;
Integer age = sup2.get();
System.out.println(age);
}
@Test
public void test(){
Comparator com = (x, y) -> Integer.compare(x, y);
Comparator com2 = Integer::compare;
}
@Test
public void test(){
BiPredicate biPredicate = (x, y) -> x.equals(y);
System.out.println(biPredicate.test("xyn","xyn"));
BiPredicate biPredicate2 = String::equals;
System.out.println(biPredicate2.test("zyn","zyn"));
}
语法:ClassName::new
注意:需要调用构造器的参数列表,要与函数式接口中的抽象方法的参数列表保持一致;
@Test
public void test5(){
Supplier supplier = () -> new User("xyn",20);
// 构造器引用方式(调用无参构造)
Supplier supplier2 = User::new;
BiFunction biFunction = (x, y) -> new User(x,y);
biFunction.apply("zyn",20);
// 构造器引用方式(调用有参构造)
BiFunction biFunction2 = User::new;
biFunction2.apply("zyn",20);
}
语法:Type[]::new
@Test
public void test(){
Function function = x -> new String[x];
System.out.println(function.apply(10).length);
// 数组引用方式
Function function2 = String[]::new;
System.out.println(function.apply(10).length);
}
下一篇:Java8新特性之Stream流(二)
参考:https://blog.csdn.net/zxzxzx0119/article/details/82392396
视频:https://www.bilibili.com/video/av35195879