Java : EnumMap (列挙型のマップ) - API使用例
EnumMap (Java SE 20 & JDK 20) の使用例まとめです。
だいたいのメソッドを網羅済みです。
API仕様書のおともにどうぞ。
概要
EnumMap クラスは、列挙型(Enum) をキーとする Map の実装です。
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}
コンストラクタ
EnumMap (Class<K> keyType)
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)
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)
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
メソッド
void clear ()
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 ()
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)
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)
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 ()
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)
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)
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 ()
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 ()
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)
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)
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)
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 ()
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 ()
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]
AbstractMapで宣言されたメソッド
isEmpty, toString
「Java API 使用例 : AbstractMap」をご参照ください。
Mapで宣言されたメソッド
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
「Java API 使用例 : Map」をご参照ください。
関連記事
- List(リスト)の基本
- Map(マップ)の基本
- Set(セット)の基本
- List の初期化方法いろいろ
- Map の初期化方法いろいろ
- Set の初期化方法いろいろ
- 配列 vs. List
- 配列からListへの変換
- List から配列への変換
- API 使用例
- Collection (コレクション)
- Collections (コレクション操作)
- Comparable
- Comparator
- Iterator
- List (リスト)
- Map (マップ)
- Map.Entry (キーと値のペア)
- Queue (キュー)
- AbstractQueue
- ArrayBlockingQueue (ブロッキング・配列キュー)
- ArrayDeque (両端キュー)
- BlockingDeque (ブロッキング・両端キュー)
- BlockingQueue (ブロッキング・キュー)
- ConcurrentLinkedDeque (並列処理用・両端キュー)
- ConcurrentLinkedQueue (並列処理用キュー)
- Deque (両端キュー)
- LinkedBlockingDeque (ブロッキング・リンク両端キュー)
- LinkedBlockingQueue (ブロッキング・リンクキュー)
- LinkedList (二重リンク・リスト)
- PriorityBlockingQueue (ブロッキング・優先度付きキュー)
- PriorityQueue (優先度付きキュー)
- RandomAccess
- Set (セット)
- Spliterator