Java常用函数接口

Java常用函数接口

Java 8 中引入的常用函数式接口,也就是 java.util.function 包中的接口。这些接口提供了一种简洁的方式来定义函数,常用于 Lambda 表达式和方法引用。下面是一些常用的接口:

一、Predicate(断言)

  • 方法:test(T t),接受一个参数并返回一个 boolean 值。
  • 用途:用于检查给定的输入是否满足某些条件。常见的用法是过滤集合中的元素。image-20240406222733024

示例一:

Predicate<Integer> isPositive = num -> num > 0;
System.out.println(isPositive.test(5)); // 输出 true
System.out.println(isPositive.test(-3)); // 输出 false

示例二:

	public static void main(String[] args) {
        doPredicate(s -> s.contains("h"));
    }

    public static void doPredicate(Predicate<String> predicate){
        System.out.println("result:"+predicate.test("hello"));
    }
输出结果:

image-20240406222905368

二、Consumer(消费者)

  • 方法:accept(T t),接受一个参数并且不返回结果。
  • 用途:对给定的输入执行某些操作,但没有返回值。image-20240406223259179

示例一:

Consumer<String> printUpperCase = str -> System.out.println(str.toUpperCase());
printUpperCase.accept("hello"); // 输出 HELLO

示例二:

	public static void main(String[] args) {
        doConsumer(s -> System.out.println(s.toUpperCase()));
    }
    public static void doConsumer(Consumer<String> consumer){
        consumer.accept("hello");
    }
结果:

image-20240406223522858

三、Supplier(供应商)

  • 方法:get(),不接受任何参数,但返回一个结果。
  • 用途:生成或提供值,常用于延迟计算或工厂方法。

image-20240406223930791

示例一:

Supplier<Double> randomNumber = () -> Math.random();
System.out.println(randomNumber.get()); // 输出一个随机数

示例二:

	public static void main(String[] args) {
        doSupplier(()->"hello");
    }

    public static void doSupplier(Supplier<String> supplier){
        System.out.println(supplier.get());
    }
结果:

image-20240406223920312

四、Function(函数)

  • 方法:apply(T t),接受一个参数并返回一个结果。
  • 用途:将输入映射为输出,是最常用的函数式接口之一。
  • 说明,传入两个泛型,也可以理解为将第二个类型转为第一个类型,第二个泛型类型也就是要入参的类型,返回值为第一个泛型类型

image-20240406224239505

示例一:

Function<Integer, String> convertToString = num -> "The number is: " + num;
System.out.println(convertToString.apply(10)); // 输出 The number is: 10

示例二:

	public static void main(String[] args) {
        int result = doFunction(str ->{
         int integer = Integer.parseInt(str);
         return integer*100;
        });
        System.out.println(result);
    }

    public static Integer doFunction(Function<String,Integer> function){
        return function.apply("10");
    }
结果:

image-20240406224338214

五、Comparator(比较)

  • 方法:int compare(T obj1, T obj2):接受两个参数并比较它们的顺序。如果第一个参数应该排在第二个参数之前,则返回负整数;如果第一个参数应该排在第二个参数之后,则返回正整数;如果两个参数相等,则返回零。
  • 用途:排序:最常见的用途是对集合中的元素进行排序。通过传递一个 Comparator 对象给排序算法(如 Collections.sort()Arrays.sort()),可以自定义对象的比较方式。

示例:

实现从短到长排序

String[] strings = {"apple", "banana", "orange", "grape"};
Arrays.sort(strings,(s1,s2)-> s1.length() -s2.length());
System.out.println(Arrays.toString(strings));
结果:

image-20240406225214845

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

造 山

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值