Java : EnumMap with Examples

EnumMap (Java SE 20 & JDK 20) with Examples.
You will find code examples on most EnumMap methods.


Summary

A specialized Map implementation for use with enum type keys. All of the keys in an enum map must come from a single enum type that is specified, explicitly or implicitly, when the map is created. Enum maps are represented internally as arrays. This representation is extremely compact and efficient.

Class diagram

final var map = new EnumMap<DayOfWeek, String>(DayOfWeek.class);
System.out.println(map); // {}

map.put(DayOfWeek.MONDAY, "aaa");
map.put(DayOfWeek.TUESDAY, "bbb");
map.put(DayOfWeek.WEDNESDAY, "ccc");

System.out.println(map); // {MONDAY=aaa, TUESDAY=bbb, WEDNESDAY=ccc}

System.out.println(map.get(DayOfWeek.MONDAY)); // aaa
System.out.println(map.get(DayOfWeek.TUESDAY)); // bbb
System.out.println(map.get(DayOfWeek.WEDNESDAY)); // ccc
System.out.println(map.get(DayOfWeek.THURSDAY)); // null

map.remove(DayOfWeek.MONDAY);
System.out.println(map); // {TUESDAY=bbb, WEDNESDAY=ccc}

Constructors

EnumMap (Class<K> keyType)

Creates an empty enum map with the specified key type.

final var map = new EnumMap<DayOfWeek, Integer>(DayOfWeek.class);
System.out.println(map); // {}
System.out.println(map.isEmpty()); // true

map.put(DayOfWeek.MONDAY, 10);
map.put(DayOfWeek.TUESDAY, 20);
map.put(DayOfWeek.WEDNESDAY, 30);

System.out.println(map); // {MONDAY=10, TUESDAY=20, WEDNESDAY=30}
System.out.println(map.size()); // 3

EnumMap (EnumMap<K,? extends V> m)

Creates an enum map with the same key type as the specified enum map, initially containing the same mappings (if any).

final var m = new EnumMap<DayOfWeek, Integer>(DayOfWeek.class);
m.put(DayOfWeek.MONDAY, 10);
m.put(DayOfWeek.TUESDAY, 20);
m.put(DayOfWeek.WEDNESDAY, 30);

System.out.println(m); // {MONDAY=10, TUESDAY=20, WEDNESDAY=30}
System.out.println(m.size()); // 3

final var map = new EnumMap<>(m);
System.out.println(map); // {MONDAY=10, TUESDAY=20, WEDNESDAY=30}
System.out.println(map.size()); // 3

EnumMap (Map<K,? extends V> m)

Creates an enum map initialized from the specified map.

final var m = new HashMap<DayOfWeek, Integer>();
m.put(DayOfWeek.MONDAY, 10);
m.put(DayOfWeek.TUESDAY, 20);
m.put(DayOfWeek.WEDNESDAY, 30);

System.out.println(m); // {WEDNESDAY=30, TUESDAY=20, MONDAY=10}
System.out.println(m.size()); // 3

final var map = new EnumMap<>(m);
System.out.println(map); // {MONDAY=10, TUESDAY=20, WEDNESDAY=30}
System.out.println(map.size()); // 3

Methods

void clear ()

Removes all mappings from this map.

final var map = new EnumMap<DayOfWeek, Integer>(DayOfWeek.class);

map.put(DayOfWeek.MONDAY, 10);
map.put(DayOfWeek.TUESDAY, 20);
map.put(DayOfWeek.WEDNESDAY, 30);

System.out.println(map); // {MONDAY=10, TUESDAY=20, WEDNESDAY=30}
System.out.println(map.size()); // 3

map.clear();
System.out.println(map); // {}
System.out.println(map.size()); // 0

EnumMap<K,V> clone ()

Returns a shallow copy of this enum map.

final var map = new EnumMap<DayOfWeek, Integer>(DayOfWeek.class);

