Java8函数式接口:简化代码,提升表达力

程序员他爱做梦 2024-02-24 02:25:37
在 Java 8 中,函数式接口是一个关键的特性,它们允许将方法作为参数传递或返回类型,从而提高代码的模块性和灵活性。下面是一些常见的函数式接口的实例代码。 1. ConsumerConsumer 接口代表一个接受单个输入参数且不返回结果的操作。它常用于对对象执行操作。 import java.util.function.Consumer;Consumer printConsumer = System.out::println;printConsumer.accept("Hello, World!"); 2. SupplierSupplier 接口提供一个没有参数的方法并返回一个泛型类型的结果。它通常用于延迟计算或构造。 import java.util.function.Supplier;Supplier randomSupplier = Math::random;System.out.println(randomSupplier.get()); 3. FunctionFunction 接口表示接受一个参数并产生结果的函数。这是一个非常通用的接口。 import java.util.function.Function;Function lengthFunction = String::length;System.out.println(lengthFunction.apply("Hello")); 4. PredicatePredicate 接口表示一个参数的布尔值函数。 import java.util.function.Predicate;Predicate nonEmptyStringPredicate = (String s) -> !s.isEmpty();System.out.println(nonEmptyStringPredicate.test("Test")); 5.使用 Function 进行函数组合Function times2 = e -> e * 2;Function squared = e -> e * e;Function times2AndSquare = times2.andThen(squared);System.out.println(times2AndSquare.apply(4)); // 输出 64 6.使用 Predicate 进行逻辑组合Predicate isEven = x -> x % 2 == 0;Predicate isPositive = x -> x > 0;Predicate isEvenAndPositive = isEven.and(isPositive);System.out.println(isEvenAndPositive.test(4)); // true 7. UnaryOperatorUnaryOperator 是 Function 的一个特例,用于操作单个操作数,其类型与结果类型相同。 import java.util.function.UnaryOperator;UnaryOperator square = x -> x * x;System.out.println(square.apply(5)); // 输出 25 8. BiFunctionBiFunction 接口表示接受两个参数并产生结果的函数。 import java.util.function.BiFunction;BiFunction add = (a, b) -> a + b;System.out.println(add.apply(2, 3)); // 输出 5 9. BinaryOperatorBinaryOperator 是 BiFunction 的一个特例,用于操作两个相同类型的操作数并返回相同类型的结果。 import java.util.function.BinaryOperator;BinaryOperator multiply = (a, b) -> a * b;System.out.println(multiply.apply(3, 4)); // 输出 12 10.使用 Function 创建工厂方法import java.util.HashMap;import java.util.Map;import java.util.function.Function;class ComplexClass { private int value; ComplexClass(int value) { this.value = value; } // Getter and other methods...}Function complexClassFactory = ComplexClass::new;ComplexClass complexInstance = complexClassFactory.apply(10); 11.UnaryOperator 创建连续操作UnaryOperator toUpperCase = String::toUpperCase;UnaryOperator addExclamation = str -> str + "!";UnaryOperator shout = toUpperCase.andThen(addExclamation);System.out.println(shout.apply("hello")); // 输出 "HELLO!" 12. BiConsumerBiConsumer 接口表示接受两个输入参数的操作,并且不返回任何结果。 import java.util.function.BiConsumer;BiConsumer concatAndPrint = (a, b) -> System.out.println(a + b);concatAndPrint.accept("Hello, ", "World!"); // 输出 Hello, World! 13. BiPredicateBiPredicate 接口表示接受两个参数的布尔值函数。 import java.util.function.BiPredicate;BiPredicate validate = (i, s) -> i > 0 && s.startsWith("A");System.out.println(validate.test(1, "Apple")); // 输出 true 14. ToIntFunctionToIntFunction 接口表示将一个对象转换为一个原始 int 类型的函数。 import java.util.function.ToIntFunction;ToIntFunction length = String::length;System.out.println(length.applyAsInt("Hello")); // 输出 5 通过这些特性,Java 8 的函数式接口极大地提升了代码的简洁性和可读性,同时也促进了函数式编程范式在 Java 社区中的普及。 作者:一只爱撸猫的程序猿链接:https://juejin.cn/post/7316797749518270491
0 阅读:6

程序员他爱做梦

简介:感谢大家的关注