|
1 | 1 | package com.vogella.java.collections.map; |
2 | 2 |
|
3 | 3 | import java.util.HashMap; |
4 | | -import java.util.Iterator; |
5 | 4 | import java.util.Map; |
6 | 5 |
|
7 | 6 | public class MapTester { |
8 | 7 | public static void main(String[] args) { |
9 | | - // Keys are Strings |
10 | | - // Objects are also Strings |
11 | 8 |
|
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)); |
20 | 17 | } |
21 | 18 |
|
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"); |
25 | 22 |
|
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)); |
34 | 26 | } |
| 27 | + |
35 | 28 | } |
| 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 | + |
36 | 38 | } |
0 commit comments