6

Java 8 Optional Class with examples

 2 years ago
source link: http://adnjavainterview.blogspot.com/2023/08/java-8-optional-class-with-examples.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Java 8 Optional Class with examples

          In java8 introduced new class called Optional, using optional we can handle NullPointerException without using any null check condition.( Refer - Java 8 features) It provided many methods and using those it will be helpful to write cleanable code.

         Using optional if variable value is null then using orElse method we can have a null functionality code inside it.

 Below are the methods available in Optional class,

Java 8 optional class methods

The Optional.ofNullable() --- > method returns a Non-empty Optional if a value present in the given object. Otherwise returns an empty Optional. 

Optional.empty() -- > method is useful to create an empty Optional object.

Optional.of(value) --> creates an Optional object using value. 

Optional.ifPresent -- > If optional variable value is present, invoke the specified consumer with the value, otherwise, do nothing.

orElse --> If optional variable value is null then invokes orElse method.

 orElseGet --> If optional variable value is null then invokes orElse method and return the result of that invocation.

 orElseThrow --> If optional variable value is null then invokes orElseThrow method and throws the exception provided in the method.

 get -- > method returns the value from the optional.

 isPresent - if optional variable value is present/exist then return true else it's false.

Optional Class Code Example - 

import java.util.Optional;

public class OptionalClassUsages {
	
	public static void main(String[] args) {
		
		//ofNullable, orElse, orElseGet and orElseThrow method example
		String str = "Optional with ofNullable method usage";
		System.out.println(Optional.ofNullable(str).orElse("null logic"));
		
		str = null;
		System.out.println(Optional.ofNullable(str).orElse("null logic"));
		System.out.println(Optional.ofNullable(str).orElseGet(() -> "orElseGet method"));
		
		try {
			Optional.ofNullable(str).orElseThrow(() -> {
				return new Exception("orElseThrow Exception");
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		//optional of, ifPresent, filter usages
		Optional<String> optional = Optional.of("ab");
		optional.ifPresent(s-> System.out.println(s));
		optional.filter(s->s.equals("ab"))
				.ifPresent(s-> System.out.println("ifPresent method usage"));
		
		//isPresent and get method usage example
		System.out.println(optional.isPresent());
		System.out.println(optional.get());	
	}
}
Output:- 
Optional with ofNullable method usage
null logic
orElseGet method
java.lang.Exception: orElseThrow Exception
	at com.main.OptionalClassUsages.lambda$1(OptionalClassUsages.java:19)
	at java.util.Optional.orElseThrow(Optional.java:290)
	at com.main.OptionalClassUsages.main(OptionalClassUsages.java:18)
ab
ifPresent method usage
true
ab
Thank you for visiting the blog.
Reference posts:

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK