java中常见的函数式接口及简单示例

在Java中,有一些常见的函数式接口可以用于支持函数式编程和Lambda表达式的使用。以下是一些常见的函数式接口:

Predicate:用于判断输入的值是否满足某个条件。它包含方法test,接收一个参数并返回一个布尔值。

Function:用于将一个输入值映射为一个输出值。它包含方法apply,接收一个参数并返回一个结果。

Consumer:用于执行某个操作并消耗输入值,没有返回值。它包含方法accept,接收一个参数但不返回任何结果。

Supplier:用于提供一个值或对象。它不接收任何参数,但会返回一个值或对象。

UnaryOperator:用于对单个输入进行操作,并产生相同类型的输出。它继承自Function接口,接收一个参数并返回一个相同类型的结果。

BinaryOperator:用于对两个输入进行操作,并产生相同类型的输出。它继承自BiFunction接口,接收两个参数并返回一个相同类型的结果。

BiFunction:用于将两个输入值映射为一个输出值。它包含方法apply,接收两个参数并返回一个结果。

Runnable:用于定义执行的代码块,没有输入参数和返回值。它包含方法run,用于执行代码块。

Comparator:用于比较两个对象的顺序。它包含方法compare,接收两个参数并返回一个表示顺序的整数值。

各个函数接口的简单示例如下:

1 Predicate

用于判断输入的值是否满足某个条件。

@FunctionalInterface
interface Predicate<T> {
    boolean test(T t);
}

// 示例:判断一个整数是否为正数
Predicate<Integer> isPositive = (num) -> num > 0;
System.out.println(isPositive.test(5));  // 输出:true
System.out.println(isPositive.test(-2)); // 输出:false

2 Function

用于将一个输入值映射为一个输出值。

@FunctionalInterface
interface Function<T, R> {
    R apply(T t);
}

// 示例:将字符串转换为整数
Function<String, Integer> stringToInt = Integer::parseInt;
System.out.println(stringToInt.apply("123")); // 输出:123

Consumer

用于执行某个操作并消耗输入值,没有返回值。

@FunctionalInterface
interface Consumer<T> {
    void accept(T t);
}

// 示例:打印字符串长度
Consumer<String> printLength = str -> System.out.println(str.length());
printLength.accept("Hello"); // 输出:5

Supplier

用于提供一个值或对象。

@FunctionalInterface
interface Supplier<T> {
    T get();
}

// 示例:生成随机数
Supplier<Integer> randomGenerator = () -> (int) (Math.random() * 100);
System.out.println(randomGenerator.get()); // 输出:随机生成的一个整数

5 UnaryOperator

对单个输入进行操作,并产生相同类型的输出。

@FunctionalInterface
interface UnaryOperator<T> extends Function<T, T> {
    // 继承自Function接口,省略了apply方法的声明
}

// 示例:对整数进行平方操作
UnaryOperator<Integer> square = num -> num * num;
System.out.println(square.apply(5)); // 输出:25

6 BinaryOperator

对两个输入进行操作,并产生相同类型的输出。

@FunctionalInterface
interface BinaryOperator<T> extends BiFunction<T,T,T> {
    // 继承自BiFunction接口,省略了apply方法的声明
}

// 示例:对两个整数求最大值
BinaryOperator<Integer> max = (a, b) -> a > b ? a : b;
System.out.println(max.apply(5, 3)); // 输出:5

BiConsumer

对两个输入值执行某个操作,没有返回值。

@FunctionalInterface
interface BiConsumer<T, U> {
    void accept(T t, U u);
}

// 示例:打印两个整数的和
BiConsumer<Integer, Integer> printSum = (a, b) -> System.out.println(a + b);
printSum.accept(2, 3); // 输出:5

BiFunction

将两个输入值映射为一个输出值。

@FunctionalInterface
interface BiFunction<T, U, R> {
    R apply(T t, U u);
}

// 示例:将两个字符串拼接起来
BiFunction<String, String, String> concatenate = (str1, str2) -> str1 + str2;
System.out.println(concatenate.apply("Hello, ", "World!")); // 输出:Hello, World!

9 IntPredicate

用于判断输入的整数是否满足某个条件。

@FunctionalInterface
interface IntPredicate {
    boolean test(int value);
}

// 示例:判断一个整数是否为偶数
IntPredicate isEven = num -> num % 2 == 0;
System.out.println(isEven.test(4));  // 输出:true
System.out.println(isEven.test(7));  // 输出:false

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值