The EnumMap.size()
method in Java is used to determine the number of key-value mappings present in the map. This guide will cover the method’s usage, explain how it works, and provide examples to demonstrate its functionality using enum constants as keys. We will also cover a real-world use case to show how EnumMap.size()
can be used effectively.
Table of Contents
- Introduction
size
Method Syntax- Examples
- Basic Usage of
size
Method - Working with Empty and Non-Empty Maps
- Basic Usage of
- Real-World Use Case
- Example: Tracking Assigned Tasks
- Conclusion
Introduction
The EnumMap.size()
method is a member of the EnumMap
class in Java. It returns the number of key-value mappings in the map, allowing you to determine how many entries are present in the EnumMap
.
size() Method Syntax
The syntax for the size
method is as follows:
public int size()
- Returns: The number of key-value mappings in this map.
Examples
Basic Usage of size
Method
The size
method can be used to find out how many entries are present in an EnumMap
.
Example
import java.util.EnumMap;
public class EnumMapSizeExample {
// 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 with Day as key and String as value
EnumMap<Day, String> tasks = new EnumMap<>(Day.class);
// Adding entries to the EnumMap
tasks.put(Day.MONDAY, "Go to gym");
tasks.put(Day.TUESDAY, "Attend meeting");
tasks.put(Day.WEDNESDAY, "Work from home");
// Printing the size of the EnumMap
System.out.println("Number of tasks assigned: " + tasks.size());
}
}
Output:
Number of tasks assigned: 3
Working with Empty and Non-Empty Maps
The size
method returns 0
for an empty EnumMap
and the correct count for a non-empty EnumMap
.
Example
import java.util.EnumMap;
public class EnumMapEmptySizeExample {
enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
public static void main(String[] args) {
// Create an empty EnumMap with Day as key and String as value
EnumMap<Day, String> tasks = new EnumMap<>(Day.class);
// Printing the size of the empty EnumMap
System.out.println("Number of tasks assigned (initial): " + tasks.size());
// Adding an entry to the EnumMap
tasks.put(Day.MONDAY, "Go to gym");
// Printing the size after adding an entry
System.out.println("Number of tasks assigned (after adding): " + tasks.size());
}
}
Output:
Number of tasks assigned (initial): 0
Number of tasks assigned (after adding): 1
Real-World Use Case
Example: Tracking Assigned Tasks
A common real-world use case for EnumMap.size()
is tracking the number of tasks assigned for the week.
Example
import java.util.EnumMap;
public class TaskTracker {
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");
// Printing the number of tasks assigned for the week
System.out.println("Total number of tasks assigned for the week: " + tasks.size());
}
}
Output:
Total number of tasks assigned for the week: 7
In this example, EnumMap.size()
is used to count the total number of tasks assigned for the week, making it easy to track task assignments.
Conclusion
The EnumMap.size()
method in Java provides a way to determine the number of key-value mappings in the map. By understanding how to use this method, you can efficiently manage and track the number of entries in collections where the keys are enum constants. This method allows you to get the size of an EnumMap
, making it a versatile tool for managing data in various scenarios.