自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Java 8 Predicate 函數(shù)接口

開(kāi)發(fā) 后端
Predicate 函數(shù)接口同之前介紹的 Function 接口一樣,是一個(gè)函數(shù)式接口,它可以接受一個(gè)泛型 <T> 參數(shù),返回值為布爾類型。

[[414975]]

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

Predicate 函數(shù)接口同之前介紹的 Function 接口一樣,是一個(gè)函數(shù)式接口,它可以接受一個(gè)泛型 <T> 參數(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<T> { 
  7.  
  8.     boolean test(T t); 
  9.  
  10.     default Predicate<T> and(Predicate<? super T> other) { 
  11.         Objects.requireNonNull(other); 
  12.         return (t) -> test(t) && other.test(t); 
  13.     } 
  14.    
  15.     default Predicate<T> negate() { 
  16.         return (t) -> !test(t); 
  17.     } 
  18.  
  19.     default Predicate<T> or(Predicate<? super T> other) { 
  20.         Objects.requireNonNull(other); 
  21.         return (t) -> test(t) || other.test(t); 
  22.     } 
  23.  
  24.     static <T> Predicate<T> 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<String> 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<String> 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<Integer> numberList = Arrays.asList(3, 4, 5, 6, 7, 8, 9, 10); 
  10.  
  11.         Predicate<Integer> greaterThan5 = number -> number > 5; 
  12.         Predicate<Integer> lessThan9 = number -> number < 9; 
  13.         Predicate<Integer> 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<Integer> 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<Integer> numberList = Arrays.asList(3, 4, 5, 6, 7, 8, 9, 10); 
  10.         Predicate<Integer> 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<Integer> numberList = Arrays.asList(3, 4, 5, 6, 7, 8, 9, 10); 
  10.  
  11.         Predicate<Integer> lessThan5 = number -> number <= 5; 
  12.         Predicate<Integer> 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<Integer> numberList = Arrays.asList(3, 4, 5, 6, 7, 8, 9, 10); 
  10.  
  11.         Predicate<Integer> lessThan5 = number -> number <= 5; 
  12.         Predicate<Integer> 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 <T> List<T> filter(List<T> list, Predicate<T> predicate) { 
  25.         List<T> 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<Dog> 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<Dog> predicate = dog -> ("哈士奇").equals(dog.getName()); 
  18.         System.out.println(filter(dogList, predicate)); 
  19.     } 
  20.  
  21.     public static <T> List<T> filter(List<T> list, Predicate<T> predicate) { 
  22.         List<T> 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 nameInteger 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<T, U> { 
  6.     boolean test(T t, U u); 
  7.  
  8.     default BiPredicate<T, U> and(BiPredicate<? super T, ? super U> other) { 
  9.         Objects.requireNonNull(other); 
  10.         return (T t, U u) -> test(t, u) && other.test(t, u); 
  11.     } 
  12.  
  13.     default BiPredicate<T, U> negate() { 
  14.         return (T t, U u) -> !test(t, u); 
  15.     } 
  16.  
  17.     default BiPredicate<T, U> or(BiPredicate<? super T, ? super U> other) { 
  18.         Objects.requireNonNull(other); 
  19.         return (T t, U u) -> test(t, u) || other.test(t, u); 
  20.     } 

 

責(zé)任編輯:武曉燕 來(lái)源: 未讀代碼
相關(guān)推薦

2021-04-14 07:33:02

Java函數(shù)式斷言

2023-07-26 07:13:55

函數(shù)接口Java 8

2019-08-05 08:05:27

Java開(kāi)發(fā)代碼

2020-05-25 16:25:17

Java8Stream函數(shù)式接口

2023-10-23 14:16:01

Java函數(shù)式編程

2024-02-28 08:37:28

Lambda表達(dá)式Java函數(shù)式接口

2020-10-16 10:07:03

Lambda表達(dá)式Java8

2022-12-26 07:47:37

JDK8函數(shù)式接口

2024-07-18 08:00:00

2024-06-12 00:00:01

Java函數(shù)式接口

2015-12-21 11:47:22

Java 8組合函數(shù)

2023-12-22 16:39:47

Java函數(shù)式接口開(kāi)發(fā)

2022-01-19 11:19:25

Java/接口/代碼

2015-09-28 14:54:08

Java函數(shù)式編程

2015-09-30 09:34:09

java8字母序列

2022-11-30 16:58:01

Java系統(tǒng)函數(shù)式

2024-08-19 02:00:00

FunctionJava8接口

2020-09-22 11:00:11

Java技術(shù)開(kāi)發(fā)

2022-01-06 07:39:17

Java Default關(guān)鍵字 Java 基礎(chǔ)

2020-10-19 13:03:16

Java 8接口抽象類
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)