您当前的位置: 首页 > 

cuiyaonan2000

暂无认证

  • 4浏览

    0关注

    248博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

JDK8: Funcationl Interface

cuiyaonan2000 发布时间:2022-05-07 15:10:58 ,浏览量:4

序言

之前我们了解过Jdk8提供的Predicate,是JDK提供的一种判断true和false的规范类.其实JDK8同时还提供了很多类似于Predicate的函数式接口.这里简单介绍下.让我们以后的代码更容易读懂.cuiyaonan2000@163.com

参考资料:

  1. Java 8 函数式接口 | 菜鸟教程

函数式接口

函数式接口(Functional Interface)就是一个有且仅有一个抽象方法,但是可以有多个非抽象方法的接口。

比如我们之前使用的Predicate类的源代码就只有一个接口方法,其它的都有实现类cuiyaonan2000@163.com

/*
 * 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.
 *
 * 

This is a functional interface * whose functional method is {@link #test(Object)}. * * @param the type of the input to the predicate * * @since 1.8 */ @FunctionalInterface public interface Predicate { /** * 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. * *

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 and(Predicate

关注
打赏
1638267374
查看更多评论
0.0511s