Uses of Class java.util.Optional (Java SE 26 & JDK 26)
Description: use: module: java.base, package: java.util, class: Optional

# Uses of Class
java.util.Optional

Packages that use Optional
Package
Description
java.lang
Provides classes that are fundamental to the design of the Java programming language.
java.lang.classfile
Provides classfile parsing, generation, and transformation library.
java.lang.classfile.attribute
Provides interfaces describing `class` file attributes for the `java.lang.classfile` library.
java.lang.classfile.instruction
Provides interfaces describing code instructions for the `java.lang.classfile` library.
java.lang.constant
Classes and interfaces to represent nominal descriptors for run-time entities such as classes or method handles, and classfile entities such as constant pool entries or `invokedynamic` call sites.
java.lang.foreign
Provides low-level access to memory and functions outside the Java runtime.
java.lang.invoke
The `java.lang.invoke` package provides low-level primitives for interacting with the Java Virtual Machine.
java.lang.module
Classes to support module descriptors and creating configurations of modules by means of resolution and service binding.
java.net
Provides the classes for implementing networking applications.
java.net.http
HTTP Client and WebSocket APIs
java.security.interfaces
Provides interfaces for generating RSA (Rivest, Shamir and Adleman AsymmetricCipher algorithm) keys as defined in the RSA Laboratory Technical Note PKCS#1, and DSA (Digital Signature Algorithm) keys as defined in NIST's FIPS-186.
java.security.spec
Provides classes and interfaces for key specifications and algorithm parameter specifications.
java.util
Contains the collections framework, some internationalization support classes, a service loader, properties, random number generation, string parsing and scanning classes, base64 encoding and decoding, a bit array, and several miscellaneous utility classes.
java.util.spi
Service provider classes for the classes in the java.util package.
java.util.stream
Classes to support functional-style operations on streams of elements, such as map-reduce transformations on collections.
javax.naming.ldap.spi
Provides the Service Provider Interface for DNS lookups when performing LDAP operations.
javax.net.ssl
Provides classes for the secure socket package.
jdk.jshell.spi
Defines the Service Provider Interface for pluggable JShell execution engines.

- ## Uses of Optional in java.lang

  Methods in java.lang that return Optional
  Modifier and Type
  Method
  Description
  `Optional<String[]>`
  ProcessHandle.Info.`arguments()`
  Returns an array of Strings of the arguments of the process.
  `Optional<Integer>`
  Runtime.Version.`build()`
  Returns the build number.
  `Optional<String>`
  ProcessHandle.Info.`command()`
  Returns the executable pathname of the process.
  `Optional<String>`
  ProcessHandle.Info.`commandLine()`
  Returns the command line of the process.
  `Optional<DynamicConstantDesc<Boolean>>`
  Boolean.`describeConstable()`
  Returns an `Optional` containing the nominal descriptor for this instance.
  `Optional<DynamicConstantDesc<Byte>>`
  Byte.`describeConstable()`
  Returns an `Optional` containing the nominal descriptor for this instance.
  `Optional<DynamicConstantDesc<Character>>`
  Character.`describeConstable()`
  Returns an `Optional` containing the nominal descriptor for this instance.
  `Optional<ClassDesc>`
  Class.`describeConstable()`
  Returns a nominal descriptor for this instance, if one can be constructed, or an empty `Optional` if one cannot be.
  `Optional<Double>`
  Double.`describeConstable()`
  Returns an `Optional` containing the nominal descriptor for this instance, which is the instance itself.
  `final Optional<Enum.EnumDesc<E>>`
  Enum.`describeConstable()`
  Returns an enum descriptor `EnumDesc` for this instance, if one can be constructed, or an empty `Optional` if one cannot be.
  `Optional<Float>`
  Float.`describeConstable()`
  Returns an `Optional` containing the nominal descriptor for this instance, which is the instance itself.
  `Optional<Integer>`
  Integer.`describeConstable()`
  Returns an `Optional` containing the nominal descriptor for this instance, which is the instance itself.
  `Optional<Long>`
  Long.`describeConstable()`
  Returns an `Optional` containing the nominal descriptor for this instance, which is the instance itself.
  `Optional<DynamicConstantDesc<Short>>`
  Short.`describeConstable()`
  Returns an `Optional` containing the nominal descriptor for this instance.
  `Optional<String>`
  String.`describeConstable()`
  Returns an `Optional` containing the nominal descriptor for this instance, which is the instance itself.
  `Optional<Module>`
  ModuleLayer.`findModule(String name)`
  Returns the module with the given name in this layer, or if not in this layer, the parent layers.
  `static Optional<ProcessHandle>`
  ProcessHandle.`of(long pid)`
  Returns an `Optional<ProcessHandle>` for an existing native process.
  `Optional<String>`
  Runtime.Version.`optional()`
  Returns optional additional identifying build information.
  `Optional<ProcessHandle>`
  ProcessHandle.`parent()`
  Returns an `Optional<ProcessHandle>` for the parent process.
  `Optional<String>`
  Runtime.Version.`pre()`
  Returns the optional pre-release information.
  `Optional<Instant>`
  ProcessHandle.Info.`startInstant()`
  Returns the start time of the process.
  `Optional<Duration>`
  ProcessHandle.Info.`totalCpuDuration()`
  Returns the total cputime accumulated of the process.
  `Optional<String>`
  ProcessHandle.Info.`user()`
  Return the user of the process.