map.put(DayOfWeek.MONDAY, 10);
map.put(DayOfWeek.TUESDAY, 20);
map.put(DayOfWeek.WEDNESDAY, 30);

System.out.println(map); // {MONDAY=10, TUESDAY=20, WEDNESDAY=30}

final var cloned = map.clone();
System.out.println(cloned); // {MONDAY=10, TUESDAY=20, WEDNESDAY=30}
System.out.println(cloned.getClass()); // class java.util.EnumMap

boolean containsKey (Object key)

Returns true if this map contains a mapping for the specified key.

final var map = new EnumMap<DayOfWeek, Integer>(DayOfWeek.class);

map.put(DayOfWeek.MONDAY, 10);
map.put(DayOfWeek.TUESDAY, 20);
map.put(DayOfWeek.WEDNESDAY, 30);

System.out.println(map); // {MONDAY=10, TUESDAY=20, WEDNESDAY=30}

System.out.println(map.containsKey(DayOfWeek.MONDAY)); // true
System.out.println(map.containsKey(DayOfWeek.TUESDAY)); // true
System.out.println(map.containsKey(DayOfWeek.WEDNESDAY)); // true
System.out.println(map.containsKey(DayOfWeek.SUNDAY)); // false

boolean containsValue (Object value)

Returns true if this map maps one or more keys to the specified value.

final var map = new EnumMap<DayOfWeek, Integer>(DayOfWeek.class);

map.put(DayOfWeek.MONDAY, 10);
map.put(DayOfWeek.TUESDAY, 20);
map.put(DayOfWeek.WEDNESDAY, 30);
map.put(DayOfWeek.THURSDAY, 30);

System.out.println(map); // {MONDAY=10, TUESDAY=20, WEDNESDAY=30, THURSDAY=30}

System.out.println(map.containsValue(0)); // false
System.out.println(map.containsValue(10)); // true
System.out.println(map.containsValue(20)); // true
System.out.println(map.containsValue(30)); // true
System.out.println(map.containsValue(40)); // false

Set<Map.Entry<K,V>> entrySet ()

Returns a Set view of the mappings contained in this map.

final var map = new EnumMap<DayOfWeek, Integer>(DayOfWeek.class);

map.put(DayOfWeek.MONDAY, 1);
map.put(DayOfWeek.TUESDAY, 2);
map.put(DayOfWeek.WEDNESDAY, 3);

System.out.println(map); // {MONDAY=1, TUESDAY=2, WEDNESDAY=3}

final var entries = map.entrySet();
System.out.println(entries); // [MONDAY=1, TUESDAY=2, WEDNESDAY=3]

map.replace(DayOfWeek.TUESDAY, 20);
System.out.println(map); // {MONDAY=1, TUESDAY=20, WEDNESDAY=3}
System.out.println(entries); // [MONDAY=1, TUESDAY=20, WEDNESDAY=3]

entries.remove(Map.entry(DayOfWeek.MONDAY, 1));
System.out.println(map); // {TUESDAY=20, WEDNESDAY=3}
System.out.println(entries); // [TUESDAY=20, WEDNESDAY=3]

for (final var entry : entries) {
    entry.setValue(entry.getValue() * 10);
}

System.out.println(map); // {TUESDAY=200, WEDNESDAY=30}
System.out.println(entries); // [TUESDAY=200, WEDNESDAY=30]

boolean equals (Object o)

Compares the specified object with this map for equality.

final var map1 = new EnumMap<DayOfWeek, Integer>(DayOfWeek.class);

map1.put(DayOfWeek.MONDAY, 1);
map1.put(DayOfWeek.TUESDAY, 2);

final var map2 = new EnumMap<DayOfWeek, Integer>(DayOfWeek.class);

map2.put(DayOfWeek.MONDAY, 1);
map2.put(DayOfWeek.TUESDAY, 2);

final var map3 = new EnumMap<DayOfWeek, Integer>(DayOfWeek.class);

map3.put(DayOfWeek.MONDAY, 10);
map3.put(DayOfWeek.TUESDAY, 20);

final var map4 = new EnumMap<DayOfWeek, Integer>(DayOfWeek.class);

map4.put(DayOfWeek.FRIDAY, 1);
map4.put(DayOfWeek.SATURDAY, 2);

System.out.println(map1); // {MONDAY=1, TUESDAY=2}
System.out.println(map2); // {MONDAY=1, TUESDAY=2}
System.out.println(map3); // {MONDAY=10, TUESDAY=20}
System.out.println(map4); // {FRIDAY=1, SATURDAY=2}

System.out.println(map1.equals(map2)); // true
System.out.println(map1.equals(map3)); // false
System.out.println(map1.equals(map4)); // false

V get (Object key)

Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

final var map = new EnumMap<DayOfWeek, Integer>(DayOfWeek.class);

map.put(DayOfWeek.MONDAY, 10);
map.put(DayOfWeek.TUESDAY, 20);
map.put(DayOfWeek.WEDNESDAY, 30);

System.out.println(map); // {MONDAY=10, TUESDAY=20, WEDNESDAY=30}

System.out.println(map.get(DayOfWeek.MONDAY)); // 10
System.out.println(map.get(DayOfWeek.TUESDAY)); // 20
System.out.println(map.get(DayOfWeek.WEDNESDAY)); // 30
System.out.println(map.get(DayOfWeek.THURSDAY)); // null

int hashCode ()

Returns the hash code value for this map.

final var map = new EnumMap<DayOfWeek, Integer>(DayOfWeek.class);
map.put(DayOfWeek.MONDAY, 1);
map.put(DayOfWeek.TUESDAY, 2);

System.out.println(map); // {MONDAY=1, TUESDAY=2}
System.out.println(map.hashCode()); // 2075746216
final var map = new EnumMap<DayOfWeek, Integer>(DayOfWeek.class);

map.put(DayOfWeek.MONDAY, 10);
map.put(DayOfWeek.TUESDAY, 20);

System.out.println(map); // {MONDAY=10, TUESDAY=20}
System.out.println(map.hashCode()); // 2075746219
final var map = new EnumMap<DayOfWeek, Integer>(DayOfWeek.class);

map.put(DayOfWeek.FRIDAY, 1);
map.put(DayOfWeek.SATURDAY, 2);

System.out.println(map); // {FRIDAY=1, SATURDAY=2}
System.out.println(map.hashCode()); // -1797384182

Set<K> keySet ()

Returns a Set view of the keys contained in this map.

final var map = new EnumMap<DayOfWeek, Integer>(DayOfWeek.class);

map.put(DayOfWeek.MONDAY, 10);
map.put(DayOfWeek.TUESDAY, 20);
map.put(DayOfWeek.WEDNESDAY, 30);

System.out.println(map); // {MONDAY=10, TUESDAY=20, WEDNESDAY=30}

final var keys = map.keySet();
System.out.println(keys); // [MONDAY, TUESDAY, WEDNESDAY]

map.put(DayOfWeek.THURSDAY, 40);
System.out.println(map); // {MONDAY=10, TUESDAY=20, WEDNESDAY=30, THURSDAY=40}
System.out.println(keys); // [MONDAY, TUESDAY, WEDNESDAY, THURSDAY]

keys.remove(DayOfWeek.MONDAY);
System.out.println(map); // {TUESDAY=20, WEDNESDAY=30, THURSDAY=40}
System.out.println(keys); // [TUESDAY, WEDNESDAY, THURSDAY]

V put (K key, V value)

Associates the specified value with the specified key in this map.

final var map = new EnumMap<DayOfWeek, Integer>(DayOfWeek.class);
System.out.println(map); // {}

System.out.println(map.put(DayOfWeek.MONDAY, 10)); // null
System.out.println(map); // {MONDAY=10}

