Hi,
Hope you are all doing great. Let's learn together.
Join Telegram: 📲 https://t.me/contactajoydebnath
Chapters❤
0:00 Introduction
00:47 Optional Class Theory
02:07 Why Do Developers Need Optional in Java?
05:49 Why Do Developers Need Optional in Java? Coding
18:51 Optional implementation
24:28 empty() method
27:21 of(value) method
30:34 ofNullable(value) method
32:45 isPresent() method
34:19 ifPresent(Consumer consumer) method
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 :
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:...
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, ...
'super' keyword - • 'super' keyword in Java. Bengali-Ep:13 #b...
'this' keyword - • 'this' keyword in Java. Bengali-Ep:12 #be...
Static Keyword, Static variable, block, method, nested class - • Static keyword, Static variables, block, m...
Access Modifier. Private, Public, Default, Protected -- • Access Modifier. Private, Public, Default,...
Spring Video Links:
Model-View-Controller(MVC), Spring MVC. -- • Model-View-Controller(MVC), Spring MVC. Sp...
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...
@Configuration, @Bean, @Component, @ComponentScan in Spring. -- • @Configuration, @Bean, @Component, @Compon...
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.