2
2
3
3
import com .google .common .collect .ImmutableMap ;
4
4
import com .google .common .collect .Maps ;
5
-
6
5
import java .util .Collections ;
7
6
import java .util .HashMap ;
8
7
import java .util .Map ;
@@ -21,13 +20,14 @@ public void initializeJavaHashMap() {
21
20
}
22
21
23
22
public void initializeJavaHashMapUsingAnonymousSubClass () {
24
- Map <String , String > map = new HashMap <>() {
25
- {
26
- put ("color" , "black" );
27
- put ("drink" , "coffee" );
28
- put ("shape" , "slim" );
29
- }
30
- };
23
+ Map <String , String > map =
24
+ new HashMap <>() {
25
+ {
26
+ put ("color" , "black" );
27
+ put ("drink" , "coffee" );
28
+ put ("shape" , "slim" );
29
+ }
30
+ };
31
31
32
32
System .out .println (map );
33
33
}
@@ -38,15 +38,13 @@ public void initializeImmutableJavaHashMap() {
38
38
map .put ("drink" , "coffee" );
39
39
map .put ("shape" , "slim" );
40
40
41
- Map <String , String > immutableMap =
42
- Collections .unmodifiableMap (map );
41
+ Map <String , String > immutableMap = Collections .unmodifiableMap (map );
43
42
44
43
System .out .println (immutableMap );
45
44
}
46
45
47
46
public void initializeSingletonJavaHashMap () {
48
- Map <String , String > map =
49
- Collections .singletonMap ("color" , "black" );
47
+ Map <String , String > map = Collections .singletonMap ("color" , "black" );
50
48
System .out .println (map );
51
49
}
52
50
@@ -56,39 +54,37 @@ public void initializeEmptyJavaHashMap() {
56
54
}
57
55
58
56
public void initializeImmutableJavaHashMapUsingGuava () {
59
- Map <String , String > immutableMap = ImmutableMap
60
- .of ("color" , "pink" , "drink" , "coffee" , "shape" , "slim" );
57
+ Map <String , String > immutableMap =
58
+ ImmutableMap .of ("color" , "pink" , "drink" , "coffee" , "shape" , "slim" );
61
59
62
60
System .out .println (immutableMap );
63
61
}
64
62
65
63
public void initializeMutableJavaHashMapUsingGuava () {
66
- Map <String , String > immutableMap = ImmutableMap
67
- .of ("color" , "pink" , "drink" , "coffee" , "shape" , "slim" );
64
+ Map <String , String > immutableMap =
65
+ ImmutableMap .of ("color" , "pink" , "drink" , "coffee" , "shape" , "slim" );
68
66
69
67
Map <String , String > mutableMap = Maps .newHashMap (immutableMap );
70
68
System .out .println (mutableMap );
71
69
}
72
70
73
71
public void initializeJavaHashMapUsingCollectorsToMap () {
74
72
Set <String > set = Set .of ("Pink" , "Red" , "Black" );
75
- Map <String , String > map = set . stream ()
76
- .collect (Collectors .toMap (String ::toUpperCase , String ::toLowerCase ));
73
+ Map <String , String > map =
74
+ set . stream () .collect (Collectors .toMap (String ::toUpperCase , String ::toLowerCase ));
77
75
78
76
System .out .println (map );
79
77
}
80
78
81
79
public void initializeJavaHashMapUsingMapOf () {
82
- Map <String , String > immutableMap =
83
- Map .of ("color" , "black" , "drink" , "coffee" );
80
+ Map <String , String > immutableMap = Map .of ("color" , "black" , "drink" , "coffee" );
84
81
85
82
System .out .println (immutableMap );
86
83
}
87
84
88
85
public void initializeJavaHashMapUsingMapOfEntries () {
89
- Map <String , String > immutableMap = Map .ofEntries (
90
- Map .entry ("color" , "pink" ),
91
- Map .entry ("drink" , "coffee" ));
86
+ Map <String , String > immutableMap =
87
+ Map .ofEntries (Map .entry ("color" , "pink" ), Map .entry ("drink" , "coffee" ));
92
88
93
89
System .out .println (immutableMap );
94
90
}
@@ -126,4 +122,4 @@ public static void main(String[] a) {
126
122
System .out .println ("Initialize HashMap using factory method ofEntries()" );
127
123
initializer .initializeJavaHashMapUsingMapOfEntries ();
128
124
}
129
- }
125
+ }
0 commit comments