System.out.println(map.put(DayOfWeek.TUESDAY, 20)); // null
System.out.println(map); // {MONDAY=10, TUESDAY=20}

System.out.println(map.put(DayOfWeek.WEDNESDAY, 30)); // null
System.out.println(map); // {MONDAY=10, TUESDAY=20, WEDNESDAY=30}

System.out.println(map.put(DayOfWeek.MONDAY, 999)); // 10
System.out.println(map); // {MONDAY=999, TUESDAY=20, WEDNESDAY=30}

void putAll (Map<? extends K,? extends V> m)

Copies all of the mappings from the specified map to this map.

final var map = new EnumMap<DayOfWeek, Integer>(DayOfWeek.class);
System.out.println(map); // {}

map.putAll(Map.of());
System.out.println(map); // {}

final var m1 = Map.of(DayOfWeek.MONDAY, 10);
map.putAll(m1);
System.out.println(map); // {MONDAY=10}

final var m2 = Map.of(DayOfWeek.TUESDAY, 20, DayOfWeek.WEDNESDAY, 30);
map.putAll(m2);
System.out.println(map); // {MONDAY=10, TUESDAY=20, WEDNESDAY=30}

final var m3 = Map.of(DayOfWeek.MONDAY, 910, DayOfWeek.TUESDAY, 920);
map.putAll(m3);
System.out.println(map);  // {MONDAY=910, TUESDAY=920, WEDNESDAY=30}

V remove (Object key)

Removes the mapping for this key from this map if present.

final var map = new EnumMap<DayOfWeek, Integer>(DayOfWeek.class);

map.put(DayOfWeek.MONDAY, 10);
map.put(DayOfWeek.TUESDAY, 20);
map.put(DayOfWeek.WEDNESDAY, 30);

System.out.println(map); // {MONDAY=10, TUESDAY=20, WEDNESDAY=30}

System.out.println(map.remove(DayOfWeek.TUESDAY)); // 20
System.out.println(map); // {MONDAY=10, WEDNESDAY=30}

System.out.println(map.remove(DayOfWeek.SUNDAY)); // null
System.out.println(map); // {MONDAY=10, WEDNESDAY=30}

int size ()

Returns the number of key-value mappings in this map.

final var map = new EnumMap<DayOfWeek, Integer>(DayOfWeek.class);

System.out.println(map); // {}
System.out.println(map.size()); // 0

map.put(DayOfWeek.MONDAY, 10);
System.out.println(map); // {MONDAY=10}
System.out.println(map.size()); // 1

map.put(DayOfWeek.TUESDAY, 20);
System.out.println(map); // {MONDAY=10, TUESDAY=20}
System.out.println(map.size()); // 2

map.put(DayOfWeek.WEDNESDAY, 30);
System.out.println(map); // {MONDAY=10, TUESDAY=20, WEDNESDAY=30}
System.out.println(map.size()); // 3

Collection<V> values ()

Returns a Collection view of the values contained in this map.

final var map = new EnumMap<DayOfWeek, Integer>(DayOfWeek.class);

map.put(DayOfWeek.MONDAY, 1);
map.put(DayOfWeek.TUESDAY, 2);
map.put(DayOfWeek.WEDNESDAY, 3);

System.out.println(map); // {MONDAY=1, TUESDAY=2, WEDNESDAY=3}

final var values = map.values();
System.out.println(values); // [1, 2, 3]

map.replace(DayOfWeek.TUESDAY, 20);
System.out.println(map); // {MONDAY=1, TUESDAY=20, WEDNESDAY=3}
System.out.println(values); // [1, 20, 3]

values.remove(1);
System.out.println(map); // {TUESDAY=20, WEDNESDAY=3}
System.out.println(values); // [20, 3]

Methods declared in AbstractMap

isEmpty, toString

Please see the link below.

Methods declared in Map

compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll

Please see the link below.


Related posts

To top of page