Java 8 coding preparation
- Anand Nerurkar
- Oct 3, 2023
- 1 min read
Updated: Nov 2, 2023












-- if element is repeated , then ans will be wrong

so make use of distnce on stream 1st ,then sort it as below to get correct output

if there is onlt 1 element in array, then exception is thrown

















Function.identity return same value which we pass as below



1st approach
===

2nd approach
==

ex:


3rd approach
--



below is the data





Optional
==
We can create optional object by below methos
empty()
this return optional.empty object
2.of(object)
if objct is null. then throw null pointerecxeption
3.ofNullable(obj)- if obj is null, then return optional.empty object


alays check ispresent as below

if obje is null, then it print default value as below

or you can throw exception

Comentarios