日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
Java8Predicate函數(shù)接口

本文轉(zhuǎn)載自微信公眾號(hào)「未讀代碼」,作者未讀君。轉(zhuǎn)載本文請(qǐng)聯(lián)系未讀代碼公眾號(hào)。

成都創(chuàng)新互聯(lián)公司是一家專注于做網(wǎng)站、成都網(wǎng)站制作與策劃設(shè)計(jì),迎江網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)公司做網(wǎng)站,專注于網(wǎng)站建設(shè)十多年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:迎江等地區(qū)。迎江做網(wǎng)站價(jià)格咨詢:028-86922220

Predicate 函數(shù)接口同之前介紹的 Function 接口一樣,是一個(gè)函數(shù)式接口,它可以接受一個(gè)泛型 參數(shù),返回值為布爾類型。Predicate 常用于數(shù)據(jù)過(guò)濾,如過(guò)濾出集合中符合某個(gè)條件的元素。

源碼:Java 8 中函數(shù)接口 Predicate。

 
 
 
  1. package java.util.function; 
  2.  
  3. import java.util.Objects; 
  4.  
  5. @FunctionalInterface 
  6. public interface Predicate { 
  7.  
  8.     boolean test(T t); 
  9.  
  10.     default Predicate and(Predicate other) { 
  11.         Objects.requireNonNull(other); 
  12.         return (t) -> test(t) && other.test(t); 
  13.     } 
  14.    
  15.     default Predicate negate() { 
  16.         return (t) -> !test(t); 
  17.     } 
  18.  
  19.     default Predicate or(Predicate other) { 
  20.         Objects.requireNonNull(other); 
  21.         return (t) -> test(t) || other.test(t); 
  22.     } 
  23.  
  24.     static  Predicate isEqual(Object targetRef) { 
  25.         return (null == targetRef) 
  26.                 ? Objects::isNull 
  27.                 : object -> targetRef.equals(object); 
  28.     } 

1. Predicate test

Predicate 函數(shù)接口可以用于判斷一個(gè)參數(shù)是否符合某個(gè)條件。

示例:判斷某個(gè)字符串是否為空。

 
 
 
  1. import java.util.function.Predicate; 
  2.  
  3. public class Java8PredicateTest { 
  4.     public static void main(String[] args) { 
  5.         Predicate isEmpty = String::isEmpty; 
  6.         System.out.println(isEmpty.test("")); 
  7.         System.out.println(isEmpty.test("www.wdbyte.com")); 
  8.     } 

輸出結(jié)果:

 
 
 
  1. true 
  2. false 

2. Predicate Stream filter

Stream 中的 filter() 方法是通過(guò)接收一個(gè) Predicate 函數(shù)接口實(shí)現(xiàn)的。

示例:過(guò)濾出集合中,字符串長(zhǎng)度為 4 的字符串。

 
 
 
  1. import java.util.Arrays; 
  2. import java.util.List; 
  3. import java.util.stream.Collectors; 
  4.  
  5. public class Java8PredicateFilter { 
  6.  
  7.     public static void main(String[] args) { 
  8.         List list = Arrays.asList("java", "node", "www.wdbyte.com"); 
  9.         list = list.stream().filter(str -> str.length() == 4).collect(Collectors.toList()); 
  10.         System.out.println(list); 
  11.     } 

輸出結(jié)果:

 
 
 
  1. [java, node] 

3. Predicate and

使用 and() 方法,可以讓前后兩個(gè) Predicate 判斷條件一起生效。

示例 1:過(guò)濾數(shù)字集合中,數(shù)字大小在 5 至 9 之間的數(shù)字。

 
 
 
  1. import java.util.Arrays; 
  2. import java.util.List; 
  3. import java.util.function.Predicate; 
  4. import java.util.stream.Collectors; 
  5.  
  6. public class Java8PredicateAnd { 
  7.  
  8.     public static void main(String[] args) { 
  9.         List numberList = Arrays.asList(3, 4, 5, 6, 7, 8, 9, 10); 
  10.  
  11.         Predicate greaterThan5 = number -> number > 5; 
  12.         Predicate lessThan9 = number -> number < 9; 
  13.         Predicate filter = greaterThan5.and(lessThan9); 
  14.  
  15.         numberList = numberList.stream().filter(filter).collect(Collectors.toList()); 
  16.         System.out.println(numberList); 
  17.     } 

結(jié)果輸出:

 
 
 
  1. [6, 7, 8] 

示例 2:一個(gè) Predicate 過(guò)濾數(shù)字集合中,數(shù)字大小在 5 至 9 之間的數(shù)字。

 
 
 
  1. List numberList = Arrays.asList(3, 4, 5, 6, 7, 8, 9, 10); 
  2. numberList = numberList.stream().filter(x -> x > 5 && x < 9).collect(Collectors.toList()); 
  3. System.out.println(numberList); 

輸出結(jié)果;

 
 
 
  1. [6, 7, 8] 

4. Predicate negate

predicate.negate() 方法會(huì)返回一個(gè)與指定判斷相反的 Predicate。

示例:過(guò)濾數(shù)字集合中,數(shù)字不大于 5 的數(shù)字。

 
 
 
  1. import java.util.Arrays; 
  2. import java.util.List; 
  3. import java.util.function.Predicate; 
  4. import java.util.stream.Collectors; 
  5.  
  6. public class Java8PredicateNeagete { 
  7.  
  8.     public static void main(String[] args) { 
  9.         List numberList = Arrays.asList(3, 4, 5, 6, 7, 8, 9, 10); 
  10.         Predicate greaterThan5 = number -> number > 5; 
  11.  
  12.         numberList = numberList.stream().filter(greaterThan5.negate()).collect(Collectors.toList()); 
  13.         System.out.println(numberList); 
  14.     } 

輸出結(jié)果:

 
 
 
  1. [3, 4, 5] 

5. Predicate or

示例:過(guò)濾數(shù)字集合中,數(shù)字小于等于 5,或者大于等于 9 的數(shù)字。

 
 
 
  1. import java.util.Arrays; 
  2. import java.util.List; 
  3. import java.util.function.Predicate; 
  4. import java.util.stream.Collectors; 
  5.  
  6. public class Java8PredicateOr { 
  7.  
  8.     public static void main(String[] args) { 
  9.         List numberList = Arrays.asList(3, 4, 5, 6, 7, 8, 9, 10); 
  10.  
  11.         Predicate lessThan5 = number -> number <= 5; 
  12.         Predicate greaterThan8 = number -> number >= 9; 
  13.  
  14.         numberList = numberList.stream().filter(lessThan5.or(greaterThan8)).collect(Collectors.toList()); 
  15.         System.out.println(numberList); 
  16.     } 

輸出結(jié)果:

 
 
 
  1. [3, 4, 5, 9, 10] 

6. Predicate 鏈?zhǔn)骄幊?/h3>

Predicate 的 or() ,and(),negate() 方法可以隨意組合 Predicate,組合后的判斷邏輯是從左到右,從前到后,順次判斷。

如:(數(shù)字小于 5 ).and (數(shù)字大于 9 ).negate()。

解:(數(shù)字小于 5 )AND (數(shù)字大于 9 ) 對(duì)于任意數(shù)字都得 false,false.negate() 取相反 得 true。

所以,此判斷邏輯對(duì)于任意數(shù)字都為 true。

示例:Predicate 的 or() ,and(),negate() 方法組合使用。

 
 
 
  1. import java.util.ArrayList; 
  2. import java.util.Arrays; 
  3. import java.util.List; 
  4. import java.util.function.Predicate; 
  5.  
  6. public class Java8PredicateChain { 
  7.  
  8.     public static void main(String[] args) { 
  9.         List numberList = Arrays.asList(3, 4, 5, 6, 7, 8, 9, 10); 
  10.  
  11.         Predicate lessThan5 = number -> number <= 5; 
  12.         Predicate greaterThan9 = number -> number >= 9; 
  13.  
  14.         // 小于等于 5 
  15.         System.out.println(filter(numberList, lessThan5)); 
  16.         // 大于 5 
  17.         System.out.println(filter(numberList, lessThan5.negate())); 
  18.         // 小于等于 5 或者大于等于 9 
  19.         System.out.println(filter(numberList, lessThan5.or(greaterThan9))); 
  20.         // ! (小于等于 5 AND 大于等于 9) 
  21.         System.out.println(filter(numberList, lessThan5.and(greaterThan9).negate())); 
  22.     } 
  23.  
  24.     public static  List filter(List list, Predicate predicate) { 
  25.         List resultList = new ArrayList<>(); 
  26.         for (T t : list) { 
  27.             if (predicate.test(t)) { 
  28.                 resultList.add(t); 
  29.             } 
  30.         } 
  31.         return resultList; 
  32.     } 

輸出結(jié)果:

 
 
 
  1. [3, 4, 5] 
  2. [6, 7, 8, 9, 10] 
  3. [3, 4, 5, 9, 10] 
  4. [3, 4, 5, 6, 7, 8, 9, 10] 

7. Predicate 與對(duì)象

示例:過(guò)濾符合某些特征的狗。

 
 
 
  1. import java.util.ArrayList; 
  2. import java.util.List; 
  3. import java.util.function.Predicate; 
  4.  
  5. public class Java8PredicateObject { 
  6.  
  7.     public static void main(String[] args) { 
  8.         List dogList = new ArrayList<>(); 
  9.         dogList.add(new Dog("哈士奇", 1)); 
  10.         dogList.add(new Dog("牧羊犬", 2)); 
  11.         dogList.add(new Dog("柯基", 3)); 
  12.         dogList.add(new Dog("柴犬", 3)); 
  13.  
  14.         // 找到 3歲的狗 
  15.         System.out.println(filter(dogList, dog -> dog.getAge().equals(3))); 
  16.         // 找到哈士奇信息 
  17.         Predicate predicate = dog -> ("哈士奇").equals(dog.getName()); 
  18.         System.out.println(filter(dogList, predicate)); 
  19.     } 
  20.  
  21.     public static  List filter(List list, Predicate predicate) { 
  22.         List resultList = new ArrayList<>(); 
  23.         for (T t : list) { 
  24.             if (predicate.test(t)) { resultList.add(t); } 
  25.         } 
  26.         return resultList; 
  27.     } 
  28.  
  29. class Dog { 
  30.     private String name; 
  31.     private Integer age; 
  32.  
  33.     public Dog(String name, Integer age) { 
  34.         this.name = name; 
  35.         this.age = age; 
  36.     } 
  37.  
  38.     public String getName() { 
  39.         return name; 
  40.     } 
  41.  
  42.     public void setName(String name) { 
  43.         this.name = name; 
  44.     } 
  45.  
  46.     public Integer getAge() { 
  47.         return age; 
  48.     } 
  49.  
  50.     public void setAge(Integer age) { 
  51.         this.age = age; 
  52.     } 
  53.  
  54.     @Override 
  55.     public String toString() { 
  56.         return "Dog{" + 
  57.             "name='" + name + '\'' + 
  58.             ", age=" + age + 
  59.             '}'; 
  60.     } 

輸出結(jié)果:

 
 
 
  1. [Dog{name='柯基', age=3}, Dog{name='柴犬', age=3}] 
  2. [Dog{name='哈士奇', age=1}] 

BiPredicate 和 Predicate 函數(shù)接口一樣,都是返回一個(gè)布爾類型,唯一不同的是 Predicate 接受一個(gè)參數(shù),而 BiPredicate 可以接受兩個(gè)不同類型的參數(shù)。

BiPredicate 在 Java 8 中源碼:

 
 
 
  1. package java.util.function; 
  2.  
  3. import java.util.Objects; 
  4. @FunctionalInterface 
  5. public interface BiPredicate { 
  6.     boolean test(T t, U u); 
  7.  
  8.     default BiPredicate and(BiPredicate other) { 
  9.         Objects.requireNonNull(other); 
  10.         return (T t, U u) -> test(t, u) && other.test(t, u); 
  11.     } 
  12.  
  13.     default BiPredicate negate() { 
  14.         return (T t, U u) -> !test(t, u); 
  15.     } 
  16.  
  17.     default BiPredicate or(BiPredicate other) { 
  18.         Objects.requireNonNull(other); 
  19.         return (T t, U u) -> test(t, u) || other.test(t, u); 
  20.     } 

當(dāng)前標(biāo)題:Java8Predicate函數(shù)接口
當(dāng)前路徑:http://www.5511xx.com/article/codceig.html