Professional Documents
Culture Documents
Null 1
Null 1
Null 1
ogr
am -
1–Howt
oret
urnmapt
omet
hod
packagecom.
hashmap;
i
mpor
tjav
a.ut
il
.HashMap;
i
mpor
tjav
a.ut
il
.Map;
publ
i
ccl
assHashMapDemo5{
publ
icHashMap<I
nteger
,St
ri
ng>addEmpl
oyee(
){
HashMap<Integer,Stri
ng>hashMap=newHashMap<I
nteger
,St
ri
ng>(
);
hashMap.put(
10,"rohan");
hashMap.put(
20,"sohan");
hashMap.put(
30,"velocit
y")
;
ret
urnhashMap;
}
publi
cstati
cv oi
dmai n(Stri
ng[
]ar
gs){
HashMapDemo5hashMapDemo5=newHashMapDemo5( )
;
Syst
em.out.
pri
ntl
n("
fi
rstway="+hashMapDemo5.addEmployee()
);/
/1stway
HashMap<Int
eger, St r
ing> hashMap=hashMapDemo5. addEmployee(
);
//
2ndway
Syst
em.out.
pri
ntl
n("
secondway ="+hashMap)
;
Map<Int
eger,
Stri
ng>map=hashMapDemo5. addEmploy
ee();//
3rdway
Syst
em.out.
pri
ntl
n("
thi
rdway ="
+map) ;
}
}
}
Pr
ogr
am-
2Canwet
akecust
om Empl
oyeecl
assaskeyi
nhashmap
publ
iccl
assEmpl
oyee{
i
ntid=10;
St
ri
ngname="
ram"
;
St
ri
ngsal
ary="
5000"
;
publi
cintget
Id(
){
ret
urnid;
}
publi
cvoi
dset
Id(
inti
d){
thi
s.i
d=id;
}
publi
cStr
ingget
Name(
){
ret
urnname;
}
publi
cv oi
dset
Name(
Str
ingname){
thi
s.name=name;
}
publi
cStr
ingget
Sal
ary
(){
ret
urnsal
ary;
}
publicv
oidsetSal
ary
(St
ri
ngsal
ary
){
thi
s.sal
ary=salar
y;
}
i
mpor
tjav
a.ut
il
.HashMap;
publ
iccl
assHashMapDemo6{
publ
icst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){
HashMap<Empl
oyee,
Str
ing>hashMap=newHashMap<Empl
oyee,
Str
ing>(
);
Empl
oyeeempl
oyee=newEmpl
oyee(
);
/
/empl
oyeeasobj
ecti
nmapaskeyandnameasv
alue
hashMap.
put
(empl
oyee,
empl
oyee.
get
Sal
ary
())
;
Sy
st out
em. .pr
int
ln(
hashMap.
get
(empl
oyee)
);
}
}
Pr
ogr
am-3
/
/Ar
ray
Listwi
thHashMapexampl
e
i
mpor
tjav
a.ut
il
.Arr
ayLi
st;
i
mpor
tjav
a.ut
il
.HashMap;
i
mpor
tjav
a.ut
il
.Li
st;
i
mpor
tjav
a.ut
il
.Set
;
publ
iccl
assHashMapDemo2{
publ
icst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){
Li
st<St
ri
ng>comput er
accessories=newAr
ray
List
<St
ri
ng>(
);
computer
accessor
ies.
add("watch")
;
computer
accessor
ies.
add("speaker"
);
computer
accessor
ies.
add("l
aptop")
;
Li
st<Stri
ng>electr
onics=newAr
ray
List
<St
ri
ng>(
);
el
ectroni
cs.add("
pri
nter"
);
el
ectroni
cs.add("
keyboard"
);
el
ectroni
cs.add("
mouse");
List<Str
ing>furni
ture=newAr
ray
List
<St
ri
ng>(
);
furnit
ure.add(
"bed")
;
furnit
ure.add(
"chair
");
furnit
ure.add(
"sofa"
);
/*
*HashMaphm=newHashMap(
);
*hm. put("ajay",
20);
*hm. put("sachin",
50);
*/
//cat egori
esaskeyandv al
ueasAr rayList
HashMap<St r
ing,List
<Stri
ng>>categories=newHashMap<Stri
ng,
List<Stri
ng>>();
categor i
es.put("
Comput er",
comput eraccessor
ies)
;//
passi
ngar
ray
li
stas
value
categor i
es.put("
Electr
onics",
electr
onics);
categor i
es.put("
Furnit
ure",f
urnit
ure);
//eshopaskey-st
ri
ng,val
ueasHashMap
HashMap<Stri
ng,HashMap<Str
ing,
List
<St
ri
ng>>>eshop=new
HashMap<Stri
ng,HashMap<Str
ing,
List
<St
ri
ng>>>(
);
eshop.put
("
EShop",cat
egori
es)
;
//howt oiterate
Set<St
ring>s=eshop. keySet()
;//
onlykeyscontai
nEshop
for(
Stri
ngst r: s){
System.out.println(
str)
;//key
//Syst
em. out.printl
n(eshop.get
(st
r))
;//v
alue-
eshop.
get
(Eshop)
--
pri
nt
}
}
}