jdk8函数式编程之Predicate

java.uti.function包下,JDK 8 中提供了一组常用的核心函数接口:

接口

参数

返回类型

描述

Predicate<T>

T

boolean

用于判别一个对象。比如求一个人是否为男性

Consumer<T>

T

void

用于接收一个对象进行处理但没有返回,比如接收一个人并打印他的名字

Function<T, R>

T

R

转换一个对象为不同类型的对象

Supplier<T>

None

T

提供一个对象

UnaryOperator<T>

T

T

接收对象并返回同类型的对象

BinaryOperator<T>

(T, T)

T

接收两个同类型的对象,并返回一个原类型对象

其中Predicate 用于判断对象是否符合某个条件,经常被用来过滤对象

先看一下Predicate源码

/*
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 */
package java.util.function;

import java.util.Objects;

/**
 * Represents a predicate (boolean-valued function) of one argument.
 *
 * <p>This is a <a href="package-summary.html">functional interface</a>
 * whose functional method is {@link #test(Object)}.
 *
 * @param <T> the type of the input to the predicate
 *
 * @since 1.8
 */
@FunctionalInterface
public interface Predicate<T> {

    /**
     * Evaluates this predicate on the given argument.
     *
     * @param t the input argument
     * @return {@code true} if the input argument matches the predicate,
     * otherwise {@code false}
     */
    boolean test(T t);

    /**
     * Returns a composed predicate that represents a short-circuiting logical
     * AND of this predicate and another.  When evaluating the composed
     * predicate, if this predicate is {@code false}, then the {@code other}
     * predicate is not evaluated.
     *
     * <p>Any exceptions thrown during evaluation of either predicate are relayed
     * to the caller; if evaluation of this predicate throws an exception, the
     * {@code other} predicate will not be evaluated.
     *
     * @param other a predicate that will be logically-ANDed with this
     *              predicate
     * @return a composed predicate that represents the short-circuiting logical
     * AND of this predicate and the {@code other} predicate
     * @throws NullPointerException if other is null
     */
    default Predicate<T> and(Predicate<? super T> other) {
        Objects.requireNonNull(other);
        return (t) -> test(t) && other.test(t);
    }

    /**
     * Returns a predicate that represents the logical negation of this
     * predicate.
     *
     * @return a predicate that represents the logical negation of this
     * predicate
     */
    default Predicate<T> negate() {
        return (t) -> !test(t);
    }

    /**
     * Returns a composed predicate that represents a short-circuiting logical
     * OR of this predicate and another.  When evaluating the composed
     * predicate, if this predicate is {@code true}, then the {@code other}
     * predicate is not evaluated.
     *
     * <p>Any exceptions thrown during evaluation of either predicate are relayed
     * to the caller; if evaluation of this predicate throws an exception, the
     * {@code other} predicate will not be evaluated.
     *
     * @param other a predicate that will be logically-ORed with this
     *              predicate
     * @return a composed predicate that represents the short-circuiting logical
     * OR of this predicate and the {@code other} predicate
     * @throws NullPointerException if other is null
     */
    default Predicate<T> or(Predicate<? super T> other) {
        Objects.requireNonNull(other);
        return (t) -> test(t) || other.test(t);
    }

    /**
     * Returns a predicate that tests if two arguments are equal according
     * to {@link Objects#equals(Object, Object)}.
     *
     * @param <T> the type of arguments to the predicate
     * @param targetRef the object reference with which to compare for equality,
     *               which may be {@code null}
     * @return a predicate that tests if two arguments are equal according
     * to {@link Objects#equals(Object, Object)}
     */
    static <T> Predicate<T> isEqual(Object targetRef) {
        return (null == targetRef)
                ? Objects::isNull
                : object -> targetRef.equals(object);
    }
}

Predicate这个类比较简单,只有5个方法

1.boolean test(T t);

这是最核心的一个方法,验证传入的参数是举个匹配这个函数接口,返回值类型为Boolean

举个栗子:

Predicate<String> p = s->s.equals("a");
System.out.println(p.test("a"));

2.default Predicate<T> and(Predicate<? super T> other)

值得一提的是jdk1.8接口引入了default关键字,接口中也可以有非抽象的方法了,给了接口一个默认的实现方法

这个方法是把传入的函数和当前函数拼接为一个新的函数,必须同时满足两个函数才会返回True

看一个栗子:

        Predicate<String> predicate1 = s->s.contains("a");
        Predicate<String> predicate2 = s->s.contains("b");
        Predicate<String> predicate3 = predicate1.and(predicate2);
        System.out.println(predicate3.test("a"));
        System.out.println(predicate3.test("abc"));

3.negate和or方法就很好理解了,negate是对test方法取非运算,or是或运算,isEqual是一个静态方法,判断参数是否和给定目标相等。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值