You are on page 1of 7

Ther

eisl
i
stofbel
owf
eat
uresofj
ava1.
8as
Funct
ional
int
erf
ace
LambdaExpr
essi
on
Def
aul
tmet
hod
St
ati
cmet
hod
ForeachMet
hod
Opt
ional
Class
St
ri
ngJoi
ner
s

Funct
ionalI
nter
face
AnI nt
erf
acethatcontai
nsexact
lyoneabst
ractmet
hodi
sknownas
functi
onali
nter
face.
f
oll
owi
ngar
ethef
unct
ioni
nter
faces(
sel
f-
study
)
pr
edi
cat
e
consumer
Funct
ion
suppl
i
er
I
tcanhaveanynumberofdef
aul
t,st
ati
cmet
hodsbutcancont
ainonl
y
oneabst
ractmet
hod.
Exampl
e-
packagecom.
test
;

@Funct
ional
Int
erf
ace
publ
ici
nter
faceTest{

v
oidget
Student
Name(
Str
ingname)
;

packagecom.
test
;

publ
iccl
assMai
nimpl
ement
sTest{
@Overr
ide
publ
icvoi
dgetSt
udentName(St
ri
ngname){
Syst
em.out.
pri
ntl
n(name)
;
}

publ
icst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){

Mai
nmai n=newMain(
);
mai
n.get
Student
Name(
"ashok"
);
}

LambdaExpr
essi
on-
Why
?
Lesscodi
ng

Sy
ntax
-(ar
gument
-l
ist
)->{
body
}

 Argument -l
ist
:Itcanbeemptyornon-
emptyaswell
.
 Arrow-token:Itis used tol i
nk ar
guments-
li
st and body of
expression.
 Body :I
t cont ai
ns expressi
ons and st
atements for l
ambda
expression.

NoPar
amet
erSy
ntax

(
)->{
/
/Bodyofnopar
amet
erl
ambda
}

OnePar
amet
erSy
ntax

(
p1)-
>{
/
/Bodyofsi
ngl
epar
amet
erl
ambda
}
TwoPar
amet
erSy
ntax

(
p1,
p2)->{
/
/Bodyofmul
ti
plepar
amet
erl
ambda
}
Exampl
e-
packagecom.
test
;

publ
ici
nter
faceAddi
ti
on{

i
ntadd(
inta,
i
ntb)
;

packagecom.
test
;

publ
iccl
assMai
n{

publ
icst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){

//Mult
ipl
eparametersinlambdaexpressi
on
Addi
ti
onadditi
on=( a,b)->(
a+b) ;
Syst
em.out.
pri
ntl
n(addit
ion.
add(
10,20));

//Mult
ipl
eparameterswit
hdat at
ypei
nlambdaexpr
essi
on
Addi
ti
onadditi
on2=( i
nta,i
ntb)->(a+b);
Syst
em.out.
pri
ntl
n(addi
ti
on2.add(
100,
200));
}
}
Output
30
300

Def
aul
tmet
hod
Javaprov
idesafaci
li
tytocreatedefaultmethodsi
nsidetheinter
face.
Methodswhicharedefi
nedinsidetheinter
faceandtaggedwi t
hdefault
areknownasdefaul
tmet hods.Thesemet hodsarenon-abst
ract
methods.
Exampl
e-
packagecom.
test
;

publ
ici
nter
faceExampl
e{

def
aul
tv oi
dm1(){
Syst out
em. .pr
int
ln(
"t
hisi
sdef
aul
tm1met
hod"
);
}
}

packagecom.
test
;

publ
iccl
assTest
Mai
nimpl
ement
sExampl
e{

publ
icst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){

TestMai
ntest
Mai
n=newTest
Mai
n()
;
test
Main.
m1()
;
}
}

Output
t
hisisdef
aul
tm1met
hod

Stat
icmet
hod-
Javapr
ovi
desafaci
l
ityt
ocr
eat
est
ati
cmet
hodsi
nsi
det
hei
nter
face.

packagecom.
demo;

publ
ici
nter
faceExampl
e{

st
ati
cvoidx1(
){
Syst
em.out
.pr
int
ln(
"t
hisi
sst
ati
cmet
hod"
);
}
}

