<!doctypehtml PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html lang=en><title>
   Optional (Java Platform SE 8 )
  </title><meta content=2021-10-11 name=date><meta content="java.util.Optional class"name=keywords><meta content=empty() name=keywords><meta content=of() name=keywords><meta content=ofNullable() name=keywords><meta content=get() name=keywords><meta content=isPresent() name=keywords><meta content=ifPresent() name=keywords><meta content=filter() name=keywords><meta content=map() name=keywords><meta content=flatMap() name=keywords><meta content=orElse() name=keywords><meta content=orElseGet() name=keywords><meta content=orElseThrow() name=keywords><meta content=equals() name=keywords><meta content=hashCode() name=keywords><meta content=toString() name=keywords><link href=../../stylesheet.css rel=stylesheet title=Style><script src=../../script.js></script><script>window.ohcglobal || document.write('<script src="/en/dcommon/js/global.js">\x3C/script>')</script><body><script><!--
    try {
        if (location.href.indexOf('is-external=true') == -1) {
            parent.document.title="Optional (Java Platform SE 8 )";
        }
    }
    catch(err) {
    }
//-->
var methods = {"i0":9,"i1":10,"i2":10,"i3":10,"i4":10,"i5":10,"i6":10,"i7":10,"i8":10,"i9":9,"i10":9,"i11":10,"i12":10,"i13":10,"i14":10};
var tabs = {65535:["t0","All Methods"],1:["t1","Static Methods"],2:["t2","Instance Methods"],8:["t4","Concrete Methods"]};
var altColor = "altColor";
var rowColor = "rowColor";
var tableTab = "tableTab";
var activeTableTab = "activeTableTab";</script><noscript><div>JavaScript is disabled on your browser.</div></noscript><div class=topNav><a name=navbar.top>  </a><div class=skipNav><a title="Skip navigation links"href=#skip.navbar.top> Skip navigation links </a></div><a name=navbar.top.firstrow>  </a><ul class=navList title=Navigation><li><a href=../../overview-summary.html> Overview </a><li><a href=package-summary.html> Package </a><li class=navBarCell1Rev>Class<li><a href=class-use/Optional.html> Use </a><li><a href=package-tree.html> Tree </a><li><a href=../../deprecated-list.html> Deprecated </a><li><a href=../../index-files/index-1.html> Index </a><li><a href=../../help-doc.html> Help </a></ul><div class=aboutLanguage><strong> Java™ Platform <br> Standard Ed. 8 </strong></div></div><div class=subNav><ul class=navList><li><a title="interface in java.util"href=../../java/util/Observer.html> <span class=typeNameLink> Prev Class </span> </a><li><a title="class in java.util"href=../../java/util/OptionalDouble.html> <span class=typeNameLink> Next Class </span> </a></ul><ul class=navList><li><a href=../../index.html?java/util/Optional.html target=_top> Frames </a><li><a href=Optional.html target=_top> No Frames </a></ul><ul class=navList id=allclasses_navbar_top><li><a href=../../allclasses-noframe.html> All Classes </a></ul><div><script><!--
  allClassesLink = document.getElementById("allclasses_navbar_top");
  if(window==top) {
    allClassesLink.style.display = "block";
  }
  else {
    allClassesLink.style.display = "none";
  }
  //--></script></div><div><ul class=subNavList><li>Summary: <li>Nested | <li>Field | <li>Constr | <li><a href=#method.summary> Method </a></ul><ul class=subNavList><li>Detail: <li>Field | <li>Constr | <li><a href=#method.detail> Method </a></ul></div><a name=skip.navbar.top>  </a></div><div class=header><div class=subTitle>compact1, compact2, compact3</div><div class=subTitle>java.util</div><h2 title="Class Optional"class=title>Class Optional&LTT></h2></div><div class=contentContainer><ul class=inheritance><li><a title="class in java.lang"href=../../java/lang/Object.html> java.lang.Object </a><li><ul class=inheritance><li>java.util.Optional&LTT></ul></ul><div class=description><ul class=blockList><li class=blockList><hr> <br> <pre>public final class <span class=typeNameLabel>Optional&LTT></span>
extends <a title="class in java.lang"href=../../java/lang/Object.html>Object</a></pre> <div class=block>A container object which may or may not contain a non-null value. If a value is present, <code>
        isPresent()
       </code> will return <code>
        true
       </code> and <code>
        get()
       </code> will return the value. <p>Additional methods that depend on the presence or absence of a contained value are provided, such as <a href=../../java/util/Optional.html#orElse-T-> <code>
          orElse()
         </code> </a> (return a default value if value not present) and <a href=../../java/util/Optional.html#ifPresent-java.util.function.Consumer-> <code>
          ifPresent()
         </code> </a> (execute a block of code if the value is present).<p>This is a <a href=../lang/doc-files/ValueBased.html> value-based </a> class; use of identity-sensitive operations (including reference equality ( <code>
         ==
        </code> ), identity hash code, or synchronization) on instances of <code>
         Optional
        </code> may have unpredictable results and should be avoided.</div> <dl><dt><span class=simpleTagLabel> Since: </span><dd>1.8</dl></ul></div><div class=summary><ul class=blockList><li class=blockList> <ul class=blockList><li class=blockList><a name=method.summary>  </a> <h3>Method Summary</h3> <table summary="Method Summary table, listing methods, and an explanation"border=0 cellpadding=3 cellspacing=0 class=memberSummary><caption><span class=activeTableTab id=t0> <span> All Methods </span> <span class=tabEnd>   </span> </span> <span class=tableTab id=t1> <span> <a href=javascript:show(1);> Static Methods </a> </span> <span class=tabEnd>   </span> </span> <span class=tableTab id=t2> <span> <a href=javascript:show(2);> Instance Methods </a> </span> <span class=tabEnd>   </span> </span> <span class=tableTab id=t4> <span> <a href=javascript:show(8);> Concrete Methods </a> </span> <span class=tabEnd>   </span> </span></caption><tbody><tr><th class=colFirst scope=col>Modifier and Type<th class=colLast scope=col>Method and Description<tr class=altColor id=i0><td class=colFirst><code>
             static &LTT> 
             <a title="class in java.util"href=../../java/util/Optional.html> Optional </a>
             &LTT>
            </code><td class=colLast><code>
             <span class=memberNameLink> <a href=../../java/util/Optional.html#empty--> empty </a> </span>
             ()
            </code> <div class=block>Returns an empty <code>
              Optional
             </code> instance.</div><tr class=rowColor id=i1><td class=colFirst><code>
             boolean
            </code><td class=colLast><code>
             <span class=memberNameLink> <a href=../../java/util/Optional.html#equals-java.lang.Object-> equals </a> </span>
             (
             <a title="class in java.lang"href=../../java/lang/Object.html> Object </a>
              obj)
            </code> <div class=block>Indicates whether some other object is "equal to" this Optional.</div><tr class=altColor id=i2><td class=colFirst><code>
             <a title="class in java.util"href=../../java/util/Optional.html> Optional </a>
             <
             <a title="type parameter in Optional"href=../../java/util/Optional.html> T </a>
             >
            </code><td class=colLast><code>
             <span class=memberNameLink> <a href=../../java/util/Optional.html#filter-java.util.function.Predicate-> filter </a> </span>
             (
             <a title="interface in java.util.function"href=../../java/util/function/Predicate.html> Predicate </a>
             &LT? super
             <a title="type parameter in Optional"href=../../java/util/Optional.html> T </a>
             > predicate)
            </code> <div class=block>If a value is present, and the value matches the given predicate, return an <code>
              Optional
             </code> describing the value, otherwise return an empty <code>
              Optional
             </code> .</div><tr class=rowColor id=i3><td class=colFirst><code>
             &LTU> 
             <a title="class in java.util"href=../../java/util/Optional.html> Optional </a>
             &LTU>
            </code><td class=colLast><code>
             <span class=memberNameLink> <a href=../../java/util/Optional.html#flatMap-java.util.function.Function-> flatMap </a> </span>
             (
             <a title="interface in java.util.function"href=../../java/util/function/Function.html> Function </a>
             &LT? super
             <a title="type parameter in Optional"href=../../java/util/Optional.html> T </a>
             ,
             <a title="class in java.util"href=../../java/util/Optional.html> Optional </a>
             &LTU>> mapper)
            </code> <div class=block>If a value is present, apply the provided <code>
              Optional
             </code> -bearing mapping function to it, return that result, otherwise return an empty <code>
              Optional
             </code> .</div><tr class=altColor id=i4><td class=colFirst><code>
             <a title="type parameter in Optional"href=../../java/util/Optional.html> T </a>
            </code><td class=colLast><code>
             <span class=memberNameLink> <a href=../../java/util/Optional.html#get--> get </a> </span>
             ()
            </code> <div class=block>If a value is present in this <code>
              Optional
             </code> , returns the value, otherwise throws <code>
              NoSuchElementException
             </code> .</div><tr class=rowColor id=i5><td class=colFirst><code>
             int
            </code><td class=colLast><code>
             <span class=memberNameLink> <a href=../../java/util/Optional.html#hashCode--> hashCode </a> </span>
             ()
            </code> <div class=block>Returns the hash code value of the present value, if any, or 0 (zero) if no value is present.</div><tr class=altColor id=i6><td class=colFirst><code>
             void
            </code><td class=colLast><code>
             <span class=memberNameLink> <a href=../../java/util/Optional.html#ifPresent-java.util.function.Consumer-> ifPresent </a> </span>
             (
             <a title="interface in java.util.function"href=../../java/util/function/Consumer.html> Consumer </a>
             &LT? super
             <a title="type parameter in Optional"href=../../java/util/Optional.html> T </a>
             > consumer)
            </code> <div class=block>If a value is present, invoke the specified consumer with the value, otherwise do nothing.</div><tr class=rowColor id=i7><td class=colFirst><code>
             boolean
            </code><td class=colLast><code>
             <span class=memberNameLink> <a href=../../java/util/Optional.html#isPresent--> isPresent </a> </span>
             ()
            </code> <div class=block>Return <code>
              true
             </code> if there is a value present, otherwise <code>
              false
             </code> .</div><tr class=altColor id=i8><td class=colFirst><code>
             &LTU> 
             <a title="class in java.util"href=../../java/util/Optional.html> Optional </a>
             &LTU>
            </code><td class=colLast><code>
             <span class=memberNameLink> <a href=../../java/util/Optional.html#map-java.util.function.Function-> map </a> </span>
             (
             <a title="interface in java.util.function"href=../../java/util/function/Function.html> Function </a>
             &LT? super
             <a title="type parameter in Optional"href=../../java/util/Optional.html> T </a>
             ,? extends U> mapper)
            </code> <div class=block>If a value is present, apply the provided mapping function to it, and if the result is non-null, return an <code>
              Optional
             </code> describing the result.</div><tr class=rowColor id=i9><td class=colFirst><code>
             static &LTT> 
             <a title="class in java.util"href=../../java/util/Optional.html> Optional </a>
             &LTT>
            </code><td class=colLast><code>
             <span class=memberNameLink> <a href=../../java/util/Optional.html#of-T-> of </a> </span>
             (T value)
            </code> <div class=block>Returns an <code>
              Optional
             </code> with the specified present non-null value.</div><tr class=altColor id=i10><td class=colFirst><code>
             static &LTT> 
             <a title="class in java.util"href=../../java/util/Optional.html> Optional </a>
             &LTT>
            </code><td class=colLast><code>
             <span class=memberNameLink> <a href=../../java/util/Optional.html#ofNullable-T-> ofNullable </a> </span>
             (T value)
            </code> <div class=block>Returns an <code>
              Optional
             </code> describing the specified value, if non-null, otherwise returns an empty <code>
              Optional
             </code> .</div><tr class=rowColor id=i11><td class=colFirst><code>
             <a title="type parameter in Optional"href=../../java/util/Optional.html> T </a>
            </code><td class=colLast><code>
             <span class=memberNameLink> <a href=../../java/util/Optional.html#orElse-T-> orElse </a> </span>
             (
             <a title="type parameter in Optional"href=../../java/util/Optional.html> T </a>
              other)
            </code> <div class=block>Return the value if present, otherwise return <code>
              other
             </code> .</div><tr class=altColor id=i12><td class=colFirst><code>
             <a title="type parameter in Optional"href=../../java/util/Optional.html> T </a>
            </code><td class=colLast><code>
             <span class=memberNameLink> <a href=../../java/util/Optional.html#orElseGet-java.util.function.Supplier-> orElseGet </a> </span>
             (
             <a title="interface in java.util.function"href=../../java/util/function/Supplier.html> Supplier </a>
             &LT? extends
             <a title="type parameter in Optional"href=../../java/util/Optional.html> T </a>
             > other)
            </code> <div class=block>Return the value if present, otherwise invoke <code>
              other
             </code> and return the result of that invocation.</div><tr class=rowColor id=i13><td class=colFirst><code>
             &LTX extends
             <a title="class in java.lang"href=../../java/lang/Throwable.html> Throwable </a>
             >
             <br>
             <a title="type parameter in Optional"href=../../java/util/Optional.html> T </a>
            </code><td class=colLast><code>
             <span class=memberNameLink> <a href=../../java/util/Optional.html#orElseThrow-java.util.function.Supplier-> orElseThrow </a> </span>
             (
             <a title="interface in java.util.function"href=../../java/util/function/Supplier.html> Supplier </a>
             &LT? extends X> exceptionSupplier)
            </code> <div class=block>Return the contained value, if present, otherwise throw an exception to be created by the provided supplier.</div><tr class=altColor id=i14><td class=colFirst><code>
             <a title="class in java.lang"href=../../java/lang/String.html> String </a>
            </code><td class=colLast><code>
             <span class=memberNameLink> <a href=../../java/util/Optional.html#toString--> toString </a> </span>
             ()
            </code> <div class=block>Returns a non-empty string representation of this Optional suitable for debugging.</div></table> <ul class=blockList><li class=blockList><a name=methods.inherited.from.class.java.lang.Object>  </a> <h3>Methods inherited from class java.lang. <a title="class in java.lang"href=../../java/lang/Object.html> Object </a></h3> <code>
           <a href=../../java/lang/Object.html#clone--> clone </a>
           ,
           <a href=../../java/lang/Object.html#finalize--> finalize </a>
           ,
           <a href=../../java/lang/Object.html#getClass--> getClass </a>
           ,
           <a href=../../java/lang/Object.html#notify--> notify </a>
           ,
           <a href=../../java/lang/Object.html#notifyAll--> notifyAll </a>
           ,
           <a href=../../java/lang/Object.html#wait--> wait </a>
           ,
           <a href=../../java/lang/Object.html#wait-long-> wait </a>
           ,
           <a href=../../java/lang/Object.html#wait-long-int-> wait </a>
          </code></ul></ul></ul></div><div class=details><ul class=blockList><li class=blockList> <ul class=blockList><li class=blockList><a name=method.detail>  </a> <h3>Method Detail</h3> <a name=empty-->  </a> <ul class=blockList><li class=blockList><h4>empty</h4> <pre>public static &LTT> <a title="class in java.util"href=../../java/util/Optional.html>Optional</a>&LTT> empty()</pre> <div class=block>Returns an empty <code>
            Optional
           </code> instance. No value is present for this Optional.</div> <dl><dt><span class=simpleTagLabel> API Note: </span><dd>Though it may be tempting to do so, avoid testing if an object is empty by comparing with <code>
             ==
            </code> against instances returned by <code>
             Option.empty()
            </code> . There is no guarantee that it is a singleton. Instead, use <a href=../../java/util/Optional.html#isPresent--> <code>
              isPresent()
             </code> </a> .<dt><span class=paramLabel> Type Parameters: </span><dd><code>
             T
            </code> - Type of the non-existent value<dt><span class=returnLabel> Returns: </span><dd>an empty <code>
             Optional
            </code></dl></ul> <a name=of-java.lang.Object->  </a> <a name=of-T->  </a> <ul class=blockList><li class=blockList><h4>of</h4> <pre>public static &LTT> <a title="class in java.util"href=../../java/util/Optional.html>Optional</a>&LTT> of(T value)</pre> <div class=block>Returns an <code>
            Optional
           </code> with the specified present non-null value.</div> <dl><dt><span class=paramLabel> Type Parameters: </span><dd><code>
             T
            </code> - the class of the value<dt><span class=paramLabel> Parameters: </span><dd><code>
             value
            </code> - the value to be present, which must be non-null<dt><span class=returnLabel> Returns: </span><dd>an <code>
             Optional
            </code> with the value present<dt><span class=throwsLabel> Throws: </span><dd><code>
             <a title="class in java.lang"href=../../java/lang/NullPointerException.html> NullPointerException </a>
            </code> - if value is null</dl></ul> <a name=ofNullable-java.lang.Object->  </a> <a name=ofNullable-T->  </a> <ul class=blockList><li class=blockList><h4>ofNullable</h4> <pre>public static &LTT> <a title="class in java.util"href=../../java/util/Optional.html>Optional</a>&LTT> ofNullable(T value)</pre> <div class=block>Returns an <code>
            Optional
           </code> describing the specified value, if non-null, otherwise returns an empty <code>
            Optional
           </code> .</div> <dl><dt><span class=paramLabel> Type Parameters: </span><dd><code>
             T
            </code> - the class of the value<dt><span class=paramLabel> Parameters: </span><dd><code>
             value
            </code> - the possibly-null value to describe<dt><span class=returnLabel> Returns: </span><dd>an <code>
             Optional
            </code> with a present value if the specified value is non-null, otherwise an empty <code>
             Optional
            </code></dl></ul> <a name=get-->  </a> <ul class=blockList><li class=blockList><h4>get</h4> <pre>public <a title="type parameter in Optional"href=../../java/util/Optional.html>T</a> get()</pre> <div class=block>If a value is present in this <code>
            Optional
           </code> , returns the value, otherwise throws <code>
            NoSuchElementException
           </code> .</div> <dl><dt><span class=returnLabel> Returns: </span><dd>the non-null value held by this <code>
             Optional
            </code><dt><span class=throwsLabel> Throws: </span><dd><code>
             <a title="class in java.util"href=../../java/util/NoSuchElementException.html> NoSuchElementException </a>
            </code> - if there is no value present<dt><span class=seeLabel> See Also: </span><dd><a href=../../java/util/Optional.html#isPresent--> <code>
              isPresent()
             </code> </a></dl></ul> <a name=isPresent-->  </a> <ul class=blockList><li class=blockList><h4>isPresent</h4> <pre>public boolean isPresent()</pre> <div class=block>Return <code>
            true
           </code> if there is a value present, otherwise <code>
            false
           </code> .</div> <dl><dt><span class=returnLabel> Returns: </span><dd><code>
             true
            </code> if there is a value present, otherwise <code>
             false
            </code></dl></ul> <a name=ifPresent-java.util.function.Consumer->  </a> <ul class=blockList><li class=blockList><h4>ifPresent</h4> <pre>public void ifPresent(<a title="interface in java.util.function"href=../../java/util/function/Consumer.html>Consumer</a>&LT? super <a title="type parameter in Optional"href=../../java/util/Optional.html>T</a>> consumer)</pre> <div class=block>If a value is present, invoke the specified consumer with the value, otherwise do nothing.</div> <dl><dt><span class=paramLabel> Parameters: </span><dd><code>
             consumer
            </code> - block to be executed if a value is present<dt><span class=throwsLabel> Throws: </span><dd><code>
             <a title="class in java.lang"href=../../java/lang/NullPointerException.html> NullPointerException </a>
            </code> - if value is present and <code>
             consumer
            </code> is null</dl></ul> <a name=filter-java.util.function.Predicate->  </a> <ul class=blockList><li class=blockList><h4>filter</h4> <pre>public <a title="class in java.util"href=../../java/util/Optional.html>Optional</a><<a title="type parameter in Optional"href=../../java/util/Optional.html>T</a>> filter(<a title="interface in java.util.function"href=../../java/util/function/Predicate.html>Predicate</a>&LT? super <a title="type parameter in Optional"href=../../java/util/Optional.html>T</a>> predicate)</pre> <div class=block>If a value is present, and the value matches the given predicate, return an <code>
            Optional
           </code> describing the value, otherwise return an empty <code>
            Optional
           </code> .</div> <dl><dt><span class=paramLabel> Parameters: </span><dd><code>
             predicate
            </code> - a predicate to apply to the value, if present<dt><span class=returnLabel> Returns: </span><dd>an <code>
             Optional
            </code> describing the value of this <code>
             Optional
            </code> if a value is present and the value matches the given predicate, otherwise an empty <code>
             Optional
            </code><dt><span class=throwsLabel> Throws: </span><dd><code>
             <a title="class in java.lang"href=../../java/lang/NullPointerException.html> NullPointerException </a>
            </code> - if the predicate is null</dl></ul> <a name=map-java.util.function.Function->  </a> <ul class=blockList><li class=blockList><h4>map</h4> <pre>public &LTU> <a title="class in java.util"href=../../java/util/Optional.html>Optional</a>&LTU> map(<a title="interface in java.util.function"href=../../java/util/function/Function.html>Function</a>&LT? super <a title="type parameter in Optional"href=../../java/util/Optional.html>T</a>,? extends U> mapper)</pre> <div class=block>If a value is present, apply the provided mapping function to it, and if the result is non-null, return an <code>
            Optional
           </code> describing the result. Otherwise return an empty <code>
            Optional
           </code> .</div> <dl><dt><span class=simpleTagLabel> API Note: </span><dd>This method supports post-processing on optional values, without the need to explicitly check for a return status. For example, the following code traverses a stream of file names, selects one that has not yet been processed, and then opens that file, returning an <code>
             Optional&LTFileInputStream>
            </code> : <pre><code>
     Optional&LTFileInputStream> fis =
         names.stream().filter(name -> !isProcessedYet(name))
                       .findFirst()
                       .map(name -> new FileInputStream(name));
 </code></pre> Here, <code>
             findFirst
            </code> returns an <code>
             Optional&LTString>
            </code> , and then <code>
             map
            </code> returns an <code>
             Optional&LTFileInputStream>
            </code> for the desired file if one exists.<dt><span class=paramLabel> Type Parameters: </span><dd><code>
             U
            </code> - The type of the result of the mapping function<dt><span class=paramLabel> Parameters: </span><dd><code>
             mapper
            </code> - a mapping function to apply to the value, if present<dt><span class=returnLabel> Returns: </span><dd>an <code>
             Optional
            </code> describing the result of applying a mapping function to the value of this <code>
             Optional
            </code> , if a value is present, otherwise an empty <code>
             Optional
            </code><dt><span class=throwsLabel> Throws: </span><dd><code>
             <a title="class in java.lang"href=../../java/lang/NullPointerException.html> NullPointerException </a>
            </code> - if the mapping function is null</dl></ul> <a name=flatMap-java.util.function.Function->  </a> <ul class=blockList><li class=blockList><h4>flatMap</h4> <pre>public &LTU> <a title="class in java.util"href=../../java/util/Optional.html>Optional</a>&LTU> flatMap(<a title="interface in java.util.function"href=../../java/util/function/Function.html>Function</a>&LT? super <a title="type parameter in Optional"href=../../java/util/Optional.html>T</a>,<a title="class in java.util"href=../../java/util/Optional.html>Optional</a>&LTU>> mapper)</pre> <div class=block>If a value is present, apply the provided <code>
            Optional
           </code> -bearing mapping function to it, return that result, otherwise return an empty <code>
            Optional
           </code> . This method is similar to <a href=../../java/util/Optional.html#map-java.util.function.Function-> <code>
             map(Function)
            </code> </a> , but the provided mapper is one whose result is already an <code>
            Optional
           </code> , and if invoked, <code>
            flatMap
           </code> does not wrap it with an additional <code>
            Optional
           </code> .</div> <dl><dt><span class=paramLabel> Type Parameters: </span><dd><code>
             U
            </code> - The type parameter to the <code>
             Optional
            </code> returned by<dt><span class=paramLabel> Parameters: </span><dd><code>
             mapper
            </code> - a mapping function to apply to the value, if present the mapping function<dt><span class=returnLabel> Returns: </span><dd>the result of applying an <code>
             Optional
            </code> -bearing mapping function to the value of this <code>
             Optional
            </code> , if a value is present, otherwise an empty <code>
             Optional
            </code><dt><span class=throwsLabel> Throws: </span><dd><code>
             <a title="class in java.lang"href=../../java/lang/NullPointerException.html> NullPointerException </a>
            </code> - if the mapping function is null or returns a null result</dl></ul> <a name=orElse-java.lang.Object->  </a> <a name=orElse-T->  </a> <ul class=blockList><li class=blockList><h4>orElse</h4> <pre>public <a title="type parameter in Optional"href=../../java/util/Optional.html>T</a> orElse(<a title="type parameter in Optional"href=../../java/util/Optional.html>T</a> other)</pre> <div class=block>Return the value if present, otherwise return <code>
            other
           </code> .</div> <dl><dt><span class=paramLabel> Parameters: </span><dd><code>
             other
            </code> - the value to be returned if there is no value present, may be null<dt><span class=returnLabel> Returns: </span><dd>the value, if present, otherwise <code>
             other
            </code></dl></ul> <a name=orElseGet-java.util.function.Supplier->  </a> <ul class=blockList><li class=blockList><h4>orElseGet</h4> <pre>public <a title="type parameter in Optional"href=../../java/util/Optional.html>T</a> orElseGet(<a title="interface in java.util.function"href=../../java/util/function/Supplier.html>Supplier</a>&LT? extends <a title="type parameter in Optional"href=../../java/util/Optional.html>T</a>> other)</pre> <div class=block>Return the value if present, otherwise invoke <code>
            other
           </code> and return the result of that invocation.</div> <dl><dt><span class=paramLabel> Parameters: </span><dd><code>
             other
            </code> - a <code>
             Supplier
            </code> whose result is returned if no value is present<dt><span class=returnLabel> Returns: </span><dd>the value if present otherwise the result of <code>
             other.get()
            </code><dt><span class=throwsLabel> Throws: </span><dd><code>
             <a title="class in java.lang"href=../../java/lang/NullPointerException.html> NullPointerException </a>
            </code> - if value is not present and <code>
             other
            </code> is null</dl></ul> <a name=orElseThrow-java.util.function.Supplier->  </a> <ul class=blockList><li class=blockList><h4>orElseThrow</h4> <pre>public &LTX extends <a title="class in java.lang"href=../../java/lang/Throwable.html>Throwable</a>> <a title="type parameter in Optional"href=../../java/util/Optional.html>T</a> orElseThrow(<a title="interface in java.util.function"href=../../java/util/function/Supplier.html>Supplier</a>&LT? extends X> exceptionSupplier)
                                    throws X extends <a title="class in java.lang"href=../../java/lang/Throwable.html>Throwable</a></pre> <div class=block>Return the contained value, if present, otherwise throw an exception to be created by the provided supplier.</div> <dl><dt><span class=simpleTagLabel> API Note: </span><dd>A method reference to the exception constructor with an empty argument list can be used as the supplier. For example, <code>
             IllegalStateException::new
            </code><dt><span class=paramLabel> Type Parameters: </span><dd><code>
             X
            </code> - Type of the exception to be thrown<dt><span class=paramLabel> Parameters: </span><dd><code>
             exceptionSupplier
            </code> - The supplier which will return the exception to be thrown<dt><span class=returnLabel> Returns: </span><dd>the present value<dt><span class=throwsLabel> Throws: </span><dd><code>
             X
            </code> - if there is no value present<dd><code>
             <a title="class in java.lang"href=../../java/lang/NullPointerException.html> NullPointerException </a>
            </code> - if no value is present and <code>
             exceptionSupplier
            </code> is null<dd><code>
             X extends
             <a title="class in java.lang"href=../../java/lang/Throwable.html> Throwable </a>
            </code></dl></ul> <a name=equals-java.lang.Object->  </a> <ul class=blockList><li class=blockList><h4>equals</h4> <pre>public boolean equals(<a title="class in java.lang"href=../../java/lang/Object.html>Object</a> obj)</pre> <div class=block>Indicates whether some other object is "equal to" this Optional. The other object is considered equal if: <ul><li>it is also an <code>
              Optional
             </code> and;<li>both instances have no value present or;<li>the present values are "equal to" each other via <code>
              equals()
             </code> .</ul></div> <dl><dt><span class=overrideSpecifyLabel> Overrides: </span><dd><code>
             <a href=../../java/lang/Object.html#equals-java.lang.Object-> equals </a>
            </code>  in class  <code>
             <a title="class in java.lang"href=../../java/lang/Object.html> Object </a>
            </code><dt><span class=paramLabel> Parameters: </span><dd><code>
             obj
            </code> - an object to be tested for equality<dt><span class=returnLabel> Returns: </span><dd>{code true} if the other object is "equal to" this object otherwise <code>
             false
            </code><dt><span class=seeLabel> See Also: </span><dd><a href=../../java/lang/Object.html#hashCode--> <code>
              Object.hashCode()
             </code> </a> , <a title="class in java.util"href=../../java/util/HashMap.html> <code>
              HashMap
             </code> </a></dl></ul> <a name=hashCode-->  </a> <ul class=blockList><li class=blockList><h4>hashCode</h4> <pre>public int hashCode()</pre> <div class=block>Returns the hash code value of the present value, if any, or 0 (zero) if no value is present.</div> <dl><dt><span class=overrideSpecifyLabel> Overrides: </span><dd><code>
             <a href=../../java/lang/Object.html#hashCode--> hashCode </a>
            </code>  in class  <code>
             <a title="class in java.lang"href=../../java/lang/Object.html> Object </a>
            </code><dt><span class=returnLabel> Returns: </span><dd>hash code value of the present value or 0 if no value is present<dt><span class=seeLabel> See Also: </span><dd><a href=../../java/lang/Object.html#equals-java.lang.Object-> <code>
              Object.equals(java.lang.Object)
             </code> </a> , <a href=../../java/lang/System.html#identityHashCode-java.lang.Object-> <code>
              System.identityHashCode(java.lang.Object)
             </code> </a></dl></ul> <a name=toString-->  </a> <ul class=blockListLast><li class=blockList><h4>toString</h4> <pre>public <a title="class in java.lang"href=../../java/lang/String.html>String</a> toString()</pre> <div class=block>Returns a non-empty string representation of this Optional suitable for debugging. The exact presentation format is unspecified and may vary between implementations and versions.</div> <dl><dt><span class=overrideSpecifyLabel> Overrides: </span><dd><code>
             <a href=../../java/lang/Object.html#toString--> toString </a>
            </code>  in class  <code>
             <a title="class in java.lang"href=../../java/lang/Object.html> Object </a>
            </code><dt><span class=simpleTagLabel> Implementation Requirements: </span><dd>If a value is present the result must include its string representation in the result. Empty and present Optionals must be unambiguously differentiable.<dt><span class=returnLabel> Returns: </span><dd>the string representation of this instance</dl></ul></ul></ul></div></div><div class=bottomNav><a name=navbar.bottom>  </a><div class=skipNav><a title="Skip navigation links"href=#skip.navbar.bottom> Skip navigation links </a></div><a name=navbar.bottom.firstrow>  </a><ul class=navList title=Navigation><li><a href=../../overview-summary.html> Overview </a><li><a href=package-summary.html> Package </a><li class=navBarCell1Rev>Class<li><a href=class-use/Optional.html> Use </a><li><a href=package-tree.html> Tree </a><li><a href=../../deprecated-list.html> Deprecated </a><li><a href=../../index-files/index-1.html> Index </a><li><a href=../../help-doc.html> Help </a></ul><div class=aboutLanguage><strong> Java™ Platform <br> Standard Ed. 8 </strong></div></div><div class=subNav><ul class=navList><li><a title="interface in java.util"href=../../java/util/Observer.html> <span class=typeNameLink> Prev Class </span> </a><li><a title="class in java.util"href=../../java/util/OptionalDouble.html> <span class=typeNameLink> Next Class </span> </a></ul><ul class=navList><li><a href=../../index.html?java/util/Optional.html target=_top> Frames </a><li><a href=Optional.html target=_top> No Frames </a></ul><ul class=navList id=allclasses_navbar_bottom><li><a href=../../allclasses-noframe.html> All Classes </a></ul><div><script><!--
  allClassesLink = document.getElementById("allclasses_navbar_bottom");
  if(window==top) {
    allClassesLink.style.display = "block";
  }
  else {
    allClassesLink.style.display = "none";
  }
  //--></script></div><div><ul class=subNavList><li>Summary: <li>Nested | <li>Field | <li>Constr | <li><a href=#method.summary> Method </a></ul><ul class=subNavList><li>Detail: <li>Field | <li>Constr | <li><a href=#method.detail> Method </a></ul></div><a name=skip.navbar.bottom>  </a></div><p class=legalCopy><small> <font size=-1> <a href=http://bugreport.sun.com/bugreport/> Submit a bug or feature </a> <br> For further API reference and developer documentation, see <a href=https://docs.oracle.com/javase/8/docs/index.html target=_blank> Java SE Documentation </a> . That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples. <br> <a href=../../../legal/cpyr.html> Copyright </a> © 1993, 2021, Oracle and/or its affiliates. All rights reserved. Use is subject to <a href=http://download.oracle.com/otndocs/jcp/java_se-8-mrel-spec/license.html> license terms </a> . Also see the <a href=http://www.oracle.com/technetwork/java/redist-137594.html target=_blank> documentation redistribution policy </a> . </font> </small></p><script src=https://www.oracleimg.com/us/assets/metrics/ora_docs.js></script><noscript><p>Scripting on this page tracks web page traffic, but does not change the content in any way.</p></noscript>