[Sleeping JDK] Detailed explanation of Java functional programming interface Supplier

1. Introduction to Supplier

What are the benefits of Supplier? There are also different opinions on this on the Internet. Supplier may not be as easy to understand as Predicate , Consumer, and Function introduced before . The official introduction is:

Represents a provider of results that does not require new or different results to be returned each time the Supplier is called.

The source code is as follows:

@FunctionalInterface
public interface Supplier<T> {
    
    

    /**
     * Gets a result.
     *
     * @return a result
     */
    T get();
}

We can use Supplier to represent how to obtain a result. When we need the result, we can call the get() function to get the result. Give a simple example:

Suppliper<Person> supplier = Person::new;
Person p = supplier.get();

The above example describes how to obtain a Person object. Yes, it is a very simple example. This has also led many people to think that there is no need for Supplier. Indeed, if it is just a new simple object, using Supplier is indeed a bit of a problem. A masterpiece.

What you must understand is that using Supplier helps you obtain a result, which can be an object or a fixed value. When you find that a series of cumbersome operations are required to obtain a result, you can consider using Supplier to express how to obtain the result.

What needs to be noted here is not to confuse it with Consumer and Function. First of all, Supplier's get() function has no parameters, and its goal is to obtain results. Consumer and Function both need to pass in a parameter. Consumer focuses on the processing process, and Function focuses on the process and results.

2. Implement the class SingletonSupplier

If you only use the Supplier interface, it may be difficult to meet business needs in many cases, and you need to implement the Supplier interface. Take Spring as an example SingletonSupplier. Using it, we can easily maintain a singleton.

I won’t go into the specific source code, let’s see how to SingletonSupplierimplement a singleton:

public class Person {
    
    
    private static SingletonSupplier<Person> holder = new SingletonSupplier<>(null, Person::new);

    private Person() {
    
    }

    public static Person getInstance() {
    
    
        return holder.get();
    }
}

Is the implementation process simple? And this is still a lazy singleton, and a new instance will only be created when it is used.

3. Other variations of Supplier

Interface name parameter Return type describe
BooleanSupplier none boolean The result is of type Boolean
DoubleSupplier none boolean The result is of type Double
IntSupplier none boolean The result is of type int
LongSupplier none boolean The result is of type Long

Article recommendation:

[Sleeping JDK] Detailed explanation of Java functional programming interface Predicate

[Sleeping JDK] Detailed explanation of Java functional programming interface Consumer, Function

[Sleeping JDK] Detailed explanation of Java functional programming interface UnaryOperator, BinaryOperator
end
Java Development Paradise

Guess you like

Origin blog.csdn.net/u012534326/article/details/107031068