🎓 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.values() method in Java is used to return a collection view of the values 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.values() can be used effectively.
Table of Contents
- Introduction
valuesMethod Syntax- Examples
- Basic Usage of
valuesMethod - Iterating Over Values
- Basic Usage of
- Real-World Use Case
- Example: Summing Up Task Durations
- Conclusion
Introduction
The EnumMap.values() method is a member of the EnumMap class in Java. It returns a collection view of the values contained in the map, allowing you to access all values stored in the EnumMap.
values() Method Syntax
The syntax for the values method is as follows:
public Collection<V> values()
- Returns: A collection view of the values contained in this map.
Examples
Basic Usage of values Method
The values method can be used to retrieve a collection of all values contained in an EnumMap.
Example
import java.util.EnumMap;
import java.util.Collection;
public class EnumMapValuesExample {
// 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 collection of values
Collection<String> values = holidays.values();
// Printing the collection of values
System.out.println("Values in holidays map: " + values);
}
}
Output:
Values in holidays map: [New Year's Day, Christmas Day]
Iterating Over Values
You can use the values method to iterate over the values in an EnumMap.
Example
import java.util.EnumMap;
import java.util.Collection;
public class EnumMapIterateValuesExample {
// 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 collection of values
Collection<Integer> values = fruitMap.values();
// Iterating over the values and printing them
System.out.println("Values in fruitMap:");
for (Integer value : values) {
System.out.println(value);
}
}
}
Output:
Values in fruitMap:
10
20
30
Real-World Use Case
Example: Summing Up Task Durations
A common real-world use case for EnumMap.values() is summing up the durations of tasks assigned for the week.
Example
import java.util.EnumMap;
import java.util.Collection;
public class TaskDurationCalculator {
// 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 task durations for each day
EnumMap<Day, Integer> taskDurations = new EnumMap<>(Day.class);
// Adding task durations for each day (in hours)
taskDurations.put(Day.MONDAY, 2);
taskDurations.put(Day.TUESDAY, 3);
taskDurations.put(Day.WEDNESDAY, 4);
taskDurations.put(Day.THURSDAY, 1);
taskDurations.put(Day.FRIDAY, 5);
// Retrieving the collection of task durations
Collection<Integer> durations = taskDurations.values();
// Summing up the task durations
int totalDuration = 0;
for (Integer duration : durations) {
totalDuration += duration;
}
// Printing the total duration of tasks for the week
System.out.println("Total duration of tasks for the week: " + totalDuration + " hours");
}
}
Output:
Total duration of tasks for the week: 15 hours
In this example, EnumMap.values() is used to retrieve the task durations and sum them up to find the total duration of tasks for the week.
Conclusion
The EnumMap.values() method in Java provides a way to get a collection view of the values contained in the map. By understanding how to use this method, you can efficiently access and iterate over the values in collections where the keys are enum constants. This method allows you to manage and utilize the values 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