🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
The EnumMap.entrySet() method in Java is used to return a set view of the mappings contained in the map. This guide will cover the method's usage with examples, and we will also cover a real-world use case to show how EnumMap.entrySet() can be used effectively.
Table of Contents
- Introduction
entrySetMethod Syntax- Examples
- Basic Usage of
entrySetMethod - Iterating Over Entries
- Basic Usage of
- Real-World Use Case
- Example: Printing Task Assignments
- Conclusion
Introduction
The EnumMap.entrySet() method is a member of the EnumMap class in Java. It returns a set view of the mappings contained in the map, allowing you to access all key-value pairs stored in the EnumMap.
entrySet() Method Syntax
The syntax for the entrySet method is as follows:
public Set<Map.Entry<K,V>> entrySet()
- Returns: A set view of the mappings contained in this map.
Examples
Basic Usage of entrySet Method
The entrySet method can be used to retrieve a set of all key-value pairs contained in an EnumMap.
Example
import java.util.EnumMap;
import java.util.Set;
import java.util.Map;
public class EnumMapEntrySetExample {
// Define an enum representing months of the year
enum Month {
JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER
}
public static void main(String[] args) {
// Create an EnumMap with Month as key and String as value
EnumMap<Month, String> holidays = new EnumMap<>(Month.class);
// Adding entries to the EnumMap
holidays.put(Month.JANUARY, "New Year's Day");
holidays.put(Month.DECEMBER, "Christmas Day");
// Retrieving the set of entries
Set<Map.Entry<Month, String>> entries = holidays.entrySet();
// Printing the set of entries
System.out.println("Entries in holidays map: " + entries);
}
}
Output:
Entries in holidays map: [JANUARY=New Year's Day, DECEMBER=Christmas Day]
Iterating Over Entries
You can use the entrySet method to iterate over the key-value pairs in an EnumMap.
Example
import java.util.EnumMap;
import java.util.Set;
import java.util.Map;
public class EnumMapIterateEntriesExample {
// Define an enum representing types of fruits
enum Fruit {
APPLE, BANANA, ORANGE, MANGO, GRAPE
}
public static void main(String[] args) {
// Create an EnumMap with Fruit as key and Integer as value
EnumMap<Fruit, Integer> fruitMap = new EnumMap<>(Fruit.class);
// Adding entries to the EnumMap
fruitMap.put(Fruit.APPLE, 10);
fruitMap.put(Fruit.BANANA, 20);
fruitMap.put(Fruit.ORANGE, 30);
// Retrieving the set of entries
Set<Map.Entry<Fruit, Integer>> entries = fruitMap.entrySet();
// Iterating over the entries and printing them
System.out.println("Entries in fruitMap:");
for (Map.Entry<Fruit, Integer> entry : entries) {
System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue());
}
}
}
Output:
Entries in fruitMap:
Key: APPLE, Value: 10
Key: BANANA, Value: 20
Key: ORANGE, Value: 30
Real-World Use Case
Example: Printing Task Assignments
A common real-world use case for EnumMap.entrySet() is printing out all task assignments for the week.
Example
import java.util.EnumMap;
import java.util.Set;
import java.util.Map;
public class TaskPrinter {
// Define an enum representing days of the week
enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
public static void main(String[] args) {
// Create an EnumMap to manage tasks for each day
EnumMap<Day, String> tasks = new EnumMap<>(Day.class);
// Adding tasks for each day
tasks.put(Day.MONDAY, "Go to gym");
tasks.put(Day.TUESDAY, "Attend meeting");
tasks.put(Day.WEDNESDAY, "Work from home");
tasks.put(Day.THURSDAY, "Team lunch");
tasks.put(Day.FRIDAY, "Project presentation");
tasks.put(Day.SATURDAY, "Family time");
tasks.put(Day.SUNDAY, "Rest day");
// Retrieving the set of entries
Set<Map.Entry<Day, String>> entries = tasks.entrySet();
// Printing all task assignments
System.out.println("Task assignments for the week:");
for (Map.Entry<Day, String> entry : entries) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}
Output:
Task assignments for the week:
MONDAY: Go to gym
TUESDAY: Attend meeting
WEDNESDAY: Work from home
THURSDAY: Team lunch
FRIDAY: Project presentation
SATURDAY: Family time
SUNDAY: Rest day
In this example, EnumMap.entrySet() is used to print out all task assignments for the week, making it easy to see the distribution of tasks.
Conclusion
The EnumMap.entrySet() method in Java provides a way to get a set view of the mappings contained in the map. By understanding how to use this method, you can efficiently access and iterate over the key-value pairs in collections where the keys are enum constants. This method allows you to manage and utilize the entries in an EnumMap, making it a versatile tool for managing data in various scenarios.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment