File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import java .util .HashMap ;
2+ import java .util .Map ;
3+
4+ public class Hashmap
5+ {
6+ public static void main (String [] args )
7+ {
8+ HashMap <String , Integer > map = new HashMap <>();
9+ print (map );
10+ map .put ("abc" , 10 );
11+ map .put ("mno" , 30 );
12+ map .put ("xyz" , 20 );
13+
14+ System .out .println ("Size of map is" + map .size ());
15+
16+ print (map );
17+ if (map .containsKey ("abc" ))
18+ {
19+ Integer a = map .get ("abc" );
20+ System .out .println ("value for key \" abc\" is:- " + a );
21+ }
22+ map .clear ();
23+ print (map );
24+ }
25+ public static void print (Map <String , Integer > map )
26+ {
27+ if (map .isEmpty ())
28+ {
29+ System .out .println ("map is empty" );
30+ }
31+ else
32+ {
33+ System .out .println (map );
34+ }
35+ }
36+ }
37+ On executing the HashMap program , output goes like this :
38+
39+ 1
40+ 2
41+ 3
42+ 4
43+ 5
44+ map is empty
45+ Size of map is :- 3
46+ {abc =10 , xyz =20 , mno =30 }
47+ value for key "abc" is :- 10
48+ map is empty
You can’t perform that action at this time.
0 commit comments