Predict interface of Java 8

The test () part of the predict interface is the source code

@FunctionalInterface
public interface Predicate<T> {
    /**
     * The specific filtering operation needs to be implemented by subclasses
     * Used to process whether the parameter t meets the requirements
     */
    boolean test(T t);

The test method is used to:

  • 1 . Evaluate the expression in the parameter (to put it bluntly, verify that the parameter passed in does not conform to the rules. There are examples later)
  • 2 . Its return value is a boolean type (note this).

Let's do the following

import java.util.function.Predicate;

/**
 * Let's verify the contradiction.
 * 1. Evaluate the expression in the parameter (to put it bluntly, verify that the parameter passed in does not conform to the rules. There are examples later)
 *    Let's verify this sentence and explain it.
 */
public class PredicateTestOne {

    public static void main(String[] args) {

        PredicateTestOne predicateTestOne = new PredicateTestOne();

        Predicate<String> predicate = new Predicate<String>() {
            @Override
            public boolean test(String s) {

                return s.equals("zhangsan");
            }
        };

        System.out.println(predicate.test("lisi"));
        System.out.println("--- --- --- --- --- ---");
        System.out.println(predicate.test("zhangsan"));
    }
}


Execution result:

false
--- --- --- --- --- ---
true

It's not difficult to understand why to say: evaluate the expression in the parameter

1. The test () method accepts a parameter
2. Judge whether this parameter conforms to the judgment in the test() method body

In this Code: we can see that the test() method body judges whether the parameter s passed in is equal to zhangsan, and the object Zhang San is passed in by external call.

But this violates the new feature of 1.8 - > functional programming, that is, what we need to do is take the function as the parameter, "to put it bluntly, we need to write less code and do more things"., It seems that we can't do this. If we want to judge whether the len gt h of the incoming parameter is greater than 5, or whether the incoming parameter is odd or even? Do we have to write multiple parameters as before? Of course, if you don't continue to do that, the emergence of Java 8 will be meaningless!

Let's deal with the above problems: (adopt traditional and new methods)

  • 1. Judge whether the length of the incoming string is greater than 5
  • 2. Judge whether the parameter passed in is even
  • 3. Judge whether the number is greater than 10

Traditional methods

import java.util.List;
import java.util.function.Predicate;

/**
 * - 1.Judge whether the length of the incoming string is greater than 5
 * - 2.Judge whether the passed in parameter is odd
 * - 3.Determine whether the number is greater than 10
 */
public class PredicateTestOne {

    public static void main(String[] args) {
        /** Let's start with the traditional way */
        /**  - 1.Judge whether the length of the incoming string is greater than 5 */
        PredicateTestOne predicateTestOne = new PredicateTestOne();

        System.out.println(predicateTestOne.judgeStringLength("hello"));
        System.out.println(predicateTestOne.judgenumbersOdds(4));
        System.out.println(predicateTestOne.judgeSpecialNumbers(-1));

    }

    /**
     *
     * - 1.Judge whether the length of the incoming string is greater than 5
     *
     * @param judgeString String to be judged
     * @return
     */
    public boolean judgeStringLength(String judgeString) {

        return judgeString.length() > 5 ? true:false;
    }

    /**
     *  - 2.Judge whether the passed in parameter is odd
     *
     * @param number        Number to be judged
     * @return               1 Represents an even number, and 0 represents an odd number
     */
    public int judgenumbersOdds(int number) {

        return number % 2 == 0 ? 1 : 0;
    }

    /**
     * - 3.Determine whether the number is greater than 10
     *
     * @param number        Number to be judged
     * @return               1. Represents greater than 10, and 0 represents less than 10
     */
    public int judgeSpecialNumbers(int number) {
        return number > 10 ? 1 : 0;
    }
}


Methods in Java 8

import java.util.function.Predicate;

/**
 * - 1.Judge whether the length of the incoming string is greater than 5
 * - 2.Judge whether the passed in parameter is an even number
 * - 3.Determine whether the number is greater than 10
 */
public class PredicateTestThree {

    public static void main(String[] args) {

        PredicateTestThree predicate = new PredicateTestThree();

        /** - 1.Judge whether the length of the incoming string is greater than 5 */
        System.out.println(predicate.judgeConditionByFunction(12345,value -> String.valueOf(value).length() > 5));
        /** - 2.Judge whether the passed in parameter is odd */
        System.out.println(predicate.judgeConditionByFunction(4,value -> value % 2 == 0));
        /** - 3.Determine whether the number is greater than 10 */
        System.out.println(predicate.judgeConditionByFunction(-1, value-> value > 10));
    }

    public boolean judgeConditionByFunction(int value,Predicate<Integer> predicate) {
        return predicate.test(value);
    }
}

To sum up, the above code mainly says two things

  • 1. Do more with less code
  • 2. Explain the function of the test() method - > the expression in the evaluation parameter

Self reprint
https://blog.csdn.net/qq_27416233/article/details/83418791

Keywords: Java

Added by Beyond Reality on Fri, 04 Mar 2022 22:39:04 +0200