Skip to content

Commit 4571ceb

Browse files
committed
Improve map description
1 parent 1464ea7 commit 4571ceb

File tree

2 files changed

+70
-22
lines changed

2 files changed

+70
-22
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.vogella.java.collections.map;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
public class ConvertMapTester {
9+
public static void main(String[] args) {
10+
11+
// keys are Strings
12+
// objects are also Strings
13+
Map<String, String> map = new HashMap<>();
14+
fillData(map);
15+
16+
// convert keys to array
17+
String[] strings = keysAsArray(map);
18+
for (String string : strings) {
19+
System.out.println(string);
20+
}
21+
// convert keys to list
22+
List<String> list = keysAsList(map);
23+
for (String string : list) {
24+
System.out.println(string);
25+
}
26+
}
27+
28+
private static void fillData(Map<String, String> map) {
29+
map.put("Android", "Mobile");
30+
map.put("Eclipse IDE", "Java");
31+
map.put("Eclipse RCP", "Java");
32+
map.put("Git", "Version control system");
33+
34+
}
35+
36+
private static String[] keysAsArray(Map<String, String> map) {
37+
return map.keySet().toArray(new String[map.keySet().size()]);
38+
}
39+
40+
// assumes the key is of type String
41+
private static List<String> keysAsList(Map<String, String> map) {
42+
List<String> list = new ArrayList<String>(map.keySet());
43+
return list;
44+
}
45+
46+
}
Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,38 @@
11
package com.vogella.java.collections.map;
22

33
import java.util.HashMap;
4-
import java.util.Iterator;
54
import java.util.Map;
65

76
public class MapTester {
87
public static void main(String[] args) {
9-
// Keys are Strings
10-
// Objects are also Strings
118

12-
Map<String, String> mMap = new HashMap<String, String>();
13-
mMap.put("Android", "Mobile");
14-
mMap.put("Eclipse", "IDE");
15-
mMap.put("Git", "Version control system");
16-
17-
// Output
18-
for (String key : mMap.keySet()) {
19-
System.out.println(key +" "+ mMap.get(key));
9+
// keys are Strings
10+
// objects are also Strings
11+
Map<String, String> map = new HashMap<>();
12+
fillData(map);
13+
14+
// write to command line
15+
for (String key : map.keySet()) {
16+
System.out.println(key + " " + map.get(key));
2017
}
2118

22-
System.out.println("Changing the data");
23-
// Adding to the map
24-
mMap.put("iPhone", "Created by Apple");
19+
// add and remove from the map
20+
map.put("iPhone", "Created by Apple");
21+
map.remove("Android");
2522

26-
// Delete from map
27-
28-
mMap.remove("Android");
29-
30-
System.out.println("New output:");
31-
// Output
32-
for (String key : mMap.keySet()) {
33-
System.out.println(key +" "+ mMap.get(key));
23+
// write again to command line
24+
for (String key : map.keySet()) {
25+
System.out.println(key + " " + map.get(key));
3426
}
27+
3528
}
29+
30+
private static void fillData(Map<String, String> map) {
31+
map.put("Android", "Mobile");
32+
map.put("Eclipse IDE", "Java");
33+
map.put("Eclipse RCP", "Java");
34+
map.put("Git", "Version control system");
35+
36+
}
37+
3638
}

0 commit comments

Comments
 (0)