Java中EnumMap的putAll(map)方法: Java.util.EnumMap.putAll(map)

作者: Arvin Chen 分类: Java 来源: Break易站(www.breakyizhan.com)

Java中的Java.util.EnumMap.putAll(map)方法用于将所有映射从一个映射复制到一个映射。较旧的映射将在较新的映射中替换。

句法:

void putAll(map)

参数:该方法采用一个参数映射。它是要复制到新的map。

返回值该方法不返回任何值。

下面的程序说明了Java.util.EnumMap.putAll(map)方法

Java.util.EnumMap.putAll(map)程序1:

// Java program to demonstrate keySet()
import java.util.*;

// An enum of geeksforgeeks
public enum gfg {
    Global_today,
    India_today,
    China_today
};

class Enum_demo {
    public static void main(String[] args)
    {

        EnumMap<gfg, Integer> mp1 =
                     new EnumMap<gfg, Integer>(gfg.class);

        EnumMap<gfg, Integer> mp2 =
                     new EnumMap<gfg, Integer>(gfg.class);

        // Values are associated
        mp1.put(gfg.Global_today, 799);
        mp1.put(gfg.India_today, 69);

        // Copies all the mappings of mp1 to mp2
        mp2.putAll(mp1);

        // Prints the first map
        System.out.println("Mappings in Map1: " + mp1);

        // Prints the second map
        System.out.println("Mappings in Map2: " + mp2);
    }
}

输出:

Mappings in Map1: {Global_today=799, India_today=69}
Mappings in Map2: {Global_today=799, India_today=69}

Java.util.EnumMap.putAll(map)程序2

// Java program to demonstrate the working of keySet()
import java.util.*;

// an enum of geeksforgeeks
// visitors in India and United States
public enum gfg {

    India_today,
    United_States_today
}
;

class Enum_demo {
    public static void main(String[] args)
    {

        EnumMap<gfg, String> mp1 =
                     new EnumMap<gfg, String>(gfg.class);

        EnumMap<gfg, String> mp2 =
                     new EnumMap<gfg, String>(gfg.class);

        // Values are associated
        mp1.put(gfg.India_today, "61.8%");
        mp1.put(gfg.United_States_today, "18.2%");

        // Copies all the mappings of mp1 to mp2
        mp2.putAll(mp1);

        // Prints the first map
        System.out.println("Mappings in Map1: " + mp1);

        // Prints the second map
        System.out.println("Mappings in Map2: " + mp2);
    }
}

输出:

Mappings in Map1: {India_today=61.8%, United_States_today=18.2%}
Mappings in Map2: {India_today=61.8%, United_States_today=18.2%}
  •   本文标题:Java中EnumMap的putAll(map)方法: Java.util.EnumMap.putAll(map) - Break易站
    转载请保留页面地址:https://www.breakyizhan.com/java/5456.html

    发表笔记

    电子邮件地址不会被公开。 必填项已用*标注

    更多阅读