Java中的Java.util.EnumMap.clone()方法用于将一个映射的映射值复制到另一个映射。它基本上创建了这个EnumMap的浅表副本。

句法:

Enum_map_2 = Enum_map_1.clone()

参数:该方法不接受任何参数。

返回值:该方法返回EnumMap的浅表副本。

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

Java.util.EnumMap.clone()程序1:

// Java program to demonsrate clone() method
import java.util.*;
 
// An enum of fruits price is created
public enum Price_of_Fruits {
    Orange,
    Apple,
    Banana,
    Pomegranate,
    Guava
};
 
class Enum_map {
    public static void main(String[] args)
    {
 
        EnumMap<Price_of_Fruits, Integer> mp1 = new EnumMap<Price_of_Fruits, Integer>(Price_of_Fruits.class);
 
        EnumMap<Price_of_Fruits, Integer> mp2 = new EnumMap<Price_of_Fruits, Integer>(Price_of_Fruits.class);
 
        // Values are associated in mp1
        mp1.put(Price_of_Fruits.Orange, 30);
        mp1.put(Price_of_Fruits.Apple, 60);
        mp1.put(Price_of_Fruits.Banana, 40);
        mp1.put(Price_of_Fruits.Pomegranate, 120);
        mp1.put(Price_of_Fruits.Guava, 20);
 
        // Price of fruits in mp1
        System.out.println("Price of fruits in 1st map " + mp1);
 
        // Copying the values of mp1 to mp2
        mp2 = mp1.clone();
 
        // Price of fruits in mp2
        System.out.println("Price of fruits in 2nd map " + mp2);
    }
}

输出:

Price of fruits in 1st map {Orange=30, Apple=60, Banana=40, Pomegranate=120, Guava=20}
Price of fruits in 2nd map {Orange=30, Apple=60, Banana=40, Pomegranate=120, Guava=20}

Java.util.EnumMap.clone()程序2:

// Java program to demonsrate clone() method
import java.util.*;
 
// An enum of gfg ranking is created
public enum gfg_ranking {
    Global_2018,
    India_2018
};
 
class Enum_map {
    public static void main(String[] args)
    {
 
        EnumMap<gfg_ranking, Integer> mp1 = new EnumMap<gfg_ranking, Integer>(gfg_ranking.class);
 
        EnumMap<gfg_ranking, Integer> mp2 = new EnumMap<gfg_ranking, Integer>(gfg_ranking.class);
 
        // Values are associated in mp1
        mp1.put(gfg_ranking.Global_2018, 800);
        mp1.put(gfg_ranking.India_2018, 72);
 
        // Price of fruits in mp1
        System.out.println("GeeksforGeeks ranking in first map " + mp1);
 
        // Copying the values of mp1 to mp2
        mp2 = mp1.clone();
 
        // Price of fruits in mp2
        System.out.println("GeeksforGeeks ranking in second map " + mp2);
    }
}

输出:

GeeksforGeeks ranking in first map {Global_2018=800, India_2018=72}
GeeksforGeeks ranking in second map {Global_2018=800, India_2018=72}
 
转载请保留页面地址:https://www.breakyizhan.com/java/5451.html
扫描二维码添加微信 
  • ,每次淘宝领取淘宝优惠券,淘宝购物更优惠。现在添加微信,还可以领取机械键盘50元优惠券!添加微信后回复机械键盘即可领取!
    支持我们,就用微信淘宝!