
刚刚学习完 java 我想分享所有默认函数接口! 函数接口是一种抽象接口。假如你要处理 lambda 它们是必要的表达式(函数编程)。它们简化了代码,并广泛应用于流中。虽然你可以创建自己的函数接口,但当 java 它为我们提供了一些重要的接口(例如 consumer、predicate、function 为什么还要担心supplier?
1. 消费者:consumer 它是一个函数接口,表示接受单个输入参数而不返回结果。它通常用于不修改给定参数的操作(如打印或记录)。
签名:void accept(t t)(其中 t 它是一种通用类型)
2. 谓词:predicate 它是一个函数接口,表示返回布尔值的单个参数函数。它通常用于过滤或评估条件(例如,检查数字是否为偶数)。
签名:布尔测试(t t)
立即学习“Java免费学习笔记(深入);
3、功能:function 它是一个函数接口,表示接受参数并产生结果的函数。它通常用于转换(例如,将一种类型转换为另一种类型或修改数据)。
签名:r apply(t t)
4. 供应商:supplier 它是一个函数接口,表示一个没有输入参数并返回结果的函数。它通常用于无需输入即可生成或提供值。
签名:t get()
函数接口可以通过定义接受这些接口作为参数的通用方法来有效地使用,例如 consumer、predicate、function 和supplier。这使我们能够使用泛型的强大功能,并确保我们的方法适用于各种类型。
这是一个完整代码的示例,演示了所有功能
import java.util.List;
import java.util.Random;
import java.util.function.*;
public class Main {
public static void main(String[] args) {
// Consumer
usingConsumer((a) -> System.out.printf("Hello %s", a), "saami");
System.out.println();
// Bi-Consumer
usingBiConsumer((a, b) -> System.out.printf("Name: %s, Age: %d", a, b), "saami", 20);
System.out.println();
// Predicate
var result1 = usingPredicate((a) -> a % 2 == 0, 34);
if (result1) {
System.out.println("Even");
} else {
System.out.println("Odd");
}
// Bi-Predicate
var result2 = usingBiPredicate((a, b) -> a > b, 12, 22);
if (result2) {
System.out.println("Greater");
} else {
System.out.println("Lesser");
}
// Function
var result3 = usingFunction((a) -> a + ": this is a number", 5);
System.out.println(result3);
// Bi-Function
var result4 = usingBiFunction((a, b) -> (a > b ? "Greater": "Lesser"), 5, 6);
System.out.println(result4);
// Unary-Operator
var result5 = usingUnaryOperator((a) -> a+5, 10);
System.out.println(result5);
// Binary-Operator
var result6 = usingBinaryOperator((a, b) -> a + b, 12, 32);
System.out.println(result6);
Random r = new Random();
// Function as Predicate
var result7 = usingFunctionAsPredicate((a) -> a > 99, 999);
System.out.println(result7);
// Using Consumer for printing of the list.
printData((a) -> {
for (var ele : a) {
System.out.println(ele);
}
} , List.of("S1", "S2", "S3", "S4", "S5"));
// Using Supplier as a random number generator
String[] arr = {"saami", "john", "raymond", "puff"};
System.out.println(getRandomOne(arr, () -> new Random().nextInt(arr.length)));
// Using Custom Comparator
System.out.println(usingCustomFunctionalInterface((a, b, c) -> a + b + c, "Saami", " Abbas", " Khan"));
}
public static <t> void usingConsumer(Consumer<t> consumer, T a) {
// Method that takes consumer interface will return void.
// Can print something constituting 'a'
consumer.accept(a);
}
public static <t l> void usingBiConsumer(BiConsumer<t l> biConsumer, T a, L b) {
biConsumer.accept(a, b);
}
public static <t> boolean usingPredicate(Predicate<t> predicate, T a) {
return predicate.test(a);
}
public static <t l> boolean usingBiPredicate(BiPredicate<t l> biPredicate, T a, L b) {
return biPredicate.test(a, b);
}
public static <t r> R usingFunction(Function<t r> function, T a) {
// T for the parameter and R for the return type here the return type could be as same as T or
// could be different like if T is Integer the R could be String 8 + ""
return function.apply(a);
}
public static <t u r> R usingBiFunction(BiFunction<t u r> biFunction, T a, U b) {
return biFunction.apply(a, b);
}
public static <t> T usingUnaryOperator(UnaryOperator<t> unaryOperator, T a) {
return unaryOperator.apply(a);
}
public static <t> T usingBinaryOperator(BinaryOperator<t> binaryOperator, T a, T b) {
return binaryOperator.apply(a, b);
}
public static <t r> R usingFunctionAsPredicate(Function<t r> prediFunction, T a) {
return prediFunction.apply(a);
}
public static <t> void printData(Consumer<t> consumer, T a) {
/*
* Prints the data, (List.of()) using a for loop inside of lambda function.
*/
consumer.accept(a);
}
public static String getRandomOne(String[] arr, Supplier<integer> supplier) {
return arr[supplier.get()];
}
@FunctionalInterface
interface Concat<t> {
T concat(T a, T b, T c);
}
public static <t> T usingCustomFunctionalInterface(Concat<t> concat, T a, T b, T c) {
return concat.concat(a, b, c);
}
}
</t></t></t></integer></t></t></t></t></t></t></t></t></t></t></t></t></t></t></t></t></t></t></t></t>最终判决
java 函数接口是简化代码和提高可读性的强大工具。这些接口可以让您更容易地定义简单的操作,无论您是处理集合、执行转换还是处理数据流。
通过理解和应用 consumer、predicate、function、supplier 您可以充分利用自定义接口等函数接口 java 函数编程功能。
以上是Java 更多关于图灵教育的其他相关文章,请关注默认函数接口的详细内容!