- ## Uses of Optional in java.lang.classfile

  Methods in java.lang.classfile that return Optional
  Modifier and Type
  Method
  Description
  `Optional<Signature.RefTypeSig>`
  Signature.TypeParam.`classBound()`
  Returns the class bound of the type parameter.
  `Optional<CodeModel>`
  MethodModel.`code()`
  Returns the body of this method, if there is one.
  `default <T extends Attribute<T>>
  Optional<T>`
  AttributedElement.`findAttribute(AttributeMapper<T> attr)`
  Finds an attribute by name.
  `Optional<Signature.ClassTypeSig>`
  Signature.ClassTypeSig.`outerType()`
  Returns the signature of the class that this class is a member of, only if this is a member class.
  `Optional<MethodModel>`
  CodeModel.`parent()`
  Returns the enclosing method, if known.
  `Optional<ClassModel>`
  FieldModel.`parent()`
  Returns the class model this field is a member of, if known.
  `Optional<ClassModel>`
  MethodModel.`parent()`
  Returns the class model this method is a member of, if known.
  `Optional<ClassEntry>`
  ClassModel.`superclass()`
  Returns the superclass of this class, if there is one.
  `Optional<ClassEntry>`
  ClassReader.`superclassEntry()`
  Returns the constant pool entry describing the name of the superclass, if any.
  Methods in java.lang.classfile with parameters of type Optional
  Modifier and Type
  Method
  Description
  `default CodeBuilder`
  CodeBuilder.`exceptionCatch(Label start, Label end, Label handler, Optional<ClassEntry> catchType)`
  Declares an exception table entry.
  `static Signature.TypeParam`
  Signature.TypeParam.`of(String identifier, Optional<Signature.RefTypeSig> classBound, Signature.RefTypeSig... interfaceBounds)`
  Returns a signature for a type parameter.