packagecom.
demo;

publ
iccl
assMai
nTesti
mpl
ement
sExampl
e{

publ
icst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){
Exampl
e.x1(
);
}
}

f
orEach(
)met
hod-

TheJavaforEach(
)methodisautil
it
yfunct
iont
oit
erateoveracol
l
ect
ion
suchas(l
ist
,setormap)andstream.Iti
susedtoperf
orm agiv
enact
ion
oneachtheelementofthecol
l
ection.

packagecom.
test
;

i
mpor
tjav
a.ut
il
.HashMap;
i
mpor
tjav
a.ut
il
.Map;

publ
iccl
assMapDemo{

publ
icst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){

Map<St
ri
ng,
Str
ing>map=newHashMap<St
ri
ng,
Str
ing>(
);

map.
put
("
10"
,"ram")
;
map.
put
("
11"
,"shyam")
;
map.
put
("
12"
,"ganesh"
);

map.f
orEach(
(k,
v)-
>Sy
st out
em. .pr
int
ln(
"Key="+k+"
,
Val
ue="+v
));
}
}

Output
Key=11,Val
ue=shyam
Key=12,Val
ue=ganesh
Key=10,Val
ue=ram

Opt
ional
class-

Javai
ntr
oducedanewcl
assOptionali
njdk8.Iti
sapubl
icf
inal
classand
usedt
odealwithNul
l
Poi
nter
Excepti
oninJav aappl
i
cati
on.

Youmusti
mportj
ava.
uti
lpackagetousethi
scl
ass.I
tprovi
desmethods
whi
chareusedt
ocheckthepresenceofv
aluef
orpart
icul
arvar
iabl
e.
Why ?
packagecom.
test
;

publ
iccl
assMapDemo{

publ
icst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){

St
ri
ng[
]str=newSt ring[10];
St
ri
nglowercaseStr
ing=st r[5]
.t
oLower
Case(
);
Sy
stem.out
.pri
nt(
lower caseStr
ing)
;

}
}

Excepti
onint
hread"mai
n"j
ava.
lang.
Nul
lPoi
nterExcept
ion
atcom.
test
.MapDemo.
main(MapDemo.java:
8)

Her
ewearegetti
ngexcept
ion,
toav
oidt
hist
ypeofexcept
ion,
weshoul
d
gof
oropt
ional
class

packagecom.
test
;

i
mpor
tjav
a.ut
il
.Opt
ional
;

publ
iccl
assMapDemo{

publ
icst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){

St
ring[]str=newStri
ng[10]
;
Optional<St
ri
ng>checkNull
=Opt ional
.of
Nullabl
e(str
[5]
);
i
f(checkNul l
.i
sPr
esent(
)){//checkforvalueispresentor
not
St
ri
nglowercaseStr
ing=str[5].
toLowerCase()
;
Sy
stem.out
.pri
nt(
lowercaseSt r
ing)
;
}el
se
Sy
stem.out
.pri
ntl
n("
stri
ngv alueisnotpresent
");

}
}

Output
st
ri
ngv al
uei
snotpr
esent
.
JavaStr
ingJoiner-
Javaaddedanewf i
nal
classSt
ringJoinerinjava.
uti
lpackage.I
tisused
toconst
ructasequenceofcharacter
ssepar atedbyadel i
miter
.Now,
youcancreat
est r
ingbypassi
ngdel i
mi t
ersli
kecomma( ,
),hy
phen(-
)etc

Exampl
e
i
mportj
ava.
uti
l
.St
ri
ngJoi
ner
;

publ
iccl
assExampl
e{

publ
icst
aticvoi
dmain(St
ri
ng[]ar
gs){
Str
ingJoi
nerst
ri
ngJoiner=newStr
ingJoi
ner
(",
")
;//passi
ng
comma(,
)asdeli
miter

//Addingv aluestoStringJoiner
str
ingJoiner.
add("Ram" );
str
ingJoiner.
add("Shyam" );
str
ingJoiner.
add("ashok");
str
ingJoiner.
add("aj
ay");
System.out.pri
ntl
n(str
ingJoiner)
;
}
}

Out
put
Ram,
Shyam,
ashok,
aj
ay

You might also like