Home >Java >javaTutorial >Java EnumMap
A Java Collection Framework member known as EnumMap, which extends AbstractMap, is a specific implementation for the interface map, mainly for enumeration types. In addition to that, several other features are also present for EnumMap.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
It includes:
More details on Java EnumMap will be discussed in the following sections.
Syntax
Java EnumMap can be declared using the below syntax.
public class EnumMap<Key extends Enum<Key>,Value> extends AbstractMap<Key,Value> implements Serializable, Cloneable
The following are the constructors that are commonly used in Java EnumMap:
Now, let us see some of the commonly used methods of Java EnumMap.
In order to understand more about the Java Enum Map, let us implement the above-mentioned methods in some programs.
Sample program to create an enum map and copy the elements to another enum map.
Code:
import java.util.EnumMap; class JavaEnumMapExample { enum fruits { APPLE, ORANGE, GRAPES, KIWI } public static void main(String[] args) { // EnumMap creation of the fruits enum EnumMap<fruits, Integer> fr = new EnumMap<>(fruits.class); // add key-value to the mapping using the method put() fr.put(fruits.APPLE, 2); fr.put(fruits.ORANGE, 5); System.out.println("The key-value pairs in EnumMap 1 is : " + fr); //create another enum map fru EnumMap<fruits, Integer> fru = new EnumMap<>(fruits.class); // copy all the elements from first enum map to this using the methodputAll() fru.putAll(fr); fru.put(fruits.GRAPES, 3); System.out.println("The key-value pairs in EnumMap 2 is : " + fru); } }
Output:
Explanation to the above program: In the above program, two enum maps are created. The first map is created with 2 elements, and the second map is created by copying the elements of the first map. In addition to that, an extra element is also added to the second map. These are done with the help of put() and putAll() methods.
Sample program to create an enum map and get the keys and values separately.
Code:
import java.util.EnumMap; class JavaEnumMapExample { enum fruits { APPLE, ORANGE, GRAPES, KIWI } public static void main(String[] args) { // EnumMap creation of the fruits enum EnumMap<fruits, Integer> fr = new EnumMap<>(fruits.class); // add key-value to the mapping using the method put() fr.put(fruits.APPLE, 2); fr.put(fruits.ORANGE, 5); System.out.println("The key-value pairs in EnumMap 1 is : " + fr); // print all the keys in the enum map using the method keySet() System.out.println("The keys in enum map 1 are : " + fr.keySet()); // print all the values in the enum map using the method values() System.out.println("The values in enum map 1 are : " + fr.values()); //create another enum map fru EnumMap<fruits, Integer> fru = new EnumMap<>(fruits.class); // copy all the elements from first enum map to this using the methodputAll() fru.putAll(fr); fru.put(fruits.GRAPES, 3); System.out.println("The key-value pairs in EnumMap 2 is : " + fru); // print all the keys in the enum map using the method keySet() System.out.println("The keys in enum map 2 are : " + fru.keySet()); // print all the values in the enum map using the method values() System.out.println("The values in enum map 2 are : " + fru.values()); } }
Output:
Explanation to the above program: Similar to the first program, two enum maps are available here. This program displays the maps’ keys and values independently using the methods keyset() and values(), respectively.
Sample program to remove an element from the enum map
Code:
import java.util.EnumMap; class JavaEnumMapExample { enum fruits { APPLE, ORANGE, GRAPES, KIWI } public static void main(String[] args) { // EnumMap creation of the fruits enum EnumMap<fruits, Integer> fr = new EnumMap<>(fruits.class); // add key-value to the mapping using the method put() fr.put(fruits.APPLE, 2); fr.put(fruits.ORANGE, 5); System.out.println("The key-value pairs in EnumMap : " + fr); // remove an element using the method remove() int val = fr.remove(fruits.APPLE); System.out.println("Removed Value: " + val); System.out.println("The key-value pairs in EnumMap after removing apple : " + fr); } }
Output:
Explanation to the above program: In this program, an element is removed from the map using the remove() method and the resultant enum map is printed in the next step.
A detailed explanation of all the aspects such as declaration, methods, constructors of Java EnumMap is discussed in this document in detail.
The above is the detailed content of Java EnumMap. For more information, please follow other related articles on the PHP Chinese website!