- ## Uses of Optional in java.lang.classfile.attribute

  Methods in java.lang.classfile.attribute that return Optional
  Modifier and Type
  Method
  Description
  `Optional<NameAndTypeEntry>`
  EnclosingMethodAttribute.`enclosingMethod()`
  Returns the name and type of the enclosing method, if the class is immediately enclosed by exactly one method or constructor.
  `default Optional<Utf8Entry>`
  EnclosingMethodAttribute.`enclosingMethodName()`
  Returns the name of the enclosing method, if the class is immediately enclosed by exactly one method or constructor.
  `default Optional<Utf8Entry>`
  EnclosingMethodAttribute.`enclosingMethodType()`
  Returns the name of the enclosing method, if the class is immediately enclosed by exactly one method or constructor.
  `default Optional<MethodTypeDesc>`
  EnclosingMethodAttribute.`enclosingMethodTypeSymbol()`
  Returns the name of the enclosing method, if the class is immediately enclosed by exactly one method or constructor.
  `Optional<Utf8Entry>`
  InnerClassInfo.`innerName()`
  Returns the simple name of this class, or empty if this class is anonymous.
  `Optional<Utf8Entry>`
  ModuleAttribute.`moduleVersion()`
  Returns the version of the module, if present.
  `Optional<Utf8Entry>`
  MethodParameterInfo.`name()`
  Returns the parameter name, if it has one.
  `Optional<ClassEntry>`
  InnerClassInfo.`outerClass()`
  Returns the class or interface of which this class is a member, if it is a member of a class or interface.
  `Optional<Utf8Entry>`
  ModuleRequireInfo.`requiresVersion()`
  Returns the required version of the required module, if present.
  Methods in java.lang.classfile.attribute with parameters of type Optional
  Modifier and Type
  Method
  Description
  `static EnclosingMethodAttribute`
  EnclosingMethodAttribute.`of(ClassEntry className, Optional<NameAndTypeEntry> method)`
  Returns an `EnclosingMethod` attribute.
  `static EnclosingMethodAttribute`
  EnclosingMethodAttribute.`of(ClassDesc className, Optional<String> methodName, Optional<MethodTypeDesc> methodType)`
  Returns an `EnclosingMethod` attribute.
  `static InnerClassInfo`
  InnerClassInfo.`of(ClassEntry innerClass, Optional<ClassEntry> outerClass, Optional<Utf8Entry> innerName, int flags)`
  Returns a nested class description.
  `static InnerClassInfo`
  InnerClassInfo.`of(ClassDesc innerClass, Optional<ClassDesc> outerClass, Optional<String> innerName, int flags)`
  Returns a nested class description.
  `static InnerClassInfo`
  InnerClassInfo.`of(ClassDesc innerClass, Optional<ClassDesc> outerClass, Optional<String> innerName, AccessFlag... flags)`
  Returns a nested class description.
  `static MethodParameterInfo`
  MethodParameterInfo.`of(Optional<Utf8Entry> name, int flags)`
  Returns a method parameter description.
  `static MethodParameterInfo`
  MethodParameterInfo.`of(Optional<String> name, AccessFlag... flags)`
  Returns a method parameter description.
  `static MethodParameterInfo`
  MethodParameterInfo.`ofParameter(Optional<String> name, int flags)`
  Returns a method parameter description.

- ## Uses of Optional in java.lang.classfile.instruction

  Methods in java.lang.classfile.instruction that return Optional
  Modifier and Type
  Method
  Description
  `Optional<ClassEntry>`
  ExceptionCatch.`catchType()`
  Returns the type of the exception to catch, or empty if this handler catches everything.
  Methods in java.lang.classfile.instruction with parameters of type Optional
  Modifier and Type
  Method
  Description
  `static ExceptionCatch`
  ExceptionCatch.`of(Label handler, Label tryStart, Label tryEnd, Optional<ClassEntry> catchTypeEntry)`
  Returns an exception table pseudo-instruction.

- ## Uses of Optional in java.lang.constant

  Methods in java.lang.constant that return Optional
  Modifier and Type
  Method
  Description
  `Optional<? extends ConstantDesc>`
  Constable.`describeConstable()`
  Returns an `Optional` containing the nominal descriptor for this instance, if one can be constructed, or an empty `Optional` if one cannot be constructed.

