Java EnumMap clear()

clear() 方法用于清空EnumMap的所有数据。此方法不会删除EnumMap本身,只是删除EnumMap所有条目。

1 语法 

public void clear()  

2 参数

3 返回值

4 EnumMap clear()示例1

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * Java EnumMap.clear的例子
 */
import java.util.EnumMap;

public class Demo {

    //创建Enum
    public enum Tutorial{
        Java,Python,PHP,Android,AngularJS
    };
    public static void main(String[] args) {
        //创建EnumMap
        EnumMap<Tutorial,String> map =
                new EnumMap<Tutorial,String >(Tutorial.class);
        map.put(Tutorial.Java,"1");
        map.put(Tutorial.Python,"2");
        map.put(Tutorial.PHP,"3");
        map.put(Tutorial.Android,"4");
        map.put(Tutorial.AngularJS,"5");
        //打印Map
        System.out.println("删除前:\n"+map);
        map.clear();
        System.out.println("删除后:\n"+map);
    }
}

输出结果为:

删除前:
{Java=1, Python=2, PHP=3, Android=4, AngularJS=5}
删除后:
{}

5 EnumMap clear()示例2

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * Java EnumMap.clear的例子
 */
import java.util.*;

public class Demo {

    //创建枚举
    public enum Days {
        Monday, Tuesday, Wednesday, Thursday
    };
    public static void main(String[] args) {
        //创建EnumMap
        EnumMap<Days, String> map= new EnumMap<Days, String>(Days.class);
        map.put(Days.Monday,"1");
        map.put(Days.Tuesday,"2");
        map.put(Days.Wednesday,"3");
        map.put(Days.Thursday,"4");
        //打印Map集合
        System.out.println("清空前:\n"+map);
        map.clear();
        System.out.println("清空后:\n"+map);
    }
}

输出结果为:

清空前:
{Monday=1, Tuesday=2, Wednesday=3, Thursday=4}
清空后:
{}

 

热门文章

优秀文章