How to use Optional Class in Java8

Posted By : Dinesh Kumar | 27-Jul-2018

Java introduced another class Optional in jdk8. It is a public last class and used to manage NullPointerException in Java application. You must import java.util bundle to utilize this class. It gives methods which are utilized to check the nearness of significant worth for particular variable. 

1. Case: Java Program without utilizing Optional 

In the accompanying case, we are not utilizing Optional class. This program terminates strangely and throws a nullPointerException. 

<html>



public class OptionalExample { 

public static void main(String[] args) { 

String[] str = new String[10]; 

String lowercaseString = str[5].toLowerCase(); 

System.out.print(lowercaseString); 

} 

} 

</html>

Output: 

Exception in thread "main" java.lang.NullPointerException 

at lambdaExample.OptionalExample.main(OptionalExample.java:6) 

2.  Case: Java Program with utilizing Optional : To avoid the strange termination, we utilize Optional class. In the accompanying illustration, we are utilizing Optional. In this way, our program can execute without slamming. 

Java Optional Example: If Value isn't Present 

<html>




import java.util.Optional; 

public class OptionalExample { 

public static void main(String[] args) { 

String[] str = new String[10]; 

Optional<String> checkNull = Optional.ofNullable(str[5]); 

if(checkNull.isPresent()){/check for esteem is present or not 

String lowercaseString = str[5].toLowerCase(); 

System.out.print(lowercaseString); 

}else 

System.out.println("string esteem isn't present"); 

} 

} 

</html>

Output: 

string esteem isn't present 

Java Optional Example: If Value is Present 

<html>


import java.util.Optional; 

public class OptionalExample { 

public static void main(String[] args) { 

String[] str = new String[10]; 

str[5] = "JAVA OPTIONAL CLASS EXAMPLE";//Setting an incentive for 5th file 

Optional<String> checkNull = Optional.ofNullable(str[5]); 

if(checkNull.isPresent()){/It Checks, esteem is present or not 

String lowercaseString = str[5].toLowerCase(); 

System.out.print(lowercaseString); 

}else 

System.out.println("String esteem isn't present"); 

} 

} 

</html>


Output: 

java optional class illustration 

Another Java Optional Example 

<html>




import java.util.Optional; 

public class OptionalExample { 

public static void main(String[] args) { 

String[] str = new String[10]; 

str[5] = "JAVA OPTIONAL CLASS EXAMPLE";/Setting an incentive for 5th file 

Optional<String> checkNull = Optional.ofNullable(str[5]); 

checkNull.ifPresent(System.out::println);/printing an incentive by utilizing method reference 

System.out.println(checkNull.get());/printing an incentive by utilizing get method 

System.out.println(str[5].toLowerCase()); 

} 

} 

</html>

Output: 

JAVA OPTIONAL CLASS EXAMPLE 

JAVA OPTIONAL CLASS EXAMPLE 

java optional class case 

Java Optional Methods Example 

<html>



import java.util.Optional; 

public class OptionalExample { 

public static void main(String[] args) { 

String[] str = new String[10]; 

str[5] = "JAVA OPTIONAL CLASS EXAMPLE";/Setting an incentive for 5th record 

//It returns an empty instance of Optional class 

Optional<String> empty = Optional.empty(); 

System.out.println(empty); 

//It returns a non-empty Optional 

Optional<String> esteem = Optional.of(str[5]); 



System.out.println("esteem: "+value.filter((s)- >s.equals("Abc"))); 

System.out.println("Filtered esteem: "+value.filter((s)- >s.equals("JAVA OPTIONAL CLASS EXAMPLE"))); 

//It returns estimation of an Optional. on the off chance that esteem isn't present, it throws a NoSuchElementException 

System.out.println("Getting esteem: "+value.get()); 

//It returns hashCode of the esteem 

System.out.println("Getting hashCode: "+value.hashCode()); 

//It returns true if esteem is present, otherwise false 

System.out.println("Is esteem present: "+value.isPresent()); 

//It returns non-empty Optional if esteem is present, otherwise returns an empty Optional 

System.out.println("Nullable Optional: "+Optional.ofNullable(str[5])); 

//It returns esteem if accessible, otherwise returns determined esteem, 

System.out.println("orElse: "+value.orElse("Value isn't present")); 

System.out.println("orElse: "+empty.orElse("Value isn't present")); 

value.ifPresent(System.out::println);/printing an incentive by utilizing method reference 

} 

} 

</html>

Output: 

Optional.empty 

Filtered esteem: Optional.empty 

Filtered esteem: Optional[JAVA OPTIONAL CLASS EXAMPLE] 

Getting esteem: JAVA OPTIONAL CLASS EXAMPLE 

Getting hashCode: - 619947648 

Is esteem present: true 

Nullable Optional: Optional[JAVA OPTIONAL CLASS EXAMPLE] 

orElse: JAVA OPTIONAL CLASS EXAMPLE 

orElse: Value isn't present 

JAVA OPTIONAL CLASS EXAMPLE

About Author

Author Image
Dinesh Kumar

Dinesh Kumar is an experienced with knowledge of Spring Framework, Spring Boot, Java, Javascript, JQuery, AngularJs, and SQL.

Request for Proposal

Name is required

Comment is required

Sending message..