The Java.util.EnumMap.clone() method in Java is used to copy the mapped values of one map to another. It basically creates a shallow copy of this map.
Syntax:
Enum_map_2 = Enum_map_1.clone()
Parameters: The method does not accept any argument.
Return Value: The method returns a shallow copy of a EnumMap.
Below programs illustrate the Java.util.EnumMap.clone() method
Program 1:
// Java program to demonstrate 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}
Program 2:
// Java program to demonstrate 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}
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- EnumMap remove() Method in Java
- EnumMap size() Method in Java
- EnumMap equals() Method in Java with Examples
- EnumMap clear() Method in Java with Examples
- EnumMap entrySet() Method in Java
- EnumMap containsKey() Method in Java
- EnumMap containsValue(value) method in Java
- EnumMap values() Method in Java
- EnumMap put() Method in Java
- EnumMap putAll(map) Method in Java
- EnumMap keySet() Method in Java
- EnumMap get() Method in Java
- EnumMap class in Java
- Clone() method in Java
- LinkedList clone() Method in Java
- HashSet clone() Method in Java
- TreeSet clone() Method in Java
- HashMap clone() Method in Java
- Hashtable clone() Method in Java
- TreeMap clone() Method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : nidhi_biet

