Java8新特性Stream流中anyMatch和allMatch和noneMatch的區(qū)別解析
1、anyMatch
判斷數(shù)據(jù)列表中是否存在任意一個(gè)元素符合設(shè)置的predicate條件,如果是就返回true,否則返回false。
- 接口定義:
- boolean anyMatch(Predicate<? super T> predicate);
- 方法描述:
- 在anyMatch 接口定義中是接收 Predicate 類型參數(shù),在Lamdba表達(dá)式中 Predicate<T> 是接收一個(gè)T類型參數(shù),然后經(jīng)過(guò)邏輯驗(yàn)證返回布爾值結(jié)果。這里anyMatch表示,判斷的條件里,任意一個(gè)元素符合條件,就返回true值。
- 使用場(chǎng)景:
- 兩個(gè)集合的交集
@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, "廣州")); // 獲取兩個(gè)集合中有相同名字或者年齡相同的,只要滿足其中一個(gè)條件即可,只會(huì)返回list集合里面的元素,有先后順序返回 List<User> users1 = list.stream() .filter(a -> userList.stream().anyMatch(b -> a.getName().equals(b.getName()) || a.getAge() == b.getAge())) .collect(Collectors.toList()); // 獲取兩個(gè)集合中相同名字并且年齡相同的,必須同時(shí)滿足兩個(gè)條件 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中其中任何一個(gè)元素中只要有包含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,流為空時(shí)總是返回true。
- 接口定義:
- boolean allMatch(Predicate<? super T> predicate);
- 方法描述:
- 在allMatch 接口定義中是接收 Predicate 類型參數(shù),在Lamdba表達(dá)式中 Predicate<T> 是接收一個(gè)T類型參數(shù),然后經(jīng)過(guò)邏輯驗(yàn)證返回布爾值結(jié)果。這里allMatch表示,判斷的條件里,全部元素符合條件,就返回true值。
- 適用場(chǎng)景:
- 基本類型的集合,但不適合于對(duì)象集合(我自己的理解)
- allMatch里面不適合寫 && ,只適合寫 ||,如果寫&&,編譯器會(huì)自動(dòng)報(bào)黃色波浪線
@Test public void a18() { List<String> typeList1 = Arrays.asList("1", "2"); List<String> typeList2 = Arrays.asList("1", "2", "3", "4"); // 集合列表中全部元素必須在allMatch里面的那些字符串,只要全部元素中有任意一個(gè)不同的元素在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,如果有一個(gè)元素不包含這兩個(gè)字符串就返回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,流為空時(shí)總是返回true。
- 接口定義:
- boolean noneMatch(Predicate<? super T> predicate);
- 方法描述:
- 在noneMatch接口定義中是接收 Predicate 類型參數(shù),在Lamdba表達(dá)式中 Predicate<T> 是接收一個(gè)T類型參數(shù),然后經(jīng)過(guò)邏輯驗(yàn)證返回布爾值結(jié)果。這里noneMatch表示與allMatch相反,判斷的條件里的元素,所有的元素都不符合,就返回true值。
- 適用場(chǎng)景:
- 兩個(gè)集合的差集 (本人只想到這么做,如果有更簡(jiǎn)潔的可以告訴我怎么寫,感謝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集合過(guò)濾掉兩者集合中名字和年齡相同的數(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集合過(guò)濾掉兩者集合中名字和年齡相同的數(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集合之間的差集,將上面兩者集合合并,就是兩個(gè)集合的差集 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集合中過(guò)濾掉之后的元素集合 * 張三12南京 * 王五14蘇州 * 王五17蘇州 */ System.out.println("-------------------"); users4.forEach(item -> { System.out.println(item.getName() + item.getAge() + item.getEmailAddress()); }); /** 只返回userList集合中過(guò)濾掉之后的元素集合 * 王五20廣州 */ } @Test public void a19() { List<String> typeList1 = Arrays.asList("1", "2"); List<String> typeList2 = Arrays.asList("1", "2", "3", "4"); // 集合列表中全部元素只要都不在noneMatch里面的判斷中,就返回true,集合列表中任何元素中只要有一個(gè)在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 開(kāi)頭,就返回true,如果所有的元素中只要其中一個(gè)元素是以a開(kāi)頭的,就返回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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java用freemarker導(dǎo)出word實(shí)用示例
本篇文章主要介紹了Java用freemarker導(dǎo)出word實(shí)用示例,使用freemarker的模板來(lái)實(shí)現(xiàn)功能,有需要的可以了解一下。2016-11-11maven實(shí)現(xiàn)jar包導(dǎo)入+導(dǎo)出方式
這篇文章主要介紹了maven實(shí)現(xiàn)jar包導(dǎo)入+導(dǎo)出方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07利用Java中Calendar計(jì)算兩個(gè)日期之間的天數(shù)和周數(shù)
Java 語(yǔ)言的Calendar(日歷),Date(日期),和DateFormat(日期格式)組成了Java標(biāo)準(zhǔn)的一個(gè)基本但是非常重要的部分。日期是商業(yè)邏輯計(jì)算一個(gè)關(guān)鍵的部分。下面這篇文章就給大家介紹了如何利用Java中Calendar計(jì)算兩個(gè)日期之間的天數(shù)和周數(shù),下面來(lái)一起看看吧。2016-12-12基于springboot實(shí)現(xiàn)數(shù)據(jù)可視化的示例代碼
本文主要介紹了基于springboot實(shí)現(xiàn)數(shù)據(jù)可視化,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧<BR>2022-07-07java 過(guò)濾器模式(Filter/Criteria Pattern)詳細(xì)介紹
這篇文章主要介紹了java 過(guò)濾器模式(Filter/Criteria Pattern)詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2016-10-10Java日期轉(zhuǎn)換注解配置date?format時(shí)間失效
這篇文章主要為大家介紹了Java日期轉(zhuǎn)換注解配置date?format時(shí)間失效,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12Java將RTF文檔轉(zhuǎn)換為Word/PDF/HTML/圖片
RTF文檔因其跨平臺(tái)兼容性而廣泛使用,但有時(shí)在不同的應(yīng)用場(chǎng)景可能需要特定的文檔格式,所以本文來(lái)講講如何通過(guò)Java將RTF轉(zhuǎn)換為Word/PDF/HTML和圖片格式2025-01-01