Java EnumMap remove()

remove() 从EnumMap中删除给定key键的值。

1 语法

public V remove(Object key)

2 参数

key:要删除的key。

3 返回值

返回与指定key的旧value值;如果指定key没有值,则返回null。

4 EnumMap remove()示例1

package com.yiidian;

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

public class Demo {

    //创建枚举
    public enum Tutorial {
        Java, CSS, Python, Android, Javascript
    };
    public static void main(String[] args) {
        EnumMap<Tutorial, String>  map =
                new EnumMap<Tutorial, String> (Tutorial.class);
        map.put(Tutorial.Java, "1");
        map.put(Tutorial.CSS, "2");
        map.put(Tutorial.Python, "3");
        map.put(Tutorial.Android, "4");
        //输出Map
        System.out.println("Map: "+map);
        map.remove(Tutorial.CSS);
        //输出Map
        System.out.println("Updated Map: "+map);
    }
}

输出结果为:

Map: {Java=1, CSS=2, Python=3, Android=4}
Updated Map: {Java=1, Python=3, Android=4}

5 EnumMap remove()示例2

package com.yiidian;

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

public class Demo {

    // 创建枚举
    public enum Days {
        Monday,Tuesday, Wednesday, Thursday
    };
    public static void main(String[] args) {
        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
        System.out.println("Map: "+map);
        map.remove(Days.Thursday);
        //输出Map
        System.out.println("Updated Map: "+map);
    }
}

输出结果为:

Map: {Monday=1, Tuesday=2, Wednesday=3}
Updated Map: {Monday=1, Tuesday=2, Wednesday=3}

热门文章

优秀文章