# EnumMap指南

## 1.概述

EnumMap是一個*Map實現，專門將Enum*作為其鍵。

## 2.搭建項目

``````Monday Soccer

Wednesday Hiking

Thursday Karate
``````

``````public enum DayOfWeek {

MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY

}``````

## 3.實例化

``````EnumMap<DayOfWeek, String> activityMap = new EnumMap<>(DayOfWeek.class);

activityMap.put(DayOfWeek.MONDAY, "Soccer");
``````

### 3.1。 EnumMap複製構造函數

EnumMap還附帶了兩個副本構造函數。第一個使用另一個EnumMap

``````EnumMap<DayOfWeek, String> activityMap = new EnumMap<>(DayOfWeek.class);

activityMap.put(DayOfWeek.MONDAY, "Soccer");

EnumMap<DayOfWeek, String> activityMapCopy = new EnumMap<>(dayMap);

assertThat(activityMapCopy.size()).isEqualTo(2);

assertThat(activityMapCopy.get(DayOfWeek.MONDAY)).isEqualTo("Soccer");

### 3.2。Map複製構造函數

``````Map<DayOfWeek, String> ordinaryMap = new HashMap();

ordinaryMap.put(DayOfWeek.MONDAY, "Soccer");

EnumMap enumMap = new EnumMap(ordinaryMap);

assertThat(enumMap.size()).isEqualTo(1);

assertThat(enumMap.get(DayOfWeek.MONDAY)).isEqualTo("Soccer");``````

## 4.添加和檢索元素

``activityMap.put(DayOfWeek.MONDAY, "Soccer");``

``assertThat(clubMap.get(DayOfWeek.MONDAY)).isEqualTo("Soccer");``

## 5.檢查元素

``````activityMap.put(DayOfWeek.WEDNESDAY, "Hiking");

assertThat(activityMap.containsKey(DayOfWeek.WEDNESDAY)).isTrue();``````

``````assertThat(activityMap.containsValue("Hiking")).isTrue();
``````

### 5.1。null空值

``````assertThat(activityMap.containsKey(DayOfWeek.SATURDAY)).isFalse();

assertThat(activityMap.containsValue(null)).isFalse();

activityMap.put(DayOfWeek.SATURDAY, null);

assertThat(activityMap.containsKey(DayOfWeek.SATURDAY)).isTrue();

assertThat(activityMap.containsValue(null)).isTrue();``````

## 6.移除元素

``````activityMap.put(DayOfWeek.MONDAY, "Soccer");

assertThat(activityMap.remove(DayOfWeek.MONDAY)).isEqualTo("Soccer");

assertThat(activityMap.containsKey(DayOfWeek.MONDAY)).isFalse();
``````

``````activityMap.put(DayOfWeek.Monday, "Soccer");

assertThat(activityMap.remove(DayOfWeek.Monday, "Hiking")).isEqualTo(false);

assertThat(activityMap.remove(DayOfWeek.Monday, "Soccer")).isEqualTo(true);
``````

## 7.集合視圖

``````EnumMap<DayOfWeek, String> activityMap = new EnumMap(DayOfWeek.class);

activityMap.put(DayOfWeek.THURSDAY, "Karate");

activityMap.put(DayOfWeek.WEDNESDAY, "Hiking");

activityMap.put(DayOfWeek.MONDAY, "Soccer");``````

### 7.1。values

``````Collection values = dayMap.values();

assertThat(values)

.containsExactly("Soccer", "Hiking", "Karate");
``````

### 7.2。keySet

``````Set keys = dayMap.keySet();

assertThat(keys)

.containsExactly(DayOfWeek.MONDAY, DayOfWeek.WEDNESDAY, DayOfWeek.SATURDAY);
``````

### 7.3。 entrySet

``````assertThat(dayMap.entrySet())

.containsExactly(

new SimpleEntry(DayOfWeek.MONDAY, "Soccer"),

new SimpleEntry(DayOfWeek.WEDNESDAY, "Hiking"),

new SimpleEntry(DayOfWeek.THURSDAY, "Karate")

);
``````

### 7.4。可變性

``````activityMap.put(DayOfWeek.TUESDAY, "Basketball");

assertThat(values)

``````

``````values.remove("Hiking");

assertThat(activityMap.containsKey(DayOfWeek.WEDNESDAY)).isFalse();

assertThat(activityMap.size()).isEqualTo(3);
``````

## 9.結論

0 條評論，你可以發表評論，我們會進行改進