代码:
package com.jay.ct; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.Iterator; import java.util.List; import java.util.function.BiConsumer; import java.util.function.BiFunction; import java.util.function.BiPredicate; import java.util.function.BinaryOperator; import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Predicate; import java.util.function.Supplier; import java.util.function.ToDoubleFunction; import java.util.function.ToIntFunction; import java.util.function.ToLongFunction; import java.util.function.UnaryOperator; import org.junit.jupiter.api.Test; public class LambdaTest { public static void main(String[] args) throws Exception { // lambda表达式的本质:作为函数式接口的实例 // 如果一个接口中只声明了一个抽象方法,则此接口为函数式接口 @FunctionInterface // 四大核心函数式接口 // 消费型:void Consumer<T> 包含方法:accept(t) // 供给型:T Supplier<T> 包含方法:get() // 函数型:R Function<T,R> 包含方法:apply(t) // 断定型:boolean Predicate<T> 包含方法:test(t) // 其他:BiFunction<T,U,R>,UnaryOperator<T>,BinaryOperator<T>, // BiConsumer<T, U>,BiPredicate<T, U>, // ToIntFunction<T>,ToDoubleFunction<T>,ToLongFunction<T> // IntFunction<R>,DoubleFunction<R>,LongFunction<R> // 方法引用:当要传递给Lambda体的操作,已经有实现的方法了,可以使用方法引用 // 方法引用可以看做是Lambda表达式深层次的表达,可以认为是Lambda表达式的一个语法糖。 // 方法引用要求:实现接口的抽象方法的参数列表和返回值类型,必须与方法引用的方法的参数列表和返回值类型保持一致 // 主要使用方式:对象::实例方法名 类::静态方法名 类::实例方法名 // 1.无参,无返回值 Runnable r1 = new Runnable() { @Override public void run() { System.out.println("a"); } }; r1.run(); Runnable r2 = () -> System.out.println("b"); r2.run(); // 2.需要一个参数,无返回值 Consumer<String> consumer1 = new Consumer<>() { @Override public void accept(String t) { System.out.println(t); } }; consumer1.accept("c"); Consumer<String> consumer2 = (t) -> System.out.println(t); consumer2.accept("d"); // 3.需要多个参数,有返回值 Comparator<Integer> comp1 = new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return o1.compareTo(o2); } }; int res1 = comp1.compare(1, 2); System.out.println("compare1:" + res1); Comparator<Integer> comp2 = (o1, o2) -> o1.compareTo(o2); var res2 = comp2.compare(50, 20); System.out.println("compare2:" + res2); // 4.boolean Predicate<T> test(t) List<String> listCity = Arrays.asList("北京", "上海", "南京"); List<String> list1 = filterStrings(listCity, (t) -> t.contains("京")); // list1.forEach((t) -> System.out.println(t)); list1.forEach(System.out::println);// 方法引用 // 5.方法引用,getJuice的参数和返回值,正好匹配Function<String, String> Function<String, String> func1 = new Function<String, String>() { @Override public String apply(String t) { return t + "被榨成了汁"; } }; String resJuice1 = func1.apply("西瓜"); System.out.println(resJuice1); Function<String, String> func2 = (t) -> t + "被榨成了汁"; String resJuice2 = func2.apply("黄瓜"); System.out.println(resJuice2); // 方法引用 类::静态方法 Function<String, String> func3 = LambdaTest::getJuice; // apply传参给getJuice,得到返回值 String resJuice3 = func3.apply("苹果"); System.out.println(resJuice3); } @Test public void test1() { // 构造器引用 Supplier<LambdaTest> lt = LambdaTest::new; // 对象::非静态方法 Function<String, String> func1 = lt.get()::getJuice2; // apply传参给getJuice,得到返回值 String resJuice1 = func1.apply("苹果"); System.out.println(resJuice1); // 类::非静态方法,符合 h1.beFriend(a1) 或者 h1.getName() 这样的。 Boy h1 = new Boy("jay"); Gril a1 = new Gril("luna"); BiFunction<Boy, Gril, String> func3 = Boy::beFriend; String res3 = func3.apply(h1, a1); System.out.println(res3); Boy h2 = new Boy("jay"); Function<Boy, String> func4 = Boy::getName; String res4 = func4.apply(h2); System.out.println(res4); } public static List<String> filterStrings(List<String> list, Predicate<String> pre) { ArrayList<String> filterList = new ArrayList(); for (var s : list) { if (pre.test(s)) { filterList.add(s); } } return filterList; } public static String getJuice(String fruitName) { return fruitName + "被榨成了汁"; } public String getJuice2(String fruitName) { return fruitName + "被榨成了汁"; } } class Boy { public Boy(String name) { super(); this.name = name; } public String name; public String beFriend(Gril p) { return this.name + "和" + p.name + "成为了好朋友"; } public String getName() { return this.name; } } class Gril { public Gril(String name) { super(); this.name = name; } public String name; }