java 8 新特性equal使用_Java 8 新特性:4-断言(Predicate)接口

news/2024/7/7 11:51:04

(原)

这个接口主要用于判断,先看看它的实现,说明,再给个例子。

/*

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

* 根据一个参数代表了一个基于boolean类型的断言

*

This is a functional interface

* whose functional method is {@link #test(Object)}.

*这是一个函数式接口,它的函数方法是test

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

* 通过这个predicate和它的参数predicate 返回一个逻辑与的判断结果,

*当去计算这个复合的predicate时,如果当前的predicate 结果是false,那么就不会计算它的参数other的值。

*

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 super T> other) {

Objects.requireNonNull(other);

return (t) -> test(t) && other.test(t);

}

/**

* Returns a predicate that represents the logical negation of this

* predicate.

* 返回一个predicate 代表了这个predicate的逻辑非

* @return a predicate that represents the logical negation of this

* predicate

*/

default Predicate 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.

*通过这个predicate和它的参数predicate 返回一个逻辑或的判断结果,

当计算这个组合的predicate,如果这个predicate是true ,那么它的参数other将不会计算

*

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 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)}.

*如果二个参数机等的话,根据Objects#equals(Object, Object)返回一个断言的结果

* @param 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 Predicate isEqual(Object targetRef) {

return (null == targetRef)

? Objects::isNull

: object -> targetRef.equals(object);

}

}

这里其实慢慢看它的doc文档,还真没有直接看它的实现来的快。无非就是一个判断的函数式接口,主要做逻辑与或非的判断,其中还有一个静态方法,其实现是这样的:

return (null == targetRef)

? Objects::isNull

: object -> targetRef.equals(object);

null == targetRef这个就不说了,因为它的返回结果是predicate,所以Objects::isNull必需是predicate的实例,它代表了一个方法的引用,为什么它符合这个函数式接口的唯一抽象方法boolean test(T t);这个呢?我们进去看下它的实现。

public static boolean isNull(Object obj) {

return obj == null;

}

这是一个静态的方法引用,接收一个Object类型的参数,返回一个boolean类型,这完全附合这个函数式接口的boolean test(T t);抽象方法,那么编译器就会认为它是predicate这个函数式接口的一个实现。

下面给出一个例子,看下怎么使用的,结果我就不分析了。

package com.demo.jdk8;

import java.util.Arrays;

import java.util.List;

import java.util.function.Predicate;

public class Test4 {

public static void main(String[] args) {

Predicate p = s -> s.length() > 3;

System.out.println(p.test("hello"));

List list = Arrays.asList(1,2,3,4,5,6,7,8);

System.out.println("part1------------------");

findOdd(list);

System.out.println("part2------------------");

conditionFilter(list, ppp -> ppp % 2 == 1);

System.out.println("part3------------------");

and(list, p1 -> p1 > 3, p2 -> p2 < 7);

System.out.println("part4------------------");

or(list, p1 -> p1 > 3, p2 -> p2 % 2 == 1);

System.out.println("part5------------------");

negate(list, p1 -> p1 > 3);

System.out.println("part6------------------");

System.out.println(isEqual("abc").test("abcd"));

}

//找到集合中的奇数

public static void findOdd(List list){

for (int i = 0; i < list.size(); i++) {

if(list.get(i) % 2 == 1){

System.out.println(list.get(i));

}

}

}

public static void conditionFilter(List list,Predicate p){

for (int i = 0; i < list.size(); i++) {

if(p.test(list.get(i))){

System.out.println(list.get(i));

}

}

}

public static void and(List list,Predicate p1,Predicate p2){

for (int i = 0; i < list.size(); i++) {

if(p1.and(p2).test(list.get(i))){

System.out.println(list.get(i));

}

}

}

public static void or(List list,Predicate p1,Predicate p2){

for (int i = 0; i < list.size(); i++) {

if(p1.or(p2).test(list.get(i))){

System.out.println(list.get(i));

}

}

}

public static void negate(List list,Predicate p1){

for (int i = 0; i < list.size(); i++) {

if(p1.negate().test(list.get(i))){

System.out.println(list.get(i));

}

}

}

public static Predicate isEqual(Object obj){

return Predicate.isEqual(obj);

}

}


http://www.niftyadmin.cn/n/707411.html

相关文章

oscached

介绍 提供了在现有JSP页面之内实现快速内存缓冲的功能,是一个广泛采用的高性能的J2EE缓存框架&#xff0c;OSCache能用于任何Java应用程序的普通的缓存解决方案。 特点&#xff1a;缓存任何对象&#xff0c;你可以不受限制的缓存部分jsp页面或HTTP请求&#xff0c;任何java对…

jsonp react 获取返回值_ajax请求JSONP格式,后台返回值是SUCCESS,返回这样的数据格式可不可以调用某某方法?...

能不能在不改后台代码的情况下实现&#xff0c;现在总是走error的方法&#xff0c;response只是”success”(**说让他们改后台很难)还要跨站请求的需求。不改后台就实现JSONP??前端什么时候这么厉害了首先要理解什么是jsonp&#xff0c;自己写个请求就行了&#xff0c;不一定…

C#,VB.NET 如何将Excel转换为Text

在工作中&#xff0c;有时我们需要转换文档的格式&#xff0c;之前已经跟大家介绍过了如何将Excel转换为PDF。今天将与大家分享如何将Excel转换为Text。这次我使用的依然是免费版的Spire.XLS for .NET组件。 Free Spire.XLS for .NET组件可以使开发人员在任何.NET应用程序上创建…

OSI与TCP/IP网络模型

OSI七层网络模型 OSI(Open System Interconnection&#xff0c;开放系统互联)七层网络模型成为开放式系统互联参考模型&#xff0c;是一个把网络通信在逻辑上的定义&#xff0c;也可以理解成为定义了通用的网络通信规范。而我们的数据在网络中传输的过程&#xff0c;实际上就是…

如何创建一个本地的mysql数据库_怎么创建一个本地的sql数据库

展开全部|CREATE DATABASE database_name[ CONTAINMENT { NONE | PARTIAL } ][ ON[ PRIMARY ] [ ,...n ][ , [ ,...n ] ][ LOG ON [ ,...n ] ]][ COLLATE collation_name ][ WITH [,...n ] ][;]::{FILESTREAM ( [,...n ] )| DEFAULT_FULLTEXT_LANGUAGE { lcid | language_nam…

codevs 1025 选菜——01背包

时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解查看运行结果题目描述 Description在小松宿舍楼下的不远处&#xff0c;有PK大学最不错的一个食堂——The Farmer’s Canteen&#xff08;NM食堂&#xff09;。由于该食堂的菜都很不错&#xff0c;价格也公道&#…

kali mysql安装教程_MySQL 安装教程(windows版)

MySQL版本&#xff1a;8.0.18window环境&#xff1a;win101.首先我们需要下载ZIP解压配置安装包&#xff0c;如果有需要的可以到下面网址下载。2.将安装包下载至F盘中新建的my_sql文件夹中并解压3.配置环境变量&#xff0c;右键我的电脑&#xff0c;选择 属性---高级系统设置--…

maven的使用

作为一个技术菜&#xff0c;这篇文章主要介绍maven的基本使用。maven的安装&#xff0c;maven的用途&#xff0c;这里就不做介绍了&#xff0c;可以百度。一、cmd命令行下创建一个简单的maven项目&#xff1a;1.mvn archetype:generate&#xff08;第一次使用maven需要联网&…