GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||
GeezerVison is showing www.tutorialspoint.com — Java Util EnumMap
Selected Reading
Java Util EnumMapIntroductionThe Java EnumMap class is a specialized Map implementation for use with enum keys. The EnumMap class is part of the Collections Framework in Java. The Enum maps are implemented as arrays internally, which makes this implementation very efficient and compact. It is a high-performance map implementation and is faster than HashMap. The EnumMap does not allow Null keys, and will throw a NullPointerException error if we try to insert a null key. However, Null values are allowed in the EnumMap class. CharactersticsThe following are the important points about EnumMap −
Class DeclarationThe following is the declaration for java.util.EnumMap class − public class EnumMap<K extends Enum<K>,V> extends AbstractMap<K,V> implements Serializable, Cloneable Parameters −
Why do we need an EnumMap Class?Suppose we have a basic need where we have to map days of the week with the subject we have to study on that day − MONDAY Mathematics TUESDAY Physics WEDNESDAY Chemistry THURSDAY Computer Science FRIDAY English Then, to organize the above needs, we can use an EnumMap as per the requirements, like here we have days of the week, which will be later used as the keys for our map − public enum Days_Of_Week {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
Creating an EnumMapTo create an EnumMap class, first we need to import the java.util.EnumMap or java.util.* package. Then we need to create an object of it to initialize it. SyntaxBelow is the syntax for initializing an EnumMap in Java − EnumMap<Days_Of_Week, String> subject = new EnumMap<>(Days_Of_Week.class); Above the Days_Of_Week is the key of the enum that maps to values, and strings are the values of the enum map associated with the corresponding keys. ExampleBelow is an example of creating an EnumMap in Java − package com.tutorialspoint;
import java.util.EnumMap;
public class EnumMapDemo {
// creating an enum for the EnumMap
public enum Days_Of_Week {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
public static void main(String[] args) {
// creating an EnumMap with Days_Of_Week as keys and String as values
EnumMap<Days_Of_Week, String> subject =
new EnumMap<>(Days_Of_Week.class);
// associate values in map
subject.put(Days_Of_Week.MONDAY, "Mathematics");
subject.put(Days_Of_Week.TUESDAY, "Physics");
subject.put(Days_Of_Week.WEDNESDAY, "Chemistry");
subject.put(Days_Of_Week.THURSDAY, "Computer Science");
subject.put(Days_Of_Week.FRIDAY, "English");
// print the whole map
System.out.println(subject);
}
}
Let us compile and run the above program. This will produce the following result − {MONDAY=Mathematics, TUESDAY=Physics, WEDNESDAY=Chemistry,
THURSDAY=Computer Science, FRIDAY=English}
Class ConstructorsThe following are the class constructors supported by the EnumMap in Java −
Class MethodsThe following are the class methods supported by the EnumMap in Java −
Methods inheritedThis class inherits methods from the following classes −
Adding a Key-Value to an EnumMapThe following example shows the usage of Java EnumMap put(K,V) method to put a value in the EnumMap instance. We've created a enum Numbers. Then EnumMap is created of enum Numbers and Integer. Few entries are added using put(K,V) and enumMap is printed. Using put() method again, a value of enumMap is replaced and map is printed again. package com.tutorialspoint;
import java.util.EnumMap;
public class EnumMapDemo {
// create an enum
public enum Numbers{ONE, TWO, THREE, FOUR, FIVE};
public static void main(String[] args) {
EnumMap<Numbers,Integer> map =
new EnumMap<>(Numbers.class);
// associate values in map
map.put(Numbers.ONE, 1);
map.put(Numbers.TWO, 2);
map.put(Numbers.THREE,3);
// print the whole map
System.out.println(map);
map.put(Numbers.THREE, 4);
// print the updated map
System.out.println(map);
}
}
Let us compile and run the above program. This will produce the following result − {ONE=1, TWO=2, THREE=3}
{ONE=1, TWO=2, THREE=4}
Advertisements
| ||||||||||||||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |