當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java EnumMap putAll(map)用法及代碼示例


Java中的Java.util.EnumMap.putAll(map)方法用於將所有映射從一個映射複製到一個新的映射。較舊的映射將替換為較新的映射。

用法:

void putAll(map)

參數:該方法采用一個參數Map。它是要複製到較新的Map。

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


以下示例程序旨在說明putAll()方法:

示例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}

示例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%}


相關用法


注:本文由純淨天空篩選整理自akash1295大神的英文原創作品 EnumMap putAll(map) Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。