Рет қаралды 13,670
Java 8 optional | optional class | optional class interview questions | java 8 interview | okay java
Java 8 Optional class
Optional.empty()
Optional.of()
Optional.ofNullable()
.isPresent()
.ifPresent()
.get()
.orElse()
.orElseGet()
orElse v/s orElseGet()
.orElseThrow()
.filter()
.map()
.flatMap()
What is optional in java 8 ?
Optional is Final class in Java 8
to handle values as ‘available’ or ‘not available’ instead of checking null values.
What are the different ways to create an optional object
Optional.empty()
Optional.of()
Optional.ofNullable()
Optional.of() v/s Optional.ofNullable()
What is difference b/w Optional.of() and Optional.ofNullable() ?
Optional.of does not handle the "null" value , it gives null pointer exception.
.isPresent()
Boolean function
Returns true if there is a value present, otherwise false.
.get()
If a value is present in this Optional, returns the value, otherwise throws NoSuchElementException.
. ifPresent(consumer)
if a value is present, it invokes the specified consumer with the value, otherwise does nothing.
opStr2.ifPresent(s -> System.out.println(s.toUpperCase()));
. orElseThrow(supplier)
Returns the contained value, if present, otherwise throws an exception to be created by the provided supplier.
// orElseThrow
Optional < Integer > item = Optional.empty();
. filter(predicate)
If a value is present and the value matches a given predicate, it returns an Optional describing the value, otherwise returns an empty Optional.
// Filter - apply a predicate
Optional Integer age = Optional.of(20);
int a = age.filter(n -> n > 18).get();
System.out.println(a);
1. Java 8 new features
• Java 8 features | okay...
2. Java 8 functional interface part
• Java 8 functional inte...
3. Java 8 functional interface coding
• Java 8 functional inte...
4. Functional Interface prior to Java 8
• Functional Interface p...
5. Default methods in interface
• Default methods in int...
6. Default methods Diamond Problem in java 8
• Default methods Diamon...
7. Java 8 lambda expression
• Java 8 lambda expressi...
8. Lambda expression variable scope interview questions
• Lambda expression vari...
9. Java 8 predicate functional Interface
• Java 8 predicate funct...
10. Java 8 function interface
• Java 8 function interf...
11. Java 8 consumer functional interface
• Java 8 consumer functi...
12. java 8 supplier functional interface
• java 8 supplier functi...
13. Java 8 method reference
• Java 8 method referenc...
14. Java 8 optional class
• Java 8 optional class ...
15. Java 8 stream pipeline flow
• Java 8 stream pipeline...
16. Java 8 stream api operations
• Java 8 stream api oper...
17. create java 8 stream
• create java 8 steam | ...
18. stream api - create java 8 streams
• java 8 stream stream a...
19. java 8 stream api- range v/s rangeclosed
• java 8 stream api | ra...
20. java 8 stream api - filter | collect
• java 8 stream api | fi...
21. java 8 stream api - map function distinct | filter
• java 8 stream api | m...
22. java 8 stream api - flatmap
• java 8 stream api | fl...
23. java 8 interview question - sort employee list using stream api function
• java 8 interview quest...
24. java 8 interview question - built in functional interfaces in java
• java 8 interview quest...
25. Create Immutable class in Java
• Create Immutable class...
26. what is a web service ?
• what is a web service ...
27. Watch Introduction to Microservices 08.50 Minutes
• okay java | introducti...
28. Watch Microservices components in one video 20 Minutes
• okayjava | microservic...
29. Watch How to Install Spring Tool Suite on Windows Machine
• okay java | install sp...
Subscribe and share okayjava
www.youtube.co...