EnumMap is specialized Map implementation designed and optimized for using Java Enum as key.
EnumMap Class Examples
Example 1: Simple Days Enum Example
In this example, we will use Days enum with all predefined days as keys in EnumMap:
import java.util.EnumMap;
import java.util.Map.Entry;
/**
* EnumMap Demonstration Example
* @author Ramesh Fadatare
*
*/
public class EnumMapExample {
enum Days {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY;
}
public static void main(final String[] args) {
final EnumMap<Days, String> enumMap = new EnumMap<>(Days.class);
enumMap.put(Days.SUNDAY, "Its Sunday!!");
enumMap.put(Days.MONDAY, "Its Monday!!");
enumMap.put(Days.TUESDAY, "Its Tuesday!!");
enumMap.put(Days.WEDNESDAY, "Its Wednesday!!");
enumMap.put(Days.THURSDAY, "Its Thursday!!");
enumMap.put(Days.FRIDAY, "Its Friday!!");
enumMap.put(Days.SATURDAY, "Its Saturday!!");
for(final Entry<Days, String> entry : enumMap.entrySet()){
System.out.println(" Key -> " + entry.getKey().SUNDAY);
System.out.println("Value - >" + entry.getValue());
}
}
}
Output:
Key -> SUNDAY
Value - >Its Sunday!!
Key -> SUNDAY
Value - >Its Monday!!
Key -> SUNDAY
Value - >Its Tuesday!!
Key -> SUNDAY
Value - >Its Wednesday!!
Key -> SUNDAY
Value - >Its Thursday!!
Key -> SUNDAY
Value - >Its Friday!!
Key -> SUNDAY
Value - >Its Saturday!!
Example 2: Real-world Example
As we know that every project has list status as a project is active, inactive, close, start etc. Let's create a ProjectStatus Enum which acts as a key against each project. Let's demonstrate this with an example:
import java.util.EnumMap;
import java.util.Map.Entry;
/**
* EnumMap Demonstration Example
* @author Ramesh Fadatare
*
*/
public class EnumMapExample2 {
public static void main(final String[] args) {
final EnumMap<ProjectStatus, Project> enumMap = new EnumMap<>(ProjectStatus.class);
enumMap.put(ProjectStatus.ACTIVE, new Project(100, "Customer Management System", "Customer Management System"));
enumMap.put(ProjectStatus.INACTIVE,
new Project(200, "Employee Management System", "Employee Management System"));
for (final Entry<ProjectStatus, Project> entry : enumMap.entrySet()) {
final ProjectStatus projectStatus = entry.getKey();
System.out.println(" Key -> " + projectStatus.name());
final Project project = entry.getValue();
System.out.println("Value - >" + project.toString());
}
}
enum ProjectStatus {
ACTIVE, INACTIVE
}
}
class Project {
private int id;
private String name;
private String desc;
public Project(final int id, final String name, final String desc) {
super();
this.id = id;
this.name = name;
this.desc = desc;
}
public int getId() {
return id;
}
public void setId(final int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
public String getDesc() {
return desc;
}
public void setDesc(final String desc) {
this.desc = desc;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "[project id : " + this.id + ", project name : "
+ this.name + ", project desc : " + this.desc + " ]";
}
}
Output:
Key -> ACTIVE
Value - >[project id : 100, project name : Customer Management System, project desc : Customer Management System ]
Key -> INACTIVE
Value - >[project id : 200, project name : Employee Management System, project desc : Employee Management System ]
Read more about EnumMap Class at EnumMap Class in Java
Reference
Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours
Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course