Java中的Java.util.EnumMap.clear()方法用於從Map中刪除所有映射。該方法不會刪除Map,而是會清除映射的Map。
用法:
enum_map.clear()
參數:此方法不接受任何參數。
返回值:此方法不返回任何值。
以下程序說明了Java.util.EnumMap.clear()方法的用法:
示例1:
// Java program to demonstrate clear() method
import java.util.*;
// An enum of geeksforgeeks ranking across
// Worldwide & in India is created
public enum gfg {
Global,
India
}
;
class Enum_map {
public static void main(String[] args)
{
EnumMap<gfg, Integer> mp = new EnumMap<gfg, Integer>(gfg.class);
// Values are associated in mp
mp.put(gfg.Global, 800);
mp.put(gfg.India, 72);
// Values in mp before removing
System.out.println("Values in map before removing " + mp);
// Removing the values from mp
mp.clear();
// Values in mp after removing
System.out.println("Values in map after removing " + mp);
}
}
輸出:
Values in map before removing {Global=800, India=72} Values in map after removing {}
示例2:
// Java program to demonstrate clear() method
import java.util.*;
// An enum of fruits price is created
public enum Price_of_Fruits {
Orange,
Apple,
Banana,
Pomegranate,
Guava
}
;
class Enum_map {
public static void main(String[] args)
{
EnumMap<Price_of_Fruits, Integer> mp = new EnumMap<Price_of_Fruits, Integer>(Price_of_Fruits.class);
// Values are associated in mp
mp.put(Price_of_Fruits.Orange, 30);
mp.put(Price_of_Fruits.Apple, 50);
mp.put(Price_of_Fruits.Banana, 40);
mp.put(Price_of_Fruits.Pomegranate, 120);
mp.put(Price_of_Fruits.Guava, 20);
// Values in mp before removing
System.out.println("Values in map before removing " + mp);
// Removing the values from mp
mp.clear();
// Values in mp after removing
System.out.println("Values in map after removing " + mp);
}
}
輸出:
Values in map before removing {Orange=30, Apple=50, Banana=40, Pomegranate=120, Guava=20} Values in map after removing {}
相關用法
- Java EnumMap equals()用法及代碼示例
- Java Set clear()用法及代碼示例
- Java SortedSet clear()用法及代碼示例
- Java SortedMap clear()用法及代碼示例
- Java List clear()用法及代碼示例
- Java Calendar clear()用法及代碼示例
- Java IntBuffer clear()用法及代碼示例
- Java LongBuffer clear()用法及代碼示例
- Java Properties clear()用法及代碼示例
- Java LinkedHashSet clear()用法及代碼示例
- Java AbstractList clear()用法及代碼示例
- Java Collection clear()用法及代碼示例
- Java AbstractCollection clear()用法及代碼示例
- Java AbstractQueue clear()用法及代碼示例
- Java AbstractMap clear()用法及代碼示例
注:本文由純淨天空篩選整理自akash1295大神的英文原創作品 EnumMap clear() Method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。