📅  最后修改于: 2023-12-03 15:01:29.702000             🧑  作者: Mango
Java EnumMap是Java集合框架中的一个映射类(Map),它的键(Key)必须是枚举类型(Enum)。EnumMap根据枚举值的顺序建立内部数组,因此EnumMap的性能很好,大多数操作的时间复杂度为O(1)。
EnumMap是Java集合框架的一部分,因此不需要特别导入。以下是一个EnumMap的示例:
import java.util.EnumMap;
import java.util.Map;
public class Example {
enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
public static void main(String[] args) {
EnumMap<Day, String> map = new EnumMap<>(Day.class);
map.put(Day.MONDAY, "星期一");
map.put(Day.TUESDAY, "星期二");
map.put(Day.WEDNESDAY, "星期三");
System.out.println(map);
}
}
输出:
{MONDAY=星期一, TUESDAY=星期二, WEDNESDAY=星期三}
在这个示例中,我们定义了一个Day枚举类型和一个EnumMap对象。我们使用put()方法将键和值添加到EnumMap。最后,我们将整个EnumMap打印出来。
下面是三个EnumMap的构造函数:
public EnumMap(Class
这个构造函数创建了一个空的EnumMap,用指定的枚举类型初始化内部数组。参数keyType是枚举类的Class对象。下面是一个示例:
import java.util.*;
public class Example {
enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
public static void main(String[] args) {
EnumMap<Day, String> map = new EnumMap<>(Day.class);
System.out.println("map.isEmpty():" + map.isEmpty()); // 输出:map.isEmpty():true
}
}
public EnumMap(EnumMap<K,? extends V> m)
这个构造函数创建了一个新的EnumMap,它的键和值都与给定的EnumMap相同。两个EnumMap必须具有相同的枚举类型。以下是一个示例:
import java.util.*;
public class Example {
enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
public static void main(String[] args) {
EnumMap<Day, String> map1 = new EnumMap<>(Day.class);
map1.put(Day.MONDAY, "星期一");
map1.put(Day.TUESDAY, "星期二");
map1.put(Day.WEDNESDAY, "星期三");
EnumMap<Day, String> map2 = new EnumMap<>(map1);
System.out.println("map2:" + map2);
}
}
输出:
map2:{MONDAY=星期一, TUESDAY=星期二, WEDNESDAY=星期三}
public EnumMap(Map<K,? extends V> m)
这个构造函数创建了一个新的EnumMap,它的键和值都与给定的map相同。所有的键都必须是枚举类型。以下是一个示例:
import java.util.*;
public class Example {
enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
public static void main(String[] args) {
Map<Day, String> map1 = new HashMap<>();
map1.put(Day.MONDAY, "星期一");
map1.put(Day.TUESDAY, "星期二");
map1.put(Day.WEDNESDAY, "星期三");
EnumMap<Day, String> map2 = new EnumMap<>(map1);
System.out.println("map2:" + map2);
}
}
输出:
map2:{MONDAY=星期一, TUESDAY=星期二, WEDNESDAY=星期三}
以下是EnumMap类的一些基本方法:
public V put(K key, V value)
这个方法在EnumMap中将指定的值与指定的键相关联。如果先前存在该键的映射,则旧值将被替换。如果不存在该键的映射,则将其添加到EnumMap中。下面是一个示例:
import java.util.*;
public class Example {
enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
public static void main(String[] args) {
EnumMap<Day, String> map = new EnumMap<>(Day.class);
map.put(Day.MONDAY, "星期一");
map.put(Day.TUESDAY, "星期二");
map.put(Day.WEDNESDAY, "星期三");
System.out.println("map:" + map); // 输出:map:{MONDAY=星期一, TUESDAY=星期二, WEDNESDAY=星期三}
map.put(Day.MONDAY, "星期一啦!");
System.out.println("map:" + map); // 输出:map:{MONDAY=星期一啦!, TUESDAY=星期二, WEDNESDAY=星期三}
map.put(Day.FRIDAY, null);
System.out.println("map:" + map); // 输出:map:{MONDAY=星期一啦!, TUESDAY=星期二, WEDNESDAY=星期三, FRIDAY=null}
}
}
public V get(Object key)
这个方法返回指定键所映射的值,如果此映射不包含该键的映射,则返回null。下面是一个示例:
import java.util.*;
public class Example {
enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
public static void main(String[] args) {
EnumMap<Day, String> map = new EnumMap<>(Day.class);
map.put(Day.MONDAY, "星期一");
map.put(Day.TUESDAY, "星期二");
map.put(Day.WEDNESDAY, "星期三");
System.out.println("map.get(Day.TUESDAY):" + map.get(Day.TUESDAY)); // 输出:map.get(Day.TUESDAY):星期二
System.out.println("map.get(Day.FRIDAY):" + map.get(Day.FRIDAY)); // 输出:map.get(Day.FRIDAY):null
}
}
public int size()
这个方法返回此映射中的键值映射关系数目。下面是一个示例:
import java.util.*;
public class Example {
enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
public static void main(String[] args) {
EnumMap<Day, String> map = new EnumMap<>(Day.class);
map.put(Day.MONDAY, "星期一");
map.put(Day.TUESDAY, "星期二");
map.put(Day.WEDNESDAY, "星期三");
System.out.println("map.size():" + map.size()); // 输出:map.size():3
}
}
public void clear()
这个方法从EnumMap中删除所有的键值映射关系。下面是一个示例:
import java.util.*;
public class Example {
enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
public static void main(String[] args) {
EnumMap<Day, String> map = new EnumMap<>(Day.class);
map.put(Day.MONDAY, "星期一");
map.put(Day.TUESDAY, "星期二");
map.put(Day.WEDNESDAY, "星期三");
System.out.println("map:" + map); // 输出:map:{MONDAY=星期一, TUESDAY=星期二, WEDNESDAY=星期三}
map.clear();
System.out.println("map:" + map); // 输出:map:{}
}
}