java.util.EnumMap.clear() 方法用于从此映射中删除所有映射。
语法
public void clear()
参数
无需参数。
返回值
void 类型.
异常
无示例:
在下面的示例中,java.util .EnumMap.clear()方法用于清除给定map的所有映射。
import java.util.*;
public class MyClass {
//创建一个枚举
public enum weekday{
SUN, MON, TUE, WED, THU, FRI, SAT
}
public static void main(String[] args) {
//创建一个EnumMap
EnumMap<weekday,Integer> MyMap =
new EnumMap<weekday,Integer>(weekday.class);
//关联map中的值
MyMap.put(weekday.SUN, 0);
MyMap.put(weekday.MON, 1);
MyMap.put(weekday.TUE, 2);
MyMap.put(weekday.WED, 3);
MyMap.put(weekday.THU, 4);
MyMap.put(weekday.FRI, 5);
MyMap.put(weekday.SAT, 6);
//打印EnumMap
System.out.println("Before applying clear() method.");
System.out.println("MyMap contains: " + MyMap);
//使用clear方法清除EnumMap的内容
MyMap.clear();
//打印EnumMap
System.out.println("\nAfter applying clear() method.");
System.out.println("MyMap contains: " + MyMap);
}
}
上述代码的输出将是:
Before applying clear() method.
MyMap contains: {SUN=0, MON=1, TUE=2, WED=3, THU=4, FRI=5, SAT=6}
After applying clear() method.
MyMap contains: {}