Predicate源码
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);
}
复制
Predicate使用
package com.example.functionalinterfacedemo;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
public class PredicateDemo {
public static void main(String[] args) {
List<Integer> asList = Arrays.asList(1,2,3,4,5,6,7,8,9);
List<Integer> list = test(asList, obj->obj.intValue()>5);
System.out.println(list);
}
/**
*
* @param list 接受参数
* @param predicate 定义函数
* @return 满足一定条件的数据
*/
public static List<Integer> test(List<Integer> list,Predicate<Integer> predicate){
List<Integer> list2=new ArrayList<Integer>();
for(Integer integer :list ){
if(predicate.test(integer)){
list2.add(integer);
}
}
return list2;
}
}
复制
最后修改时间:2020-07-01 13:37:29
文章转载自227decision,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
Java 开发玩转 MCP:从 Claude 自动化到 Spring AI Alibaba 生态整合
阿里巴巴中间件
61次阅读
2025-04-08 11:01:30
java项目选择云服务器怎么选?
云知识CLOUD
33次阅读
2025-04-09 20:02:37
瞧瞧别人家的判空,那叫一个优雅!
jinchanchanwaji
22次阅读
2025-04-03 14:56:21
Java数据库连接池学习
淡定
20次阅读
2025-04-14 22:46:26
Java反射大揭秘:程序员的“偷窥”与“开挂”指南
让天下没有难学的编程
19次阅读
2025-03-28 15:02:40
【JVM祖传手艺大揭秘】双亲委派:Java世界的"啃老"生存法则
让天下没有难学的编程
11次阅读
2025-04-09 11:01:12
面试官:Java反射和new效率对比,差距有多大?
捡田螺的小男孩
9次阅读
2025-04-13 10:34:43
Java萌新修炼手册①:开局一把JDK,环境搭建全靠浪!
让天下没有难学的编程
6次阅读
2025-04-21 10:34:37
Java萌新修炼手册②:Hello Worldの108种写法——从入门到入坟!
让天下没有难学的编程
6次阅读
2025-04-21 10:34:36
Java程序使用预处理语句的性能提升
GreatSQL社区
5次阅读
2025-04-23 11:18:50