|
| 1 | +import java.util.Collection; |
| 2 | +import java.util.EnumMap; |
| 3 | +import java.util.Map; |
| 4 | +import java.util.Set; |
| 5 | + |
| 6 | +public class EnumMapMethods { |
| 7 | + enum Day { |
| 8 | + SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY |
| 9 | + } |
| 10 | + |
| 11 | + public static void main(String[] args) { |
| 12 | + EnumMap<Day, String> map = new EnumMap<>(Day.class); |
| 13 | + |
| 14 | + // Put |
| 15 | + map.put(Day.SUNDAY, "Sunday"); |
| 16 | + map.put(Day.MONDAY, "Monday"); |
| 17 | + map.put(Day.TUESDAY, "Tuesday"); |
| 18 | + map.put(Day.WEDNESDAY, "Wednesday"); |
| 19 | + map.put(Day.THURSDAY, "Thursday"); |
| 20 | + map.put(Day.FRIDAY, "Friday"); |
| 21 | + map.put(Day.SATURDAY, "Saturday"); |
| 22 | + |
| 23 | + System.out.println("Map:" + map); |
| 24 | + |
| 25 | + // Clone |
| 26 | + EnumMap<Day, String> map1 = new EnumMap<>(Day.class); |
| 27 | + map1 = (EnumMap<Day, String>) map.clone(); |
| 28 | + System.out.println("Cloned Map:" + map1); |
| 29 | + |
| 30 | + // containsKey |
| 31 | + Boolean k = map.containsKey(Day.SUNDAY); |
| 32 | + System.out.println("Is SUNDAY present in the map? " + k); |
| 33 | + |
| 34 | + // containsValue |
| 35 | + Boolean v = map.containsValue("Sunday"); |
| 36 | + System.out.println("Is Sunday present in the map? " + v); |
| 37 | + |
| 38 | + // get |
| 39 | + |
| 40 | + String g = map.get(Day.SUNDAY); |
| 41 | + System.out.println("Value of SUNDAY is: " + g); |
| 42 | + |
| 43 | + // getOrDefault |
| 44 | + |
| 45 | + String gd = map.getOrDefault(Day.SUNDAY, "Sunday"); |
| 46 | + |
| 47 | + System.out.println("Value of SUNDAY is: " + gd); |
| 48 | + |
| 49 | + // isEmpty |
| 50 | + |
| 51 | + Boolean i = map.isEmpty(); |
| 52 | + System.out.println("Is the map empty? " + i); |
| 53 | + |
| 54 | + // keySet |
| 55 | + Set<Day> set = map.keySet(); |
| 56 | + System.out.println("Keys of the map are: " + set); |
| 57 | + |
| 58 | + // size |
| 59 | + int s = map.size(); |
| 60 | + System.out.println("Size of the map is: " + s); |
| 61 | + |
| 62 | + // values |
| 63 | + Collection<String> col = map.values(); |
| 64 | + System.out.println("Values of the map are: " + col); |
| 65 | + |
| 66 | + // entrySet |
| 67 | + Set<Map.Entry<Day, String>> set1 = map.entrySet(); |
| 68 | + System.out.println("Entries of the map are: " + set1); |
| 69 | + |
| 70 | + // equals |
| 71 | + Boolean e = map.equals(map1); |
| 72 | + System.out.println("Is map equal to map1? " + e); |
| 73 | + |
| 74 | + // hashCode |
| 75 | + int h = map.hashCode(); |
| 76 | + System.out.println("Hashcode of the map is: " + h); |
| 77 | + |
| 78 | + // toString |
| 79 | + String t = map.toString(); |
| 80 | + System.out.println("String representation of the map is: " + t); |
| 81 | + |
| 82 | + // clear |
| 83 | + map.clear(); |
| 84 | + System.out.println("Map after clear:" + map); |
| 85 | + |
| 86 | + // putAll |
| 87 | + map.putAll(map1); |
| 88 | + System.out.println("Map after putAll:" + map); |
| 89 | + |
| 90 | + // remove |
| 91 | + map.remove(Day.SUNDAY); |
| 92 | + System.out.println("Map after remove:" + map); |
| 93 | + |
| 94 | + // replace |
| 95 | + map.replace(Day.SUNDAY, "Sunday"); |
| 96 | + System.out.println("Map after replace:" + map); |
| 97 | + |
| 98 | + // replaceAll |
| 99 | + map.replaceAll((k1, v1) -> "Sunday"); |
| 100 | + System.out.println("Map after replaceAll:" + map); |
| 101 | + |
| 102 | + // merge |
| 103 | + map.merge(Day.SATURDAY, "Sunday", (v2, v3) -> "Saturday"); |
| 104 | + System.out.println("Map after merge:" + map); |
| 105 | + map.merge(Day.FRIDAY, "Sunday", (v2, v3) -> "Friday"); |
| 106 | + System.out.println("Map after merge:" + map); |
| 107 | + |
| 108 | + // forEach |
| 109 | + |
| 110 | + map.forEach((k1, v1) -> System.out.println("Key: " + k1 + " Value: " + v1)); |
| 111 | + |
| 112 | + // replace(K key, V oldValue, V newValue) |
| 113 | + map.replace(Day.SATURDAY, "Saturday", "Sunday"); |
| 114 | + System.out.println("Map after replaceIfSame:" + map); |
| 115 | + |
| 116 | + |
| 117 | + // putIfAbsent(K key, V value) |
| 118 | + |
| 119 | + map.putIfAbsent(Day.SUNDAY, "Sunday"); |
| 120 | + System.out.println("Map after putIfAbsent:" + map); |
| 121 | + |
| 122 | + // remove(K key, V value) |
| 123 | + map.remove(Day.SUNDAY, "Sunday"); |
| 124 | + System.out.println("Map after remove:" + map); |
| 125 | + |
| 126 | + |
| 127 | + // computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction) |
| 128 | + |
| 129 | + map.computeIfAbsent(Day.SUNDAY, k1 -> "Sunday"); |
| 130 | + System.out.println("Map after computeIfAbsent:" + map); |
| 131 | + |
| 132 | + // computeIfPresent(K key, BiFunction<? super K,? super V,? extends V> remappingFunction) |
| 133 | + |
| 134 | + map.computeIfPresent(Day.SUNDAY, (k1, v1) -> "Sunday"); |
| 135 | + System.out.println("Map after computeIfPresent:" + map); |
| 136 | + |
| 137 | + // compute(K key, BiFunction<? super K,? super V,? extends V> remappingFunction) |
| 138 | + map.compute(Day.SUNDAY, (k1, v1) -> "Sunday"); |
| 139 | + System.out.println("Map after compute:" + map); |
| 140 | + |
| 141 | + |
| 142 | + |
| 143 | + } |
| 144 | +} |
0 commit comments