Guide To Java 8 Optional Class. Part-II Bengali-Ep:31

Опубликовано: 24 Июнь 2026
на канале: Ajoy Debnath
125
7

Hi,
Hope you are all doing great. Let's learn together.

Join Telegram: 📲 https://t.me/contactajoydebnath

Chapters❤
0:00 Introduction
00:51 Optional Class Theory
01:50 Why Do Developers Need Optional in Java?
05:00 Why Do Developers Need Optional in Java? Coding
17:25 orElse() method in Optional
24:15 orElseGet() method in Optional
31:29 orElseThrow() method in Optional
36:08 filter() method in Optional
46:58 map() method in Optional
54:33 Difference between orElse() and orElseGet()

Note:
Java was developed by Sun Microsystems (which is now the subsidiary of Oracle) in the year 1995. James Gosling is known as the father of Java. Before Java, its name was Oak. Since Oak was already a registered company, James Gosling and his team changed the name from Oak to Java.


Document Link -
https://docs.google.com/document/d/1q...

STS Link:
https://spring.io/tools

JDK Link:
https://www.oracle.com/java/technolog...

Video Links:
Core Java Playlist:
   • Core Java  

Spring Playlist :
   • Spring Framework  

Java Videos Links :
Guide To Java 8 Optional Class. Part-I. --    • Guide To Java 8 Optional Class. Part-I. Be...  
Stream API : Intermediate & Terminal operations. --    • Stream API : Intermediate & Terminal opera...  
Stream API: Intermediate & Terminal operations. --    • Stream API: Intermediate & Terminal operat...  
Factory Method Design Pattern. --    • Factory Method Design Pattern. Bengali-Ep:...  
How to create Immutable class in Java? Immutable Design Pattern. --    • How to create Immutable class in Java? Imm...  
Object Cloning, Shallow Copy and Deep Copy. --    • Object Cloning, Shallow Copy, and Deep Cop...  
Excetption, Custom Exception, throw, throws --    • Exception, Custom Exception, throw, throws...  
Excetption, try, catch, finally --    • Exception, Propagation try, catch, finally...  
Exception, Exception Hierarchy, Call Stack, Propagation. Bengali-Ep:22 --    • Exception, Exception Hierarchy, Call Stack...  
Lambda Expression, Functional Interfaces, Anonymous Class. --    • Lambda Expression, Functional Interfaces, ...  
Singleton Pattern. Lazy Instantiation for Multiple Threads.Part-2 Bengali-Ep:20    • Singleton Pattern. Lazy Instantiation for ...  
Singleton Design Pattern in Java. Early & Lazy Instantiation Bengali-Ep:19 :    • Singleton Design Pattern Java. Early & Laz...  
HashSet, Its Methods & How It Works Internally in Java :    • HashSet, Its Methods & How It Works Intern...  
HashSet & How It Works Internally in Java. --    • HashSet & How It Works Internally in Java....  

Spring Video Links:
JDBCTemplate, RowMapper,ResultSetExtractor,BeanPropertyRowMapper Spring --    • JDBCTemplate, RowMapper,ResultSetExtractor...  
Spring JDBCTemplate, JDBC, Statement and PreparedStatement --    • Spring JDBCTemplate, JDBC, Statement and P...  
What if we remove @Configuration annotation in Spring? --    • What if we remove the @Configuration annot...  

Like, Comment, Share, and Subscribe.

Thank you again.

Java 8 - Optional Class

Optional is a container object used to contain not-null objects. The Optional object is used to represent null with an absent value. It is introduced in Java 8 and is similar to what Optional is in Guava.

Why Do Developers Need Optional in Java?
Optional is generally used as a return type for methods that might not always have a result to return. For example, a method that looks up a user by ID might not find a match, in which case it would return an empty Optional.
Optional can help to reduce the number of null pointer exceptions in your code.

empty() – To create an empty Optional object, we simply need to use its empty() static method. Optional.empty();

of(T value) – It returns an Optional with the specified present non-null value.
Optional.of(value);

ofNullable(T value) – It returns an Optional describing the specified value, if non-null, otherwise returns an empty Optional.
Optional.ofNullable(value);

isPresent() – It returns true if there is a value present, otherwise false.
opt.isPresent();

ifPresent(Consumer consumer) – If a value is present, invoke the specified consumer with the value, otherwise do nothing.


orElse() – The orElse() method is used to retrieve the value wrapped inside an Optional instance. It takes one parameter, which acts as a default value. The orElse() method returns the wrapped value if it's present, and its argument otherwise.

orElseGet() – The orElseGet() method is similar to orElse(). However, instead of taking a value to return if the Optional value is not present, it takes a supplier functional interface, which is invoked and returns the value of the invocation:

orElseThrow() – The orElseThrow() method follows from orElse() and orElseGet() and adds a new approach for handling an absent value.

filter() – We can run an inline test on our wrapped value with the filter method. It takes a predicate as an argument and returns an Optional object. If the wrapped value passes testing by the predicate, then the Optional is returned.


map() – This operation transforms the value contained in an Optional, if present, and returns a new Optional of the transformed value. If not present, returns an empty Optional.