다음을 통해 공유


ServiceLoader.FindFirst Method

Definition

Load the first available service provider of this loader's service.

[Android.Runtime.Register("findFirst", "()Ljava/util/Optional;", "", ApiSince=34)]
public Java.Util.Optional? FindFirst ();
[<Android.Runtime.Register("findFirst", "()Ljava/util/Optional;", "", ApiSince=34)>]
member this.FindFirst : unit -> Java.Util.Optional

Returns

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

Attributes

Remarks

Load the first available service provider of this loader's service. This convenience method is equivalent to invoking the #iterator() iterator() method and obtaining the first element. It therefore returns the first element from the provider cache if possible, it otherwise attempts to load and instantiate the first provider.

The following example loads the first available service provider. If no service providers are located then it uses a default implementation.

{@code
               CodecFactory factory = ServiceLoader.load(CodecFactory.class)
                                                   .findFirst()
                                                   .orElse(DEFAULT_CODECSET_FACTORY);
            }

Added in 9.

Java documentation for java.util.ServiceLoader.findFirst().

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to