|
| 1 | +package gr.codelearn; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map; |
| 5 | + |
| 6 | +public class MapDemo { |
| 7 | + public static void main(String[] args) { |
| 8 | +// createMap(); |
| 9 | + otherMapActions(); |
| 10 | + } |
| 11 | + |
| 12 | + private static void createMap() { |
| 13 | + Map<String, String> map = Map.of(); |
| 14 | + |
| 15 | + Map<String, String> map2 = Map.of("key1", "value1", "key2", "value2"); |
| 16 | + System.out.println(map2); |
| 17 | + |
| 18 | + Map<String, String> map3 = Map.ofEntries(Map.entry("key1", "value1"), Map.entry("key2", "value2")); |
| 19 | + System.out.println(map3); |
| 20 | + |
| 21 | + Map<String, String> map4 = new HashMap<>(); |
| 22 | + map4.put("Android", "Mobile"); |
| 23 | + map4.put("Eclipse IDE", "Java"); |
| 24 | + map4.put("Intellij IDEA", "Java"); |
| 25 | + map4.put("Git", "Version control system"); |
| 26 | + |
| 27 | + map4.put("Git", "A new value"); |
| 28 | + |
| 29 | + System.out.println(map4); |
| 30 | + } |
| 31 | + |
| 32 | + private static void otherMapActions() { |
| 33 | + Map<String, String> map = new HashMap<>(); |
| 34 | + map.put("Android", "Mobile"); |
| 35 | + map.put("Eclipse IDE", "Java"); |
| 36 | + map.put("Intellij IDEA", "Java"); |
| 37 | + map.put("Git", "Version control system"); |
| 38 | + |
| 39 | +// System.out.println(map.get("Eclipse IDE")); |
| 40 | + |
| 41 | + for (Map.Entry<String, String> mapEntry : map.entrySet()) { |
| 42 | + System.out.println(mapEntry); |
| 43 | + } |
| 44 | + |
| 45 | + System.out.println("\nPrinting the keys:"); |
| 46 | + for (String key : map.keySet()) { |
| 47 | + System.out.println(key); |
| 48 | + } |
| 49 | + |
| 50 | + System.out.println("\nPrinting the values:"); |
| 51 | + for (String value : map.values()) { |
| 52 | + System.out.println(value); |
| 53 | + } |
| 54 | + |
| 55 | + System.out.println("\nDoes the map contain a value for Android:"); |
| 56 | + System.out.println(map.containsKey("Android")); |
| 57 | + System.out.println(map.containsValue("Mobile")); |
| 58 | + |
| 59 | + System.out.println(map.getOrDefault("Sth", "Not found")); |
| 60 | + |
| 61 | + map.clear(); |
| 62 | + |
| 63 | + System.out.println(map); |
| 64 | + } |
| 65 | +} |
0 commit comments