ServiceLoader findFirst() in Java

findFirst(): This method is available in java.util.ServiceLoader class of Java.

Syntax:

Optional<String> java.util.ServiceLoader.findFirst()

This method load the first available service provider of this loader's service.

Parameters: NA

Returns: The first service provider or empty Optional if no service providers are located.

Throws:

ServiceConfigurationError - If a provider class cannot be loaded.

Approach

Java

import java.util.ServiceLoader;

public class ServiceLoaderfindFirst {
    public static void main(String[] args) {

        ServiceLoader<String> serviceLoader =
ServiceLoader.load(String.class);

        System.out.println(serviceLoader.findFirst());
    }

}

Output:

Optional.empty


No comments:

Post a Comment