0% found this document useful (0 votes)
146 views7 pages

Java 8 Optional Class Overview

The Optional class in Java provides a type-safe way to represent null values. It allows for methods to return non-null values or indicate absence of a value. Optional has methods like empty(), of(), ofNullable(), isPresent(), get(), orElse(), filter(), map(), and flatMap() to work with optional values in a type-safe manner.

Uploaded by

Akhilesh Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
146 views7 pages

Java 8 Optional Class Overview

The Optional class in Java provides a type-safe way to represent null values. It allows for methods to return non-null values or indicate absence of a value. Optional has methods like empty(), of(), ofNullable(), isPresent(), get(), orElse(), filter(), map(), and flatMap() to work with optional values in a type-safe manner.

Uploaded by

Akhilesh Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
  • Optional Class

6.

Optional Class

 Optional is a container object which may or may not contain a non-null value.
 Purpose of Optional class is to provide a type-level solution for representing optional values
instead of null references.
 Optional class has the following methods.

static <T> Optional<T> empty();


static <T> Optional<T> of(T);
static <T> Optional<T> ofNullable(T);

T get();
boolean isPresent();
T orElse(T);

void ifPresent(Consumer<? super T>);


Optional<T> filter(Predicate<? super T>);

Optional<U> map(Function<? super T, ? extends U>);


Optional<U> flatMap(Function<? super T, Optional<U>>);

T orElseGet(Supplier<? extends T>);


T orElseThrow(Supplier<? extends X>) throws X;

Java Learning Center 61 Java 8 New Features


[Link]
package [Link];

import [Link];
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */

public class Demo1 {


public static void main(String[] args) {

// Optional<String> myopts = new Optional<String>();

//[Link]() method
Optional<String> myopts= [Link]();

[Link]("1. "+myopts);
[Link]("2. "+[Link]("Hello Guys"));
[Link]("3. "+myopts);
[Link]("4. "+[Link]());

//[Link]("5. "+[Link]());

if([Link]()) {
[Link]("5. "+[Link]());
}else {
[Link]("6. No value Found");
}

}
}

[Link]
package [Link];

import [Link];
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */

Java Learning Center 62 Java 8 New Features


public class Demo2 {
public static void main(String[] args) {

//[Link]() method
String str ="Srinivas";
Optional<String> myopts= [Link](str);

[Link]("1. "+myopts);
[Link]("2. "+[Link]("Hello Guys"));
[Link]("3. "+myopts);
[Link]("4. "+[Link]());
//[Link]("5. "+[Link]());

if([Link]()) {
[Link]("5. "+[Link]());
}else {
[Link]("6. No value Found");
}

}
}

[Link]
package [Link];

import [Link];
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo3 {

public static void main(String[] args) {

//[Link]() with null


String str =null;
Optional<String> myopts= [Link](str);
[Link](myopts);

}
}

Java Learning Center 63 Java 8 New Features


[Link]
package [Link];

import [Link];
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */

public class Demo4 {


public static void main(String[] args) {

//Optional. ofNullable () method


String str =null;
Optional<String> myopts= [Link](str);

[Link]("1. "+myopts);
[Link]("2. "+[Link]("Hello Guys"));
[Link]("3. "+myopts);
[Link]("4. "+[Link]());

if([Link]()) {
[Link]("5. "+[Link]());
}else {
[Link]("6. No value Found");
}

}
}

[Link]
package [Link];

import [Link];
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */

public class Demo5 {


public static void main(String[] args) {

Java Learning Center 64 Java 8 New Features


//Optional. ofNullable() method
String str ="Srinivas";
Optional<String> myopts= [Link](str);

[Link]("1. "+myopts);
[Link]("2. "+[Link]("Hello Guys"));
[Link]("3. "+myopts);
[Link]("4. "+[Link]());

if([Link]()) {
[Link]("5. "+[Link]());
}else {
[Link]("6. No value Found");
}

}
}

[Link]
package [Link];

import [Link];
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo6 {
public static void main(String[] args) {

//isPresent() Vs ifPresent()
String str =null;
//String str ="Srinivas";
Optional<String> myopts= [Link](str);

if([Link]()) {
[Link]([Link]());
}

[Link](input -> [Link](input));

if([Link]()) {
[Link]([Link]().toUpperCase());
}

Java Learning Center 65 Java 8 New Features


[Link](input -> [Link]([Link]()));

[Link]("-------------------");

[Link]("Done!!!");
}
}

[Link]
package [Link];

import [Link];
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo7 {
public static void main(String[] args) {

//filter() method
String str1 =null;
Optional<String> myopts1 = [Link](str1);

Optional<String> myopts4= [Link](input -> [Link]("Sri"));


[Link]("1 . "+myopts4);

String str2 ="Srinivas";


Optional<String> myopts2 = [Link](str2);

Optional<String> myopts5= [Link](input -> [Link]("Sri"));


[Link]("2 . "+myopts5);

String str3 ="Hello Guys";


Optional<String> myopts3 = [Link](str3);

Optional<String> myopts6= [Link](input -> [Link]("Sri"));


[Link]("3 . "+myopts6);

[Link]("Done!!!");

}
}

Java Learning Center 66 Java 8 New Features


[Link]
package [Link];

import [Link];
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo8 {
public static void main(String[] args) {

//map() method
String str1 ="Srinivas";
Optional<String> myopts1 = [Link](str1);
[Link]("1. "+myopts1);

Optional<String> myopts2 = [Link](input -> input);


[Link]("2. "+myopts2);

Optional<String> myopts3 = [Link](input -> [Link]());


[Link]("3. "+myopts3);

Optional<String> myopts4 = [Link](input -> new


StringBuilder(input).reverse().toString());

[Link]("4. "+myopts4);

String mystr = null;


Optional<String> myopts = [Link](mystr);
[Link]("5. "+myopts);

Optional<String> myopts5 = [Link](input -> [Link]());


[Link]("6. "+myopts5);

[Link]("Done!!!");
}
}

Java Learning Center 67 Java 8 New Features

Java Learning Center                                   61                                       Java 8 New Features
Java Learning Center                                   62                                       Java 8 New Features 
 
 
D
Java Learning Center                                   63                                       Java 8 New Features 
 
pub
Java Learning Center                                   64                                       Java 8 New Features 
 
Dem
Java Learning Center                                   65                                       Java 8 New Features 
 
//O
Java Learning Center                                   66                                       Java 8 New Features 
 
 
m
Java Learning Center                                   67                                       Java 8 New Features 
 
 
D

You might also like