Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction:
2.
KeyPoints about EnumMaps:
3.
Syntax:
4.
Constructors of EnumMap class:
5.
Methods of the EnumMap class:
6.
 
7.
EnumMap Example:
8.
Frequently Asked Questions:
9.
Key Takeaways: 
Last Updated: Mar 27, 2024

EnumMap

Author Nishant Rana
0 upvote

Introduction:

EnumMap is a class in Java that is present in the java.util package. It implements Serializable and Cloneable and extends from the AbstractMap class. It is a special implementation of the Map Interface for the Enums.

Also read, Duck Number in Java  And Hashcode Method in Java

KeyPoints about EnumMaps:

  1. EnumMaps are faster than HashMaps.
  2. We can not add NULL values in EnumMap. It will throw NullPointerException if we try to add a NULL value in an EnumMap.
  3. They are internally represented as arrays that are highly compact and efficient.

Syntax:

EnumMaps are declared using the following syntax:

public class EnumMap<K extends Enum<K>,V> extends AbstractMap<K,V> implements Serializable, Cloneable

K: It is the type of key we need to add.

V: It is the type of value we need to map to the key.

Also see,  Swap Function in Java

Constructors of EnumMap class:

  1. EnumMap(Class<K> keyType): It can be used to create an empty EnumMap with the specified key type.
     
  2. EnumMap(EnumMap<K,? extends V> m): It can be used to create an empty EnumMap which has the same Key type as the Enum Map type.
     
  3. EnumMap(Map<K,? extends V> m): It can be used to create an EnumMap which is initialized from another map.

 

Methods of the EnumMap class:

Method Description
clear() This method is used to clear all the mappings from the map.
clone() This method is used to copy the mapped value of one map to another map.
containsKey() This method is used to check if the specified key is present in the map or not.
containsValue() This method is used to check if any key is mapped with the specified value or not.
entrySet() This method is used to create a set of all the elements in the EnumMap.
equals()  This method is used to check if two maps are equal or not.
get() This method is used to get the mapped value of the specified key.
keySet() This method is used to get a set of keys in the map.
size() This method returns the size of the map.
hashCode() This method is used to get the hash code of the EnumMap.
remove() This method is used to remove the mapping of the specified key from the EnumMap if present.
put() This method is used to add a key with the mapped value in the EnumMap.

 

 

You can also read about the Java Destructor

EnumMap Example:

import java.util.*;  
public class EnumMapExample {  

  // creating an enum  
  public enum Days {  
  Monday, Tuesday, Wednesday, Thursday  
  };  

  public static void main(String[] args) {  
  //creating and populating enum map  
  EnumMap<Days, String> map = new EnumMap<Days, String>(Days.class);  
   
   map.put(Days.Monday, "1");  
  map.put(Days.Tuesday, "2");  
  map.put(Days.Wednesday, "3");  
  map.put(Days.Thursday, "4");  

  // printing the map  
  for(Map.Entry m:map.entrySet()){    
      System.out.println(m.getKey()+" "+m.getValue());    
      }   
  }  
}  

 

Output:

Monday 1
Tuesday 2
Wednesday 3
Thursday 4

Practice by yourself on online java compiler.

You can also read about entryset java here.

Frequently Asked Questions:

Q) What is EnumMap?

A) EnumMap is a special implementation of the Map Interface for the Enums.

Q) Why is EnumMap faster than HashMaps?

A) EnumMaps are faster than HashMaps because they are internally represented as arrays that are highly compact and efficient.

Key Takeaways: 

In this blog, we have covered the following things:

  1. We first discussed what are Enums.
  2. Then we discussed its different methods and example code.

If you want to learn more about Programming Language and want to practice some questions which require you to take your basic knowledge on Programming Languages a notch higher, then you can visit our here

Until then, all the best for your future endeavors, and Keep Coding.




 

Live masterclass