Java EnumMap putAll()

putAll() 将所有映射从一个EnumMap复制到新的EnumMap。

1 语法

public void putAll(Map<? extends K,? extends V> m)  

2 参数

m:需要复制的EnumMap。

3 返回值

4 EnumMap putAll()示例1

package com.yiidian;

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

public class Demo {

    //创建枚举
    public enum Days {
        Monday,Tuesday, Wednesday, Thursday
    };
    public static void main(String[] args) {
        EnumMap<Days, String> map1 = new EnumMap<Days, String> (Days.class);
        EnumMap<Days, String> map2 = new EnumMap<Days, String> (Days.class);

        map1.put(Days.Monday, "1");
        map1.put(Days.Tuesday, "2");
        map1.put(Days.Wednesday, "3");
        // 输出map1和map2
        System.out.println("Map1: "+map1);
        System.out.println("Map2: "+map2);
        //把map1的数据复制到map2
        map2.putAll(map1);
        // 输出map1和map2
        System.out.println("Map1: "+map1);
        System.out.println("Map2: "+map2);
    }
}

输出结果为:

Map1: {Monday=1, Tuesday=2, Wednesday=3}
Map2: {}
Map1: {Monday=1, Tuesday=2, Wednesday=3}
Map2: {Monday=1, Tuesday=2, Wednesday=3}

5 EnumMap putAll()示例2

package com.yiidian;

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

public class Demo {

    // 创建枚举
    public enum Tutorial {
        Java, CSS, Python, Android, Javascript
    };
    public static void main(String[] args) {
        EnumMap<Tutorial, String> map1 =
                new EnumMap<Tutorial, String> (Tutorial.class);
        EnumMap<Tutorial, String> map2 =
                new EnumMap<Tutorial, String> (Tutorial.class);

        map1.put(Tutorial.Java, "1");
        map1.put(Tutorial.CSS, "2");
        map1.put(Tutorial.Python, "3");
        map1.put(Tutorial.Android, "4");
        //输出map1和map2
        System.out.println("Map1: "+map1);
        System.out.println("Map2: "+map2);
        map2.putAll(map1);
        //输出map1和map2
        System.out.println("Map1: "+map1);
        System.out.println("Map2: "+map2);
    }
}

输出结果为:

Map1: {Java=1, CSS=2, Python=3, Android=4}
Map2: {}
Map1: {Java=1, CSS=2, Python=3, Android=4}
Map2: {Java=1, CSS=2, Python=3, Android=4}

 

热门文章

优秀文章