Java 8 Predicate花样用法

您所在的位置:网站首页 语言学predicate的定义 Java 8 Predicate花样用法

Java 8 Predicate花样用法

2023-08-30 06:07| 来源: 网络整理| 查看: 265

1. 简介

本文介绍Java 8 Predicate链.

2. 基本用法

怎么使用简单的Predicate来过滤list中的name

@Test public void whenFilterList_thenSuccess(){ List names = Arrays.asList("Adam", "Alexander", "John", "Tom"); List result = names.stream() .filter(name -> name.startsWith("A")) .collect(Collectors.toList()); assertEquals(2, result.size()); assertThat(result, contains("Adam","Alexander")); }

我们使用Predicate来筛选以大写字母A开头的姓名。

name -> name.startsWith("A")

那么如果多个条件这么办?

3. 多条件过滤 @Test public void whenFilterListWithMultipleFilters_thenSuccess(){ List result = names.stream() .filter(name -> name.startsWith("A")) .filter(name -> name.length() < 5) .collect(Collectors.toList()); assertEquals(1, result.size()); assertThat(result, contains("Adam")); }

用两个filter传入两个 Predicate分别过滤  【以A开头的】和【姓名长度小于5】的。

4. 复杂条件 @Test public void whenFilterListWithComplexPredicate_thenSuccess(){ List result = names.stream() .filter(name -> name.startsWith("A") && name.length() < 5) .collect(Collectors.toList()); assertEquals(1, result.size()); assertThat(result, contains("Adam")); }

使用一个 filter 传入复杂的Predicate.

5. 组合使用Predicate

Predicates可以将 Predicate.and(), Predicate.or() 和 Predicate.negate()组合起来使用。

5.1. Predicate.and() @Test public void whenFilterListWithCombinedPredicatesUsingAnd_thenSuccess(){ Predicate predicate1 = str -> str.startsWith("A"); Predicate predicate2 = str -> str.length() < 5; List result = names.stream() .filter(predicate1.and(predicate2)) .collect(Collectors.toList()); assertEquals(1, result.size()); assertThat(result, contains("Adam")); }

两个条件都要满足

5.2. Predicate.or()

满足其中一个即可

@Test public void whenFilterListWithCombinedPredicatesUsingOr_thenSuccess(){ Predicate predicate1 = str -> str.startsWith("J"); Predicate predicate2 = str -> str.length() < 4; List result = names.stream() .filter(predicate1.or(predicate2)) .collect(Collectors.toList()); assertEquals(2, result.size()); assertThat(result, contains("John","Tom")); } 5.3. Predicate.negate()

将此条件取反

Predicate predicate2 =  str -> str.length() < 4;

相当于

Predicate predicate2 =  str -> str.length() >= 4;

@Test public void whenFilterListWithCombinedPredicatesUsingOrAndNegate_thenSuccess(){ Predicate predicate1 = str -> str.startsWith("J"); Predicate predicate2 = str -> str.length() < 4; List result = names.stream() .filter(predicate1.or(predicate2.negate())) .collect(Collectors.toList()); assertEquals(3, result.size()); assertThat(result, contains("Adam","Alexander","John")); } 5.4. 内联的方式组合使用Predicates @Test public void whenFilterListWithCombinedPredicatesInline_thenSuccess(){ List result = names.stream() .filter(((Predicate)name -> name.startsWith("A")) .and(name -> name.length() str.startsWith("A")); allPredicates.add(str -> str.contains("d")); allPredicates.add(str -> str.length() > 4); List result = names.stream() .filter(allPredicates.stream().reduce(x->true, Predicate::and)) .collect(Collectors.toList()); assertEquals(1, result.size()); assertThat(result, contains("Alexander")); }

注意这里初始条件是 true (如果初始条件为 false ,后续即使都满足,和初始值一起 and ,也没结果)

然后看 reduce 中使用 or 操作的例子:

@Test public void whenFilterListWithCollectionOfPredicatesUsingOr_thenSuccess(){ List result = names.stream() .filter(allPredicates.stream().reduce(x->false, Predicate::or)) .collect(Collectors.toList()); assertEquals(2, result.size()); assertThat(result, contains("Adam","Alexander")); }

Predicate::or 操作,通常会将初始值设置为 false,因为如果初始值为 true 不管后续条件是否为 true 最终结果都为 true。

7. 结论

本文介绍Java 8 Predicate。介绍了 Predicate在Stream的filter函数中的运用。讲述了复杂的Predicate或者Predicate的组合的用法。

英文原文:https://www.baeldung.com/java-predicate-chain

如果觉得本文对你有帮助,欢迎点赞评论,欢迎关注我,我将努力创作更多更好的文章。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3