Java8新特性Stream流中anyMatch和allMatch和noneMatch的區(qū)別解析
1、anyMatch
判斷數(shù)據(jù)列表中是否存在任意一個元素符合設(shè)置的predicate條件,如果是就返回true,否則返回false。
- 接口定義:
- boolean anyMatch(Predicate<? super T> predicate);
- 方法描述:
- 在anyMatch 接口定義中是接收 Predicate 類型參數(shù),在Lamdba表達式中 Predicate<T> 是接收一個T類型參數(shù),然后經(jīng)過邏輯驗證返回布爾值結(jié)果。這里anyMatch表示,判斷的條件里,任意一個元素符合條件,就返回true值。
- 使用場景:
- 兩個集合的交集
@Test
public void a17() {
List<User> list = new ArrayList<>();
list.add(new User("張三", 12, "南京"));
list.add(new User("李四", 13, "北京"));
list.add(new User("王五", 14, "蘇州"));
list.add(new User("王五", 17, "蘇州"));
List<User> userList = new ArrayList<>();
userList.add(new User("李四", 13, "北京"));
userList.add(new User("王五", 20, "廣州"));
// 獲取兩個集合中有相同名字或者年齡相同的,只要滿足其中一個條件即可,只會返回list集合里面的元素,有先后順序返回
List<User> users1 = list.stream()
.filter(a -> userList.stream().anyMatch(b -> a.getName().equals(b.getName()) || a.getAge() == b.getAge()))
.collect(Collectors.toList());
// 獲取兩個集合中相同名字并且年齡相同的,必須同時滿足兩個條件
List<User> users2 = list.stream()
.filter(a -> userList.stream().anyMatch(b -> a.getName().equals(b.getName()) && a.getAge() == b.getAge()))
.collect(Collectors.toList());
users1.forEach(item -> {
System.out.println(item.getName() + item.getAge() + item.getEmailAddress());
});
/** 第一種結(jié)果展示:
* 李四13北京
* 王五14蘇州
* 王五17蘇州
*/
users2.forEach(item -> {
System.out.println(item.getName() + item.getAge() + item.getEmailAddress());
});
/** 第二種結(jié)果展示:
* 李四13北京
*/
}
@Test
public void a15() {
Stream<String> stream = Stream.of("ac", "bcddddd", "bd");
// 判斷stream中其中任何一個元素中只要有包含b字符串或者l字符串就返回true
boolean isMatch = stream.anyMatch(str -> str.contains("b") || str.contains("l"));
System.out.println(isMatch); // true
}2、allMatch
判斷數(shù)據(jù)列表中全部元素都符合設(shè)置的predicate條件,如果是就返回true,否則返回false,流為空時總是返回true。
- 接口定義:
- boolean allMatch(Predicate<? super T> predicate);
- 方法描述:
- 在allMatch 接口定義中是接收 Predicate 類型參數(shù),在Lamdba表達式中 Predicate<T> 是接收一個T類型參數(shù),然后經(jīng)過邏輯驗證返回布爾值結(jié)果。這里allMatch表示,判斷的條件里,全部元素符合條件,就返回true值。
- 適用場景:
- 基本類型的集合,但不適合于對象集合(我自己的理解)
- allMatch里面不適合寫 && ,只適合寫 ||,如果寫&&,編譯器會自動報黃色波浪線
@Test
public void a18() {
List<String> typeList1 = Arrays.asList("1", "2");
List<String> typeList2 = Arrays.asList("1", "2", "3", "4");
// 集合列表中全部元素必須在allMatch里面的那些字符串,只要全部元素中有任意一個不同的元素在AllMatch中就返回false
boolean isMatch1 = typeList1.stream().allMatch(a -> a.equals("1") || a.equals("2") || a.equals("3"));
boolean isMatch2 = typeList2.stream().allMatch(a -> a.equals("1") || a.equals("2") || a.equals("3"));
System.out.println(isMatch1); // true
System.out.println(isMatch2); // false
}
@Test
public void a16() {
Stream<String> stream = Stream.of("abc", "abc", "bcd");
// 判斷stream中全部所有元素必須全部包含b字符串和c字符串就返回true,如果有一個元素不包含這兩個字符串就返回false
boolean isMatch = stream.allMatch(str -> str.contains("b") && str.contains("c"));
System.out.println(isMatch); // true
}3、noneMatch
判斷數(shù)據(jù)列表中全部元素都不符合設(shè)置的predicate條件,如果是就返回true,否則返回false,流為空時總是返回true。
- 接口定義:
- boolean noneMatch(Predicate<? super T> predicate);
- 方法描述:
- 在noneMatch接口定義中是接收 Predicate 類型參數(shù),在Lamdba表達式中 Predicate<T> 是接收一個T類型參數(shù),然后經(jīng)過邏輯驗證返回布爾值結(jié)果。這里noneMatch表示與allMatch相反,判斷的條件里的元素,所有的元素都不符合,就返回true值。
- 適用場景:
- 兩個集合的差集 (本人只想到這么做,如果有更簡潔的可以告訴我怎么寫,感謝0.0)
@Test
public void a17() {
List<User> list = new ArrayList<>();
list.add(new User("張三", 12, "南京"));
list.add(new User("李四", 13, "北京"));
list.add(new User("王五", 14, "蘇州"));
list.add(new User("王五", 17, "蘇州"));
List<User> userList = new ArrayList<>();
userList.add(new User("李四", 13, "北京"));
userList.add(new User("王五", 20, "廣州"));
// 獲取list集合和userList集合過濾掉兩者集合中名字和年齡相同的數(shù)據(jù)后,只返回list集合的數(shù)據(jù)
List<User> users3 = list.stream()
.filter(a -> userList.stream().noneMatch(b -> a.getName().equals(b.getName()) && a.getAge() == b.getAge()))
.collect(Collectors.toList());
// 獲取userlist集合和list集合過濾掉兩者集合中名字和年齡相同的數(shù)據(jù)后,只返回userList集合的數(shù)據(jù)
List<User> users4 = userList.stream()
.filter(a -> list.stream().noneMatch(b -> a.getName().equals(b.getName()) && a.getAge()==b.getAge()))
.collect(Collectors.toList());
// 獲取list和userList集合之間的差集,將上面兩者集合合并,就是兩個集合的差集
List<User> arrayList = new ArrayList<>();
arrayList.addAll(users3);
arrayList.addAll(users4);
arrayList.forEach(item -> {
System.out.println(item.getName() + item.getAge() + item.getEmailAddress());
});
/** 兩者集合之間的差集
* 張三12南京
* 王五14蘇州
* 王五17蘇州
* 王五20廣州
*/
System.out.println("-------------------");
users3.forEach(item -> {
System.out.println(item.getName() + item.getAge() + item.getEmailAddress());
});
/** 只返回list集合中過濾掉之后的元素集合
* 張三12南京
* 王五14蘇州
* 王五17蘇州
*/
System.out.println("-------------------");
users4.forEach(item -> {
System.out.println(item.getName() + item.getAge() + item.getEmailAddress());
});
/** 只返回userList集合中過濾掉之后的元素集合
* 王五20廣州
*/
}
@Test
public void a19() {
List<String> typeList1 = Arrays.asList("1", "2");
List<String> typeList2 = Arrays.asList("1", "2", "3", "4");
// 集合列表中全部元素只要都不在noneMatch里面的判斷中,就返回true,集合列表中任何元素中只要有一個在noneMatch的判斷中就返回false
boolean isMatch1 = typeList1.stream().noneMatch(a -> a.equals("3") || a.equals("4"));
boolean isMatch2 = typeList2.stream().noneMatch(a -> a.equals("1") || a.equals("2") || a.equals("3"));
System.out.println(isMatch1); // true
System.out.println(isMatch2); // false
}
/** noneMatch */
@Test
public void a20() {
Stream<String> stream = Stream.of("dddd", "ee", "qqq", "bcfff");
// 判斷 stream 中所有的元素都不是以 a 開頭,就返回true,如果所有的元素中只要其中一個元素是以a開頭的,就返回false
boolean isMatch = stream.noneMatch(str->str.startsWith("a"));
System.out.println(isMatch); // true
}到此這篇關(guān)于Java8新特性Stream流中anyMatch和allMatch和noneMatch的區(qū)別詳解的文章就介紹到這了,更多相關(guān)java8 anyMatch和allMatch和noneMatch區(qū)別內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java連接MYSQL數(shù)據(jù)庫的實現(xiàn)步驟
以下的文章主要描述的是java連接MYSQL數(shù)據(jù)庫的正確操作步驟,在此篇文章里我們主要是以實例列舉的方式來引出其具體介紹2013-06-06
讀寫nacos的配置中心的參數(shù)及變動監(jiān)聽方式
Spring應(yīng)用可以通過NacosDataSource下載配置參數(shù),使用ConfigService的publishConfig方法更新Nacos上的配置,但同一服務(wù)內(nèi)部更新配置不會觸發(fā)configUpdate事件,ConfigService也提供了監(jiān)聽事件功能2026-02-02
Java通過word模板實現(xiàn)創(chuàng)建word文檔報告
這篇文章主要為大家詳細介紹了Java如何通過word模板實現(xiàn)創(chuàng)建word文檔報告的教程,文中的示例代碼講解詳細,感興趣的小伙伴可以學(xué)習(xí)一下2022-09-09
SpringBoot項目引入第三方sdk?jar包的解決方案
這篇文章主要介紹了SpringBoot項目引入第三方sdk?jar包,個人感覺比較好的解決方案是將 jar上傳到本地的maven倉庫,然后通過pom依賴,引入第三方j(luò)ar包,需要的朋友可以參考下2022-05-05
Springboot項目使用Slf4j將日志保存到本地目錄的實現(xiàn)代碼
這篇文章主要介紹了Springboot項目使用Slf4j將日志保存到本地目錄的實現(xiàn)方法,本文通過示例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05
MybatisPlus使用注解的多對多級聯(lián)查詢方式
這篇文章主要介紹了MybatisPlus使用注解的多對多級聯(lián)查詢方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07

