必知必会之Lambda表达式 (2)

R apply(T t);

// 处理字符串 String str1 = functionDemo("Hello!风尘博客", s -> s.substring(6)); System.out.println(str1); String str2 = functionDemo("vanDusty", s -> s.toUpperCase()); System.out.println(str2); public static String functionDemo(String str, Function<String, String> function) { return function.apply(str); }

断言型接口

boolean test(T t);

// 将满足条件的字符串放入集合 List<String> list = Arrays.asList("hello", "van", "function", "predicate"); List<String> newList = predicateDemo(list, s -> s.length() > 5); System.out.println(newList); public static List<String> predicateDemo(List<String> list, Predicate<String> predicate) { List<String> newList = new ArrayList<>(); for (String s : list) { if (predicate.test(s)) { newList.add(s); } } return newList; } 2.2 自定义函数式接口

我们可以在任意函数式接口上使用 @FunctionalInterface 注解, 这样做可以检查它是否是一个函数式接口,同时 javadoc 也会包含一条声明,说明这个接口是一个函数式接口。

// 字符串转大写 String newStr = selfFunctionalInterface((str) -> str.toUpperCase(), "abc"); System.out.println(newStr); public static String selfFunctionalInterface(SelfFunctionalInterface<String> selfFunctionalInterface, String str) { return selfFunctionalInterface.getValue(str); } 三、方法引用和构造器引用 3.1 方法引用

方法引用是指通过方法的名字来指向一个方法。

3.1.1 方法引用使用的前提条件是什么呢?

方法引用所引用的方法的参数列表必须要和函数式接口中抽象方法的参数列表相同(完全一致);

方法引用所引用的方法的的返回值必须要和函数式接口中抽象方法的返回值相同(完全一致)。

3.1.2 方法引用三种格式

实例对象名::实例方法名

private static void instanceMethod() { UserDomain user = new UserDomain(1L, "Van"); Supplier<String> sup = () -> user.getUserName(); System.out.println(sup.get()); // 等同于 Supplier<String> supplier = user::getUserName; System.out.println(supplier.get()); }

类名::静态方法名

private static void staticMethod() { Comparator<Integer> com = (x, y) -> Integer.compare(x, y); System.out.println(com.compare(3,9)); // 等同于 Comparator<Integer> com2 = Integer::compare; System.out.println(com2.compare(3,9)); }

类名::实例方法名

private static void instanceMethodObject() { UserDomain user = new UserDomain(1L, "Van"); Function<UserDomain, String> fun = (e) -> e.getUserName(); System.out.println(fun.apply(user)); // 等同于 Function<UserDomain, String> fun2 = UserDomain::getUserName; System.out.println(fun2.apply(user)); } 3.2 构造器引用

前提:构造器参数列表要与接口中抽象方法的参数列表一致!

语法格式:类名 :: new

构造器引用

private static void object() { // UserDomain 中必须有一个 UserDomain(String userName) 的构造器,下同 Function<String,UserDomain> fun = (n) -> new UserDomain(n); fun.apply("Van"); System.out.println("===等价于==="); Function<String,UserDomain> function = UserDomain::new; function.apply("Van"); // 带两个参数的构造器引用就要用BiFunction,多个参数的话,还可以自定义一个这样的函数式接口 BiConsumer<Long, String> biConsumer = UserDomain :: new; biConsumer.accept(1L,"Van"); }

数组引用

private static void array() { //传统Lambda实现 Function<Integer,int[]> function = (i) -> new int[i]; int[] apply = function.apply(10); System.out.println(apply.length); //数组类型引用实现 function = int[] ::new; apply = function.apply(100); System.out.println(apply.length); } 四、 总结

Github 示例代码

Lambda表达式是Java对于函数式编程的温和转变,面向对象编程和函数式编程不是互相对立的,结合使用能够更加有效地帮助我们管理程序的复杂性。

技术交流

风尘博客

风尘博客-掘金

风尘博客-博客园

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zyggdj.html