New features of java8 - method reference and constructor reference

  In the last article, I briefly learned about the four core functional interfaces built into java8. These interfaces can solve most of the problems we encounter in business scenarios. Today, let's briefly learn about method references and constructor references.

 

1. Method reference
 
 
Method reference: If the content in the lambda body already has a method implemented, we can use the method reference (it can be understood that the method reference is another representation of the lambda expression)

  

 

 

    @Test
    public void test1() {
        Consumer<String> con = (x) -> System.out.println(x);
        
        // The following writing has the same effect as the above. 
        PrintStream pr = System.out;
        Consumer<String> con1 = pr :: println;
    }

 

    To use a method reference, you must implement the method with the same return value and parameters as in the functional interface.

    Let's take a look at the accept method in the built-in Consumer interface in java8.

 

 /**
     * Performs this operation on the given argument.
     *
     * @param t the input argument
     */
    void accept(T t);

    Let's look at the println method again.

 1   /**
 2      * Prints a String and then terminate the line.  This method behaves as
 3      * though it invokes <code>{@link #print(String)}</code> and then
 4      * <code>{@link #println()}</code>.
 5      *
 6      * @param x  The <code>String</code> to be printed.
 7      */
 8     public void println(String x) {
 9         synchronized (this) {
10             print(x);
11             newLine();
12         }
13     }

    Because they have to return the same value and have the same parameters, you can use method references like this.

 

 

    @Test
    public void test2() {
        Comparator<Integer> com = (x, y) -> Integer.compare(x, y);
        int b = com.compare(5, 6);
        
        Comparator<Integer> com1 = Integer :: compare;
        int c = com1.compare(7, 8);
    }

    If it is a static method, first of all, the return value and parameters must be consistent with the compare method in Comparator , and then you can directly use the class name:: method name to refer to this method. The implementation in the above interface has been implemented by the compare method in Integer , and the return values ​​of the two methods are consistent with the parameters, so they can be used in this way.

 

1     @Test
2     public void test3() {
3         BiPredicate<String, String> bi = (x, y) -> x.equals(y);
4         
5         BiPredicate<String, String> bi1 = String :: equals;
6     }

 

    This type of reference is special. This type of reference requires that the type of the first input parameter must be the instance of the method that is called, and the second and second parameters must be the method parameter before the reference of this type can be used.

 

Second, the constructor reference

  Format: ClassName::new

1     @Test
2     public void test4() {
3         Supplier<Encoder> su = () -> new Encoder();
4         
5         Supplier<Encoder> su1 = Encoder :: new;
6     }

    The no-argument constructor is called when the above

1     @Test
2     public void test5() {
3         Function<Integer, Integer> fun = (x) -> new Integer(x);
4         
5         Function<Integer, Integer> fun1 = Integer :: new;
6     }

    The above is to call the parameterized constructor.

    What constructor is called by the above two methods mainly depends on the number and type of parameters in the functional interface.

 

3. Array reference

  Format: Type::new

1     @Test
2     public void test6() {
3         Function<Integer, String[]> fun = (x) -> new String[x];
4         
5         Function<Integer, String[]> fun1 = String[] :: new;
6     }

    In the above way, we can quickly create an array.

 

 

 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326710927&siteId=291194637