java8 函数式编程Predicate

原创
07/21 22:20
阅读数 35
package com.qimh.springbootfiledemo.function;

import java.util.function.Predicate;

/**
 * @author
 * 参考连接:https://blog.csdn.net/qq_27416233/article/details/83418791
 * 我们就来看看返回的是什么
 */
public class PredicateAND {

    public static void main(String[] args) {

        PredicateAND predicateAND = new PredicateAND();

        System.out.println(predicateAND.testAndMethod("zhangsan",
                stringOne -> stringOne.equals("zhangsan"),stringTwo -> stringTwo.length() > 5));

        System.out.println(predicateAND.testAndMethod2("zhangsan",
                stringOne -> stringOne.equals("zhangsan"),stringTwo -> stringTwo.length() > 5));
    }

    /**
     *
     * @param stringOne         待判断的字符串
     * @param predicateOne      断定表达式1
     * @param predicateTwo      断定表达式2
     * @return                    是否满足两个条件
     */
    public boolean testAndMethod(String stringOne, Predicate<String> predicateOne,Predicate<String> predicateTwo) {

        return predicateOne.and(predicateTwo).test(stringOne);
    }

    /**
     *
     * @param stringOne         待判断的字符串
     * @param predicateOne      断定表达式1
     * @param predicateTwo      断定表达式2
     * @return                    是否满足两个条件
     */
    public Predicate<String> testAndMethod2(String stringOne, Predicate<String> predicateOne, Predicate<String> predicateTwo) {

        return predicateOne.and(predicateTwo);
    }


}
package com.qimh.springbootfiledemo.function;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import static sun.management.snmp.jvminstr.JvmThreadInstanceEntryImpl.ThreadStateMap.Byte1.other;

/**
 * @author
 * 我们来验证一下,那说的比较矛盾的地方。
 * 1. 评估参数里面的表达式(说白了就是验证传进来的参数符不符合规则,后面有例子)
 *    我们来验证一下这句话,并且解释一下。
 */
public class PredicateTestOne {

    public static void main(String[] args) {

        PredicateTestOne predicateTestOne = new PredicateTestOne();

        Predicate<String> predicate = new Predicate<String>() {
            @Override
            public boolean test(String s) {

                return s.equals("zhangsan");
            }
        };

        System.out.println(predicate.test("lisi"));
        System.out.println("--- --- --- --- --- ---");
        System.out.println(predicate.test("zhangsan"));

        testPredicateOR();
    }

    /**
     * 参考连接:https://www.jianshu.com/p/79a81f7f422c
     *          https://cloud.tencent.com/developer/article/1187833
     */
    public static void testPredicateOR(){
        Predicate<String> startsWith_S = str -> str.startsWith("S");
        Predicate<String> contains_m = str -> str.contains("m");
        List<Predicate<String>> list = new ArrayList<>();
        list.add(startsWith_S);
        list.add(contains_m);

        List<String> names = new ArrayList<>();
        names.add("Larry");
        names.add("Jeremy");
        names.add("Sam");
        names.add("Simon");
        names.add("Mike");

        List<String> newNames = names.stream().filter(list.stream().reduce(Predicate::or).orElse(other -> true)).collect(Collectors.toList());
        System.out.println(newNames);
    }
}
package com.qimh.springbootfiledemo.function;

import java.util.function.Predicate;

/**
 * @author
 * - 1.判断传入的字符串的长度是否大于5
 * - 2.判断传入的参数是否是偶数
 * - 3.判断数字是否大于10
 */
public class PredicateTestThree {

    public static void main(String[] args) {

        PredicateTestThree predicate = new PredicateTestThree();

        /** - 1.判断传入的字符串的长度是否大于5 */
        System.out.println(predicate.judgeConditionByFunction(12345,value -> String.valueOf(value).length() > 5));
        /** - 2.判断传入的参数是否是奇数 */
        System.out.println(predicate.judgeConditionByFunction(4,value -> value % 2 == 0));
        /** - 3.判断数字是否大于10 */
        System.out.println(predicate.judgeConditionByFunction(-1, value-> value > 10));
    }

    public boolean judgeConditionByFunction(int value, Predicate<Integer> predicate) {
        return predicate.test(value);
    }
}

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部