The JDK8 essay (05) _ Method References type continues to be long-winded, jdk8_method

Source: Internet
Author: User

The JDK8 essay (05) _ Method References type continues to be long-winded, jdk8_method
Type of Method References

I think the domestic leeching and arbitrary text theft are really serious...
If necessary, write an article every time. Source: blog.csdn.net/forevervip

In the past few days, I wanted to close the Method Reference and write a description of Aggregate, because Aggregate is a hybrid carrier of Lambda and Method Reference, but it has been plagued by a problem and cannot be understood, I re-read the generic model later, and finally remembered some underlying theories of JVM to solve the problem. We will discuss it later when writing it to Aggregate.

Method References static Method reference
public static int compareByAge(Person a, Person b) {        return a.birthday.compareTo(b.birthday);    }}

In the previous article, the reference method itself is a static method, so the static method can be referenced.
Written: Static class: Reference static method

Method References instance Method reference
class ComparisonProvider {    public int compareByName(Person a, Person b) {        return a.getName().compareTo(b.getName());    }    public int compareByAge(Person a, Person b) {        return a.getBirthday().compareTo(b.getBirthday());    }}ComparisonProvider myComparisonProvider = new ComparisonProvider();Arrays.sort(rosterAsArray, myComparisonProvider::compareByName);

Here, we refer to the compareByName method of myComparisonProvider of an instance of the ComparisonProvider class. We need to instantiate this class first and then perform method reference.
Written: Instantiation name: Reference Method

Method References type reference
String[] stringArray = { "Barbara", "James", "Mary", "John",    "Patricia", "Robert", "Michael", "Linda" };Arrays.sort(stringArray, String::compareToIgnoreCase);

In the example, String: compareToIgnoreCase is equivalent to calling (a, B)->. the Lambda expression of compareToIgnoreCase (B), which is used to compare two String types. Here the String: represents the type.
Written: Type name: Reference Method

Method References construction Method reference
public static <T, SOURCE extends Collection<T>, DEST extends Collection<T>>    DEST transferElements(        SOURCE sourceCollection,        Supplier<DEST> collectionFactory) {        DEST result = collectionFactory.get();        for (T t : sourceCollection) {            result.add(t);        }        return result;}

First, we define the above method.
There is a Supplier class,
This Supplier is a functional interface provided in JDK 8:

/* * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package java.util.function;/** * Represents a supplier of results. * * <p>There is no requirement that a new or distinct result be returned each * time the supplier is invoked. * * <p>This is a <a href="package-summary.html">functional interface</a> * whose functional method is {@link #get()}. * * @param <T> the type of results supplied by this supplier * * @since 1.8 */@FunctionalInterfacepublic interface Supplier<T> {    /**     * Gets a result.     *     * @return a result     */    T get();}

After writing so much nonsense, it is actually the following class after generic compilation and polishing:

public interface Supplier {    Object get();}

This class acts as a functional interface to return an instance of any defined (specified) generic type.
Suppose we want to define a HashSet during the call, the Lambda expression can be written as follows:

Set<Person> rosterSetLambda =    transferElements(roster, () -> { return new HashSet<>(); });

In this case, the method can be written as follows:

Set<Person> rosterSet = transferElements(roster, HashSet::new);

This is also the general practice when we need to get an instance, define a Supplier function interface, and then write the Class: new Method reference. This method is also used in later Aggregate, and Supplier is also called by java's own Aggregate api, so it is a very important usage.

Written: Class Name (constructor name): new Keyword

Source: blog.csdn.net/forevervip

There are already too many other...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.