- ## Uses of Optional in java.lang.foreign

  Methods in java.lang.foreign that return Optional
  Modifier and Type
  Method
  Description
  `Optional<MemorySegment>`
  MemorySegment.`asOverlappingSlice(MemorySegment other)`
  Returns a slice of this segment that is the overlap between this and the provided segment.
  `Optional<MemorySegment>`
  SymbolLookup.`find(String name)`
  Returns the address of the symbol with the given name.
  `Optional<Object>`
  MemorySegment.`heapBase()`
  Returns the Java object stored in the on-heap region of memory backing this memory segment, if any.
  `Optional<String>`
  MemoryLayout.`name()`
  Returns the name (if any) associated with this layout.
  `Optional<MemoryLayout>`
  FunctionDescriptor.`returnLayout()`
  Returns the return layout (if any) of this function descriptor.
  `Optional<MemoryLayout>`
  AddressLayout.`targetLayout()`
  Returns the target layout associated with this address layout (if any).

- ## Uses of Optional in java.lang.invoke

  Methods in java.lang.invoke that return Optional
  Modifier and Type
  Method
  Description
  `Optional<MethodHandleDesc>`
  MethodHandle.`describeConstable()`
  Return a nominal descriptor for this instance, if one can be constructed, or an empty `Optional` if one cannot be.
  `Optional<MethodTypeDesc>`
  MethodType.`describeConstable()`
  Returns a nominal descriptor for this instance, if one can be constructed, or an empty `Optional` if one cannot be.
  `Optional<VarHandle.VarHandleDesc>`
  VarHandle.`describeConstable()`
  Return a nominal descriptor for this instance, if one can be constructed, or an empty `Optional` if one cannot be.

- ## Uses of Optional in java.lang.module

  Methods in java.lang.module that return Optional
  Modifier and Type
  Method
  Description
  `Optional<ModuleDescriptor.Version>`
  ModuleDescriptor.Requires.`compiledVersion()`
  Returns the version of the module if recorded at compile-time.
  `Optional<ModuleReference>`
  ModuleFinder.`find(String name)`
  Finds a reference to a module of a given name.
  `Optional<URI>`
  ModuleReader.`find(String name)`
  Finds a resource, returning a URI to the resource in the module.
  `Optional<ResolvedModule>`
  Configuration.`findModule(String name)`
  Finds a resolved module in this configuration, or if not in this configuration, the parent configurations.
  `final Optional<URI>`
  ModuleReference.`location()`
  Returns the location of this module's content, if known.
  `Optional<String>`
  ModuleDescriptor.`mainClass()`
  Returns the module main class.
  `default Optional<InputStream>`
  ModuleReader.`open(String name)`
  Opens a resource, returning an input stream to read the resource in the module.
  `Optional<String>`
  ModuleDescriptor.Requires.`rawCompiledVersion()`
  Returns the string with the possibly-unparseable version of the module if recorded at compile-time.
  `Optional<String>`
  ModuleDescriptor.`rawVersion()`
  Returns the string with the possibly-unparseable version of the module.
  `default Optional<ByteBuffer>`
  ModuleReader.`read(String name)`
  Reads a resource, returning a byte buffer with the contents of the resource.
  `Optional<ModuleDescriptor.Version>`
  ModuleDescriptor.`version()`
  Returns the module version.

- ## Uses of Optional in java.net

  Methods in java.net that return Optional
  Modifier and Type
  Method
  Description
  `Optional<SSLSession>`
  SecureCacheResponse.`getSSLSession()`
  Returns an `Optional` containing the `SSLSession` in use on the original connection that retrieved the network resource.

- ## Uses of Optional in java.net.http

  Methods in java.net.http that return Optional
  Modifier and Type
  Method
  Description
  `abstract Optional<Authenticator>`
  HttpClient.`authenticator()`
  Returns an `Optional` containing the `Authenticator` set on this client.
  `abstract Optional<HttpRequest.BodyPublisher>`
  HttpRequest.`bodyPublisher()`
  Returns an `Optional` containing the `HttpRequest.BodyPublisher` set on this request.
  `default Optional<String>`
  HttpResponse.`connectionLabel()`
  Returns if present, a label identifying the connection on which the response was received.
  `abstract Optional<Duration>`
  HttpClient.`connectTimeout()`
  Returns an `Optional` containing the connect timeout duration for this client.
  `abstract Optional<CookieHandler>`
  HttpClient.`cookieHandler()`
  Returns an `Optional` containing this client's `CookieHandler`.
  `abstract Optional<Executor>`
  HttpClient.`executor()`
  Returns an `Optional` containing this client's `Executor`.
  `Optional<String>`
  HttpHeaders.`firstValue(String name)`
  Returns an `Optional` containing the first header string value of the given named (and possibly multi-valued) header.
  `<T> Optional<T>`
  HttpRequest.`getOption(HttpOption<T> option)`
  Returns the value configured on this request for the given option, if any.
  `Optional<HttpResponse<T>>`
  HttpResponse.`previousResponse()`
  Returns an `Optional` containing the previous intermediate response if one was received.
  `abstract Optional<ProxySelector>`
  HttpClient.`proxy()`
  Returns an `Optional` containing the `ProxySelector` supplied to this client.
  `Optional<SSLSession>`
  HttpResponse.`sslSession()`
  Returns an `Optional` containing the `SSLSession` in effect for this response.
  `abstract Optional<Duration>`
  HttpRequest.`timeout()`
  Returns an `Optional` containing this request's timeout duration.
  `abstract Optional<HttpClient.Version>`
  HttpRequest.`version()`
  Returns an `Optional` containing the HTTP protocol version that will be requested for this `HttpRequest`.
  Method parameters in java.net.http with type arguments of type Optional
  Modifier and Type
  Method
  Description
  `static HttpResponse.BodyHandler<Void>`
  HttpResponse.BodyHandlers.`ofByteArrayConsumer(Consumer<Optional<byte[]>> consumer)`
  Returns a `BodyHandler<Void>` that returns a `BodySubscriber``<Void>` obtained from `BodySubscribers.ofByteArrayConsumer(Consumer)`.
  `static HttpResponse.BodySubscriber<Void>`
  HttpResponse.BodySubscribers.`ofByteArrayConsumer(Consumer<Optional<byte[]>> consumer)`
  Returns a `BodySubscriber` which provides the incoming body data to the provided Consumer of `Optional<byte[]>`.

- ## Uses of Optional in java.security.interfaces

  Methods in java.security.interfaces that return Optional
  Modifier and Type
  Method
  Description
  `Optional<byte[]>`
  EdECPrivateKey.`getBytes()`
  Get a copy of the byte array representing the private key.
  `Optional<byte[]>`
  XECPrivateKey.`getScalar()`
  Get the scalar value encoded as an unpruned byte array.

- ## Uses of Optional in java.security.spec

  Methods in java.security.spec that return Optional
  Modifier and Type
  Method
  Description
  `Optional<byte[]>`
  EdDSAParameterSpec.`getContext()`
  Get the context that the signature will use.

- ## Uses of Optional in java.util

  Methods in java.util that return Optional
  Modifier and Type
  Method
  Description
  `static <T> Optional<T>`
  Optional.`empty()`
  Returns an empty `Optional` instance.
  `Optional<T>`
  Optional.`filter(Predicate<? super T> predicate)`
  If a value is present, and the value matches the given predicate, returns an `Optional` describing the value, otherwise returns an empty `Optional`.
  `Optional<S>`
  ServiceLoader.`findFirst()`
  Load the first available service provider of this loader's service.
  `<U> Optional<U>`
  Optional.`flatMap(Function<? super T, ? extends Optional<? extends U>> mapper)`
  If a value is present, returns the result of applying the given `Optional`-bearing mapping function to the value, otherwise returns an empty `Optional`.
  `<U> Optional<U>`
  Optional.`map(Function<? super T, ? extends U> mapper)`
  If a value is present, returns an `Optional` describing (as if by `Optional.ofNullable(T)`) the result of applying the given mapping function to the value, otherwise returns an empty `Optional`.
  `static <T> Optional<T>`
  Optional.`of(T value)`
  Returns an `Optional` describing the given non-`null` value.
  `static <T> Optional<T>`
  Optional.`ofNullable(T value)`
  Returns an `Optional` describing the given value, if non-`null`, otherwise returns an empty `Optional`.
  `Optional<T>`
  Optional.`or(Supplier<? extends Optional<? extends T>> supplier)`
  If a value is present, returns an `Optional` describing the value, otherwise returns an `Optional` produced by the supplying function.
  Method parameters in java.util with type arguments of type Optional
  Modifier and Type
  Method
  Description
  `<U> Optional<U>`
  Optional.`flatMap(Function<? super T, ? extends Optional<? extends U>> mapper)`
  If a value is present, returns the result of applying the given `Optional`-bearing mapping function to the value, otherwise returns an empty `Optional`.
  `Optional<T>`
  Optional.`or(Supplier<? extends Optional<? extends T>> supplier)`
  If a value is present, returns an `Optional` describing the value, otherwise returns an `Optional` produced by the supplying function.

- ## Uses of Optional in java.util.spi

  Methods in java.util.spi that return Optional
  Modifier and Type
  Method
  Description
  `default Optional<String>`
  ToolProvider.`description()`
  Returns a short description of the tool, or an empty `Optional` if no description is available.
  `static Optional<ToolProvider>`
  ToolProvider.`findFirst(String name)`
  Returns the first instance of a `ToolProvider` with the given name, as loaded by `ServiceLoader` using the system class loader.

- ## Uses of Optional in java.util.stream

  Methods in java.util.stream that return Optional
  Modifier and Type
  Method
  Description
  `Optional<T>`
  Stream.`findAny()`
  Returns an `Optional` describing some element of the stream, or an empty `Optional` if the stream is empty.
  `Optional<T>`
  Stream.`findFirst()`
  Returns an `Optional` describing the first element of this stream, or an empty `Optional` if the stream is empty.
  `Optional<T>`
  Stream.`max(Comparator<? super T> comparator)`
  Returns the maximum element of this stream according to the provided `Comparator`.
  `Optional<T>`
  Stream.`min(Comparator<? super T> comparator)`
  Returns the minimum element of this stream according to the provided `Comparator`.
  `Optional<T>`
  Stream.`reduce(BinaryOperator<T> accumulator)`
  Performs a reduction on the elements of this stream, using an associative accumulation function, and returns an `Optional` describing the reduced value, if any.
  Methods in java.util.stream that return types with arguments of type Optional
  Modifier and Type
  Method
  Description
  `static <T> Collector<T, ?, Optional<T>>`
  Collectors.`maxBy(Comparator<? super T> comparator)`
  Returns a `Collector` that produces the maximal element according to a given `Comparator`, described as an `Optional<T>`.
  `static <T> Collector<T, ?, Optional<T>>`
  Collectors.`minBy(Comparator<? super T> comparator)`
  Returns a `Collector` that produces the minimal element according to a given `Comparator`, described as an `Optional<T>`.
  `static <T> Collector<T, ?, Optional<T>>`
  Collectors.`reducing(BinaryOperator<T> op)`
  Returns a `Collector` which performs a reduction of its input elements under a specified `BinaryOperator`.

- ## Uses of Optional in javax.naming.ldap.spi

  Methods in javax.naming.ldap.spi that return Optional
  Modifier and Type
  Method
  Description
  `abstract Optional<LdapDnsProviderResult>`
  LdapDnsProvider.`lookupEndpoints(String url, Map<?,?> env)`
  Lookup the endpoints and domain name for the given `Context` `provider URL` and environment.

- ## Uses of Optional in javax.net.ssl

  Methods in javax.net.ssl that return Optional
  Modifier and Type
  Method
  Description
  `Optional<SSLSession>`
  HttpsURLConnection.`getSSLSession()`
  Returns an `Optional` containing the `SSLSession` in use on this connection.

- ## Uses of Optional in jdk.jshell.spi

  Methods in jdk.jshell.spi that return Optional
  Modifier and Type
  Method
  Description
  `default Optional<JShellConsole>`
  ExecutionEnv.`console()`
  Returns the `JShellConsole` that should be used by the execution engine, or `null` if none.