日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

java8----Predicate接口的使用

發(fā)布時(shí)間:2023/12/13 综合教程 27 生活家
生活随笔 收集整理的這篇文章主要介紹了 java8----Predicate接口的使用 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Suppliers(生產(chǎn)者)
Suppliers產(chǎn)生一個(gè)給定的泛型類型的結(jié)果。與Functional不同的是Suppliers不接受輸入?yún)?shù)。
Supplier<Person> personSupplier = Person::new;
personSupplier.get();   // new Person

Consumers(消費(fèi)者)
Consumers代表在一個(gè)單一的輸入?yún)?shù)上執(zhí)行操作。
Consumer<Person> greeter = (p) -> System.out.println("Hello, " + p.firstName);
greeter.accept(new Person("Luke", "Skywalker"));
        //5、lambda表達(dá)式中加入Predicate
        // 甚至可以用and()、or()和xor()邏輯函數(shù)來合并Predicate,
        // 例如要找到所有以J開始,長(zhǎng)度為四個(gè)字母的名字,你可以合并兩個(gè)Predicate并傳入
        Predicate<String> startsWithJ = (n) -> n.startsWith("J");
        Predicate<String> fourLetterLong = (n) -> n.length() == 4;
        languages.stream()
                .filter(startsWithJ.and(fourLetterLong))
                .forEach((n) -> System.out.print("nName, which starts with 'J' and four letter long is : " + n));
        //---基礎(chǔ)用法2------------- test方法是predicate中的唯一一個(gè)抽象方法  https://www.cnblogs.com/L-a-u-r-a/p/9077615.html

        public static void filterCondition(List<String> names, Predicate<String> predicate) {
            for (String name : names) {
                if (predicate.test(name)) {
                    System.out.println(name + " ");
                }
            }
        }

        List languages = Arrays.asList("Java", "Scala", "C++", "Haskell", "Lisp");

        System.out.println("Languages which starts with J :");
        filterCondition(languages, (str) -> str.startsWith("J"));

        System.out.println("Languages which ends with a ");
        filterCondition(languages, (str) -> str.endsWith("a"));

        System.out.println("Print language whose length greater than 4:");
        filterCondition(languages, (str) -> str.length() > 4);

        System.out.println("Print all languages :");
        filterCondition(languages, (str) -> true);
        System.out.println("Print no language : ");
        filterCondition(languages, (str) -> false);
 //---基礎(chǔ)用法------------- https://www.baidu.com/link?url=6iszXQlsmyaoWVZMaPs3g8vLRQXzdzTnKzQYTF8lg-5QQthjAu1KMSxRbEU_PznfUS4-KVH1hfn64wdAOahiCq&wd=&eqid=d6aa9d87000231f1000000065dfc8e0a

        Predicate<String> condition1 = str -> str.startsWith("j");
        Predicate<String> condition2 = str -> str.endsWith("h");
        //and
        Predicate<String> andCondition = condition1.and(condition2);
        boolean result9 = andCondition.test("jsadh");
        System.out.println(result9);//true

        //or
        Predicate<String> orCondition = condition1.or(condition2);
        result9 = orCondition.test("jasd");
        System.out.println(result9);//true

        //negate,對(duì)判斷條件取反
        Predicate<String> negate = condition1.negate();
        System.out.println(negate.test("aj"));//true

        //isEqual(了解,比較兩個(gè)對(duì)象是否相等)
        result9 = Predicate.isEqual("as").test("aaa");
        System.out.println(result9);

總結(jié)

以上是生活随笔為你收集整理的java8----Predicate接口的使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。