Java common function interface - Predicate Interface

 

JDK provides a number of interface functions common to enrich Lambda typical usage scenario, they are mainly  supplied java.util.function package. The following is a simple example of the use of Predicate interfaces.

Predicate Interface Overview

Sometimes we need to be determined for certain types of data, to thereby obtain a boolean result. Then you can use java.util.function.Predicate <T> interface.

 

Abstract method: test

Predicate interfaces include an abstract methods: Boolean Test (T T) . Conditions for determining a scene:

import java.util.function.Predicate;

public class Demo01Predicate {
    public static void main(String[] args) {
        method(s -> s.length() > 5);
    }

    private static void method(Predicate<String> predicate) {
        boolean veryLong = predicate.test("HelloWorld");
        System.out.println("字符串很长吗:" + veryLong);
    }
}

Run the program, the console output:

String long do: to true

Conditional standard logic is passed Lambda expressions, as long as the length of the string is greater than 5 is considered very long.

 

The default method: and

Since it is conditional, it will exist and, or, not three common logic. Wherein the two when the conditions employed Predicate "and" logical connection implemented together "and" in effect, use the default method and. JDK source code for it:

default Predicate<T> and(Predicate<? super T> other) {
    Objects.requireNonNull(other);
    return (t) -> test(t) && other.test(t);
}

To determine if a string contains both uppercase "H", but also contains uppercase "W", then:

Import java.util.function.Predicate; 

public  class DemoPredicateAnd {
     public  static  void main (String [] args) {
         Boolean isValid = Method (
                 // String.Contains () method, only when this value character string contains the specified sequence returns to true. 
                S -> s.contains ( "H" ), 
                S -> s.contains ( "W is" ) 
        ); 
        System.out.println ( "strings do meet the requirements:" + isValid); 
    } 

    Private  static  Boolean Method (the Predicate <String> One, the Predicate <String> TWO) {
         Boolean isValid = one.and(two).test("Hello world");
        return isValid;
    }
}

Run the program, the console output:

Strings do meet the requirements: false

 

The default method: or

And and "and" Similarly, the default method or implemented in the logic "or." JDK source code for:

default Predicate<T> or(Predicate<? super T> other) {
    Objects.requireNonNull(other);
    return (t) -> test(t) || other.test(t);
}

If you want to achieve a logical "string contains uppercase" H "or contain uppercase" W ", then the code only need to" and "changed to" or "name to the other are the same:

Import java.util.function.Predicate; 

public  class DemoPredicateOr {
     public  static  void main (String [] args) {
         Boolean isValid = Method (
                 // String.Contains () method, only when this value character string contains the specified sequence returns to true. 
                S -> s.contains ( "H" ), 
                S -> s.contains ( "W is" ) 
        ); 
        System.out.println ( "strings do meet the requirements:" + isValid); 
    } 

    Private  static  Boolean Method (the Predicate <String> One, the Predicate <String> TWO) {
         Boolean isValid = one.or(two).test("Hello world");
        return isValid;
    }
}

Run the program, the console output:

Strings do meet the requirements: to true

 

The default method: negate

"And", "or" have learned, the remaining "non" (inverted) will be simple. The default method negate the source code of the JDK:

default Predicate<T> negate() {
    return (t) -> !test(t);
}

After it is easy to see from the implementation, it is a method to perform a test, the results of boolean value "!" Instead been taken. Be sure to call before the test method

Call negate method, as and and or methods as:

import java.util.function.Predicate;

public class DemoPredicateNegate {
    public static void main(String[] args) {
        method(s -> s.length() > 5);
    }

    private static void method(Predicate<String> predicate) {
        boolean veryLong = predicate.negate().test("HelloWorld");
        System.out.println("字符串很长吗:" + veryLong);
    }
}

Run the program, the console output:

A long string it: false

 

Exercise: collection of information filtering

topic

Among an array of a number of "name + gender" of the following information, please screened to assemble a collection of strings Predicate interface will meet the requirements of the ArrayList, you need to meet two conditions:

1. Must for girls;

2. Name four words.

String [] array = { "Dilly Reba, F", "Gülnezer Bextiyar, female", "Marr JAH, M", "Zhao Liying, F"};

 

answer

Import of java.util.ArrayList;
 Import java.util.List;
 Import java.util.function.Predicate; 

public  class DemoPredicate {
     public  static  void main (String [] args) { 
        String [] Array = { "Dilly Reba, female "," Gülnezer Bextiyar, female "," Marr JAH, M "," Zhao Liying, F " }; 
        List <String> List = filter ( 
                Array, 
                S ->" F ".equals (s.split ( ",") [. 1 ]), 
                S -> s.split ( ",") [0] .length () ==. 4 
        ); 
        System.out.println (List);
    }

    private static List<String> filter(String[] array, Predicate<String> one, Predicate<String> two) {
        List<String> list = new ArrayList<>();
        for (String info : array) {
            if (one.and(two).test(info)) {
                list.add(info);
            }
        }
        return list;
    }
}

 

          

Guess you like

Origin www.cnblogs.com/liyihua/p/12286097.html