YouTip LogoYouTip

Java8 Optional Class

Java 8 Optional Class \\n\\n[![Image 1: Java 8 New Features](#) Java 8 New Features](#)\\n\\n* * *\\n\\nOptional is a container object that may or may not contain a null value. If a value is present, isPresent() will return true, and get() will return the value.\\n\\nOptional is a container: it can hold a value of type T, or just null. Optional provides many useful methods, so we don't have to explicitly check for null values.\\n\\nThe introduction of the Optional class solves the null pointer exception problem very well.\\n\\n### Class Declaration\\n\\nThe following is the declaration of the **java.util.Optional** class:\\n\\npublic final class Optionalextends Object\\n\\n### Class Methods\\n\\n| No. | Method & Description |\\n| --- | --- |\\n| 1 | **static Optional empty()** Returns an empty Optional instance. |\\n| 2 | **boolean equals(Object obj)** Determines whether some other object is equal to Optional. |\\n| 3 | **Optional filter(Predicate<? super predicate)** If a value is present, and the value matches the given predicate, return an Optional describing the value, otherwise return an empty Optional. |\\n| 4 | ** Optional flatMap(Function<? super T,Optional> mapper)** If a value is present, return the result of applying the mapping function to the Optional's value, otherwise return an empty Optional |\\n| 5 | **T get()** If a value is present in this Optional, return the value, otherwise throw an exception: NoSuchElementException |\\n| 6 | **int hashCode()** Return the hash code of the present value, if no value is present return 0. |\\n| 7 | **void ifPresent(Consumer consumer)** If a value is present, invoke the specified consumer with the value, otherwise do nothing. |\\n| 8 | **boolean isPresent()** Return true if there is a value present, otherwise return false. |\\n| 9 | **Optional map(Function mapper)** If a value is present, apply the provided mapping function to it. If the result is non-null, return an Optional describing the result, otherwise return an empty Optional. |\\n| 10 | **static Optional of(T value)** Return an Optional describing the specified non-null value. |\\n| 11 | **static Optional ofNullable(T value)** If a non-null value is present, return an Optional describing the value, otherwise return an empty Optional. |\\n| 12 | **T orElse(T other)** Return the value if present, otherwise return other. |\\n| 13 | **T orElseGet(Supplier other)** Return the value if present, otherwise invoke other and return the result of that invocation. |\\n| 14 | ** T orElseThrow(Supplier exceptionSupplier)** Return the value if present, otherwise throw an exception created by the provided Supplier. |\\n| 15 | **String toString()** Return a non-null string representation of this Optional suitable for debugging. |\\n\\n**Note:** These methods are inherited from the **java.lang.Object** class.\\n\\n* * *\\n\\n## Optional Instances\\n\\nWe can better understand the usage of the Optional class through the following example:\\n\\n## Java8Tester.java File\\n\\nimport java.util.Optional; public class Java8Tester{public static void main(String args[]){Java8Tester java8Tester = new Java8Tester(); Integer value1 = null; Integer value2 = new Integer(10); Optionala = Optional.ofNullable(value1); Optionalb = Optional.of(value2); System.out.println(java8Tester.sum(a,b)); }public Integer sum(Optionala, Optionalb){System.out.println("The first parameter value is present: " + a.isPresent()); System.out.println("The second parameter value is present: " + b.isPresent()); Integer value1 = a.orElse(new Integer(0)); Integer value2 = b.get(); return value1 + value2; }}\\n\\nExecute the above script, the output is:\\n\\n$ javac Java8Tester.java $ java Java8TesterThe first parameter value is present: falseThe second parameter value is present: true10\\n\\n* * Java 8 New Features](#)
← Java8 Datetime ApiJava8 Default Methods β†’

YouTip © 2024-2026 | Home | Learn Technology, Build Dreams!

All content is for educational and learning purposes only.