java8第二篇

2020-03-25 11:53:04 浏览数 (1)

Predicate断言式,判断是否符合指定的条件。个人感觉这个函数式接口,常用于集合的过滤操作,我们可以看下其提供的方法都有哪些。

代码语言:javascript复制
packge java.util.function;
import java.util.Objects;
@FunctionalInterface
public interface Predicate<T> {
    boolean test(T t);
     
    default Predicate<T> and(Predicate<? super T> other) {
        Objects.requireNonNull(other);
        return (t) -> test(t) && other.test(t);
    }
    
    default Predicate<T> negate() {
        return (t) -> !test(t);
    }

    default Predicate<T> or(Predicate<? super T> other) {
        Objects.requireNonNull(other);
        return (t) -> test(t) || other.test(t);
    }

    static <T> Predicate<T> isEqual(Object targetRef) {
        return (null == targetRef)
                ? Objects::isNull
                : object -> targetRef.equals(object);
    }
}

下面我们看下在示例程序中如何使用它的吧。

代码语言:javascript复制
package com.wpw.java8;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

/**
 * @author pc
 */
@Slf4j
public class PredicatedTest {

    private static void print(Object object) {
        System.out.println("object = "   object);
    }

    public static void main(String[] args) {
        Predicate<String> predicate = item -> "java函数式接口Predicate".equals(item);
        log.info("test()方法:{}", predicate.test("java函数式接口Predicate"));
        List<Integer> list = new ArrayList<>(10);
        final int size = 10;
        for (int i = 1; i <= size; i  ) {
            list.add(i);
        }
        log.info("Predicate返回一个List中的偶数");
        Object[] result = list.stream().filter(x -> x % 2 == 0).toArray();
        print(Arrays.toString(result));
        /**测试Predicate的and方法,打印list中大于3,小于6的数字*/
        Object[] array = list.stream().filter(x -> x > 3 && x < 6).toArray();
        print(Arrays.toString(array));
        /**测试Predicate的or方法,打印list中小于3或者大于5的数字*/
        Object[] objects = list.stream().filter(x -> x < 3 || x > 5).toArray();
        print(Arrays.toString(objects));
        /**测试Predicate的negate方法,返回list中大于等于3,小于等于5的数字*/
        Object[] toArray = list.stream().filter(x -> x >= 3 && x <= 5).toArray();
        print(Arrays.toString(toArray));
        Predicate<Object> objectPredicate = Predicate.isEqual("java8");
        boolean flag = objectPredicate.test("java8");
        log.info("flag:{}", flag);
    }
}

还是感觉上面的用法很实用,为了验证单一方法的测试,这里也简单给出一个示例程序。

代码语言:javascript复制
        log.info("单一方法进行测试and,or,negate");
        Predicate<Integer> andOperator = x -> x > 3;
        andOperator.and(x -> x <= 10);
        print(Arrays.toString(list.stream().filter(andOperator).collect(Collectors.toList()).toArray()));
        Predicate<Integer> orOperator = x -> x < 3;
        orOperator.or(x -> x > 6);
        print(Arrays.toString(list.stream().filter(orOperator).collect(Collectors.toList()).toArray()));
        Predicate<Integer> negateOperator = x -> x < 10;
        negateOperator.negate();
        print(Arrays.toString(list.stream().filter(negateOperator).collect(Collectors.toSet()).toArray()));

0 人点赞