java基础之--常用函数式接口Predicate与Function

1. Predicate 接口方法

有时候我们需要对某种类型的数据进行判断,从而得到一个boolean值结果。这时可以使用 java.util.function.Predicate 接口

package ListStream;

import java.util.function.Predicate;

/**
 * @Description
 * @auther 宁宁小可爱
 * @create 2020-05-21 9:30
 */
public class PredicateDemo {
    public static void main(String[] args) {
        // 条件判断的标准是传入的Lambda表达式逻辑,只要字符串长度大于5则认为很长。
        testMethod(s -> s.length() > 5);
    }

    public static void testMethod(Predicate<String> predicate){
        boolean result = predicate.test("HelloWorld");
        System.out.println("字符串很长么?  " + result);
    }
}

1.1 Predicate的与或非条件判断

package ListStream;

import java.util.function.Predicate;

/**
 * @Description
 * @auther 宁宁小可爱
 * @create 2020-05-21 9:30
 */
public class PredicateDemo {
    public static void main(String[] args) {
        // 条件判断的标准是传入的Lambda表达式逻辑 , s包含大写H 与 W
        andMethod(s -> s.contains("H") ,s ->s.contains("W"));
        // 条件判断的标准是传入的Lambda表达式逻辑 , s包含大写H 或 W
        orMethod(s -> s.contains("H"), s -> s.contains("W"));
        // 条件判断的标准是传入的Lambda表达式逻辑 , s长度大于5
        negateMethod(s -> s.length() > 5);
    }

    // 与方法 判断一个字符串既要包含大写“H”,又要包含大写“W”,
    public static void andMethod(Predicate<String> one,Predicate<String> two){
        boolean result = one.and(two).test("HelloWorld");
        System.out.println("是否包含大写W与H?  " + result);
    }

    // 或方法 判断一个字符串包含大写“H” 或 包含大写“W”,
    public static void orMethod(Predicate<String> one,Predicate<String> two){
        boolean result = one.or(two).test("HelloWorld");
        System.out.println("是否包含大写W或H?  " + result);
    }

    // 非方法 , 对结果取反
    public static void negateMethod(Predicate<String> predicate){
        boolean result = predicate.negate().test("HelloWorld");
        System.out.println("字符串很长么?  " + result);
    }
}

2. Function接口

java.util.function.Function<T,R> 接口用来根据一个类型的数据得到另一个类型的数据,前者称为前置条件, 后者称为后置条件。有进有出,所以称为“函数Function”

抽象方法apply

Function 接口中最主要的抽象方法为: R apply(T t) ,根据类型T的参数获取类型R的结果。使用的场景例如: 将 String 类型转换为 Integer 类型

package ListStream;

import java.util.function.Function;

/**
 * @Description
 * @auther 宁宁小可爱
 * @create 2020-05-21 10:11
 */
public class FunctionDemo {
    public static void main(String[] args) {
        // 最好是通过方法引用的写法
        applyMethod(Integer::parseInt);
        // 第一个操作是字符串解析成为int数字,第二个操作是乘以10两个操作通过 andThen 按照前后顺序组合到了一 起。
        andThenMethod(Integer::parseInt, i -> i *= 10);
    }

    // apply方法
    private static void applyMethod(Function<String,Integer> function){
        Integer num = function.apply("10");
        System.out.println("apply方法计算结果 :"+num+20);
    }

    // andThen方法
    private static void andThenMethod(Function<String,Integer> one,Function<Integer,Integer> two){
        int num = one.andThen(two).apply("10");
        System.out.println("andThen方法计算结果 :" + num+20);
    }
}

自定义函数模型拼接

  • 将字符串截取数字年龄部分,得到字符串
  • 将上一步的字符串转换成为int类型的数字
  • 将上一步的int数字累加100,得到结果int数字
package ListStream;

import java.util.function.Function;

/**
 * @Description
 * @auther 宁宁小可爱
 * @create 2020-05-21 10:11
 */
public class FunctionDemo {
    public static void main(String[] args) {
        String str = "宁宁,20";
        int age = getAgeNum(str, s -> s.split(",")[1], Integer::parseInt, n -> n += 100);
        System.out.println(age);
    }

    private static int getAgeNum(String str, Function<String, String> one,
                                 Function<String, Integer> two,
                                 Function<Integer, Integer> three) {
        return one.andThen(two).andThen(three).apply(str);
    }
}

3 总结: 延迟方法与终结方法

在上述学习到的多个常用函数式接口当中,方法可以分成两种:

  • 延迟方法:只是在拼接Lambda函数模型的方法,并不立即执行得到结果
  • 终结方法:根据拼好的Lambda函数模型,立即执行得到结果值的方法

通常情况下,这些常用的函数式接口中唯一的抽象方法为终结方法,而默认方法为延迟方法。但这并不是绝对的。 下面的表格中进行了方法分类的整理:

接口名称方法名称抽象/默认延迟/终结
Supplierget抽象终结
Consumeraccept抽象终结
andThen默认延迟
Predicatetest抽象终结
and默认延迟
or默认延迟
negate默认延迟
Functionapply抽象终结
andThen默认延迟
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

叫我三胖哥哥

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

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

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

打赏作者

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

抵扣说明:

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

余额充值