国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

java8利用Stream方法求兩個List對象的交集、差集與并集(即:anyMatch和allMatch和noneMatch的區(qū)別詳解)

這篇具有很好參考價值的文章主要介紹了java8利用Stream方法求兩個List對象的交集、差集與并集(即:anyMatch和allMatch和noneMatch的區(qū)別詳解)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

1、anyMatch?
判斷數(shù)據(jù)列表中是否存在任意一個元素符合設置的predicate條件,如果是就返回true,否則返回false。

接口定義:
boolean anyMatch(Predicate<? super T> predicate);

方法描述:
在anyMatch 接口定義中是接收 Predicate 類型參數(shù),在Lamdba表達式中 Predicate<T> 是接收一個T類型參數(shù),然后經(jīng)過邏輯驗證返回布爾值結果。這里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());
        });
        /** 第一種結果展示:
         * 李四13北京
         * 王五14蘇州
         * 王五17蘇州
         */
        users2.forEach(item -> {
            System.out.println(item.getName() + item.getAge() + item.getEmailAddress());
        });
        /** 第二種結果展示:
         * 李四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ù)列表中全部元素都符合設置的predicate條件,如果是就返回true,否則返回false,流為空時總是返回true。

接口定義:
boolean allMatch(Predicate<? super T> predicate);

方法描述:
在allMatch 接口定義中是接收 Predicate 類型參數(shù),在Lamdba表達式中 Predicate<T> 是接收一個T類型參數(shù),然后經(jīng)過邏輯驗證返回布爾值結果。這里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ù)列表中全部元素都不符合設置的predicate條件,如果是就返回true,否則返回false,流為空時總是返回true。

接口定義:
boolean noneMatch(Predicate<? super T> predicate);

方法描述:
在noneMatch接口定義中是接收 Predicate 類型參數(shù),在Lamdba表達式中 Predicate<T> 是接收一個T類型參數(shù),然后經(jīng)過邏輯驗證返回布爾值結果。這里noneMatch表示與allMatch相反,判斷的條件里的元素,所有的元素都不符合,就返回true值。

適用場景:

兩個集合的差集 (本人只想到這么做,如果有更簡潔的可以告訴我怎么寫,感謝0.0)
?文章來源地址http://www.zghlxwxcb.cn/news/detail-740861.html

@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
}

到了這里,關于java8利用Stream方法求兩個List對象的交集、差集與并集(即:anyMatch和allMatch和noneMatch的區(qū)別詳解)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如若轉載,請注明出處: 如若內(nèi)容造成侵權/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領支付寶紅包贊助服務器費用

相關文章

  • java 獲取兩個List集合的交集

    可以使用Java中的retainAll方法來獲取兩個List的交集: 假設有兩個List類型的集合list1和list2,代碼如下: 下面是獲取兩個集合的交集的代碼: 輸出結果為: 其中,retainAll方法會修改intersection集合,使其只包含兩個集合的交集。在這個例子中,intersection集合最初是包含list1集合的

    2024年02月11日
    瀏覽(21)
  • Stream流 - 兩個list集合對象屬性的合并、對象屬性值運算

    Stream流 - 兩個list集合對象屬性的合并、對象屬性值運算

    ?? 合并兩個 list<map>, 并將 userId 相同的所有屬性合并到一個 map 中 list1中對象的屬性:userId、userName list2中對象的屬性:userId、gender、age ?? 最終總集合中對象的屬性:userId、userName、gender、age 運行結果: 結果可見,userId 相同的所有屬性合并到集合 list1中。 ?? 合并兩個

    2024年02月06日
    瀏覽(307)
  • java中l(wèi)ist對象拷貝至新的list對象并保持兩個對象獨立的方法

    使用構造函數(shù): 可以使用List的構造函數(shù),傳遞原始List作為參數(shù)來創(chuàng)建一個新的List對象。這樣做會創(chuàng)建一個新的List對象,兩個List對象互相獨立,修改其中一個不會影響另一個。 使用addAll()方法: 可以使用List的addAll()方法將原始List中的所有元素添加到一個新的List中。 使用

    2024年02月07日
    瀏覽(32)
  • Java8-使用stream.sorted()對List排序

    1.流的定義 Stream 中文稱為 “流”,通過將集合轉換為這么一種叫做 “流” 的元素序列,通過聲明性方式,能夠對集合中的每個元素進行一系列并行或串行的操作! 如果流中的元素的類實現(xiàn)了 Comparable 接口,即有自己的排序規(guī)則,那么可以直接調(diào)用 sorted() 方法對元素進行排

    2024年02月16日
    瀏覽(93)
  • JAVA8中l(wèi)ist.stream()的一些簡單使用

    為函數(shù)式編程而生。對stream的任何修改都不會修改背后的數(shù)據(jù)源,比如對stream執(zhí)行過濾操作并不會刪除被過濾的元素,而是會產(chǎn)生一個不包含被過濾元素的新stream。 stream上的操作并不會立即執(zhí)行,只有等到用戶真正需要結果的時候才會執(zhí)行。 stream只能被使用一次,一旦遍歷

    2024年02月03日
    瀏覽(23)
  • Java8使用Stream流實現(xiàn)List列表簡單使用

    Java8使用Stream流實現(xiàn)List列表簡單使用

    ?? 目錄 1.forEach() 2.filter(T - boolean) 3.findAny()和findFirst() 4.map(T - R) 和flatMap(T - stream) 5.distinct() 去重 6.limit(long n)和skip(long n)? 7.anyMatch(T - boolean) 8.allMatch(T - boolean) 9.noneMatch(T - boolean) Java8提供了Stream(流)處理集合的關鍵抽象概念,Stream 使用一種類似用 SQL 語句從數(shù)據(jù)庫查詢數(shù)

    2023年04月27日
    瀏覽(26)
  • Java使用不同方式獲取兩個集合List的交集、補集、并集(相加)、差集(相減)

    Java使用不同方式獲取兩個集合List的交集、補集、并集(相加)、差集(相減)

    首先知道幾個單詞的意思: 并集 = union 交集 = intersection 補集 = complement 析取 = disjunction 減去 = subtract 對于兩個給定集合A、B,由兩個集合所有元素構成的集合,叫做A和B的并集。 記作:AUB 讀作“A并B” 例:{3,5}U{2,3,4,6}= {2,3,4,5,6} 對于兩個給定集合A、B,由屬于A又屬于B的所有元

    2024年01月22日
    瀏覽(92)
  • Java8 Stream流List<JSONObject>通過某一屬性進行排序

    List對象 1.首先你需要list.parallelStream().sorted 進行流處理,使用parallelStream可以充分調(diào)度多核CPU。 2.使用Comparator.comparing進行排序,reversed()進行倒序排列,thenComparing進行下一個排序。 3.Comparator.comparing()里面的內(nèi)容,也是就是Object::getter,例如Test::getName 4.最后格式化為需要的格式

    2024年02月12日
    瀏覽(94)
  • Java8使用stream流給List<Map<String,Object>>分組(多字段key)

    Java8使用 stream流 給ListMapString,Object根據(jù)字段key 分組 一、項目場景: 從已得到的List集合中,根據(jù)某一元素(這里指map的key)進行分組,篩選出需要的數(shù)據(jù)。 如果是SQL的話則使用 group by 直接實現(xiàn),代碼的方式則如下: 使用到stream流的 Collectors.groupingBy() 方法。 二、代碼實現(xiàn) 1、首

    2024年02月02日
    瀏覽(86)
  • 【java】List對象集合去除特定對象的優(yōu)雅寫法(基于java8)

    【java】List對象集合去除特定對象的優(yōu)雅寫法(基于java8) 主要用的是 Java List.removeIf方法,該方法用于刪除所有滿足特定條件的數(shù)組元素,入?yún)⑵鋵嵤且粋€布爾值的表達式即可。 使用場景,用戶類中的性別字段有三個,分別是男、女、未知。 當前端要查詢:“男”時,要求

    2024年02月16日
    瀏覽(91)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領取紅包,優(yōu)惠每天領

二維碼1

領取紅包

二維碼2

領紅包