Supplier Functional Interface in Java 8 with Examples

In Java 8,the Supplier Functional Interface is used to represent a function that takes no arguments and produces a result.

Table of Content :

  • Introduction
  • What is Supplier Functional Interface in Java 8
  • Supplier Functional Interface in Java 8 - Examples
  • Supplier Functional Interface use cases
  • Advantages to use Supplier Functional Interface
  • Conclusion
  • The Supplier interface is a part of the java.util.function package and is defined as follows:

    @FunctionalInterface public interface Supplier<T> { T get(); }

    Here, T represents the type of the result produced by the function. The get() method takes no arguments and returns a result of type T.

    The Supplier interface can be used in scenarios where a function needs to be passed as an argument to another method or where a lambda expression needs to be created that produces a result.

    Supplier Functional Interface use cases

    Here are the some examples of how the Supplier interface can be used in Java 8:

    1. Lazy Initialization: You can use the Supplier interface to implement lazy initialization of an object. For example, let's say you have a heavy object that takes a long time to initialize and you want to initialize it only when it is needed. You can use the following code:

    public class HeavyObject { public HeavyObject() { // Heavy initialization code } } Supplier<HeavyObject> heavyObjectSupplier = HeavyObject::new; HeavyObject heavyObject = heavyObjectSupplier.get();

    In this example, the heavyObjectSupplier is a Supplier that creates a new HeavyObject when its get() method is called. By using a Supplier, you can delay the initialization of the HeavyObject until it is actually needed.

    2. Generating Random Numbers: You can also use the Supplier interface to generate random numbers. For example, let's say you want to generate a random integer between 1 and 100. You can use the following code:

    Supplier<Integer> randomSupplier = () -> (int) (Math.random() * 111) + 1; int randomInteger = randomSupplier.get();

    In this example, the randomSupplier is a Supplier that generates a random integer between 1 and 100 every time its get() method is called.

    3. Configuration Properties: You can use the Supplier interface to read configuration properties from a file or a database. For example, let's say you have a configuration file that contains the maximum number of threads that can be used by an application. You can use the following code:

    Properties configProperties = new Properties(); configProperties.load(new FileInputStream("config.properties")); Supplier<Integer> maxThreadsSupplier = () -> Integer.parseInt(configProperties.getProperty("maxThreads")); int maxThreads = maxThreadsSupplier.get();

    In this example, the maxThreadsSupplier is a Supplier that reads the maxThreads property from the configuration file every time its get() method is called.

    Advantages to use Supplier Functional Interface in Java8

    There are several advantages to using the Supplier Functional Interface in Java 8:

    • Lazy Initialization: The Supplier interface can be used to implement lazy initialization of an object. This means that an object is initialized only when it is needed, which can save memory and improve performance. By using a Supplier, you can delay the initialization of an object until it is actually needed.
    • Functional Composition: The Supplier interface can be composed with other functional interfaces such as Function and Predicate to create more complex operations. For example, you can compose a Supplier that generates a random number with a Function that multiplies that number by 2.
    • Stream Processing: The Supplier interface is used extensively in stream processing in Java 8. It allows you to generate a stream of elements on the fly, which can be useful for processing large amounts of data efficiently.
    • Readability: The use of the Supplier interface can make code more readable by making it clear what the function does and what its output type is. This can improve code maintenance and reduce the risk of errors caused by incorrect type conversions or mismatches.
    • Thread Safety: The use of the Supplier interface can improve thread safety by ensuring that the objects generated by the Supplier are thread-safe. This can prevent race conditions and other threading issues

    Conclusion

    Overall, the Supplier Functional Interface provides a flexible and powerful tool for defining functions that produce results in Java 8. By using Suppliers, you can simplify your code, delay initialization of objects until they are needed, generate random numbers, and read configuration properties from files or databases.