Predicate<T> 断言型函数接口示例
Predicate<T>是一个函数接口,它接收一个实参,返回一个boolean类型的结果。它通常被用来处理一个对象集合的过滤。
import java.util.function.Predicate; import java.util.stream.Collectors; @Slf4j public class TestPredicate { /** * Predicate在filter()中使用 * filter()接收predicate参数 */ @Test public void testPredicateFilter() { Predicate<Integer> noGreaterThan5 = x -> x > 5; List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); List<Integer> collect = list.stream().filter(noGreaterThan5).collect(Collectors.toList()); System.out.println(collect); // [6, 7, 8, 9, 10] } /** * Predicate在and()中使用 * 多个过滤 */ @Test public void testPredicateAnd() { Predicate<Integer> noGreaterThan5 = x -> x > 5; Predicate<Integer> noLessThan8 = x -> x < 8; List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); List<Integer> collect = list.stream() .filter(noGreaterThan5.and(noLessThan8)) .collect(Collectors.toList()); System.out.println(collect); // [6, 7] } /** * Predicate.or() * 满足任意条件即可 */ @Test public void testPredicateOr() { Predicate<String> lengthIs3 = x -> x.length() == 3; Predicate<String> startWithA = x -> x.startsWith("A"); List<String> list = Arrays.asList("A", "AA", "AAA", "B", "BB", "BBB"); List<String> collect = list.stream() .filter(lengthIs3.or(startWithA)) .collect(Collectors.toList()); System.out.println(collect); // [A, AA, AAA, BBB] } /** * Predicate.negate() * 查找所有不以‘A’开头的元素 */ @Test public void testPredicateNegate() { Predicate<String> startWithA = x -> x.startsWith("A"); List<String> list = Arrays.asList("A", "AA", "AAA", "B", "BB", "BBB"); List<String> collect = list.stream() .filter(startWithA.negate()) .collect(Collectors.toList()); System.out.println(collect); // [B, BB, BBB] } /** * Predicate.test() */ @Test public void testPredicateTest() { List<String> list = Arrays.asList("A", "AA", "AAA", "B", "BB", "BBB"); System.out.println(StringProcessor.filter( list, x -> x.startsWith("A"))); // [A, AA, AAA] System.out.println(StringProcessor.filter( list, x -> x.startsWith("A") && x.length() == 3)); // [AAA] } static class StringProcessor { static List<String> filter(List<String> list, Predicate<String> predicate) { return list.stream().filter(predicate::test).collect(Collectors.toList()); } } /** * 链式Predicate */ @Test public void testPredicateLian() { Predicate<String> startWithA = x -> x.startsWith("a"); // start with "a" or "m" boolean result = startWithA.or(x -> x.startsWith("m")).test("mkyong"); System.out.println(result); // true // !(start with "a" and length is 3) boolean result2 = startWithA.and(x -> x.length() == 3).negate().test("abc"); System.out.println(result2); // false } /** * removeIf() 方法用于删除所有满足特定条件的数组元素。 */ @Test public void testListRemoveIf() { List<String> list = Arrays.asList("A", "AA", "AAA", "B", "BB", "BBB"); Predicate<String> predicate = x -> x.startsWith("A"); List<String> list2 = Lists.newArrayList(); list2.addAll(list); list2.removeIf(predicate); log.info(list2.toString()); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律