🎓 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
Table of Contents
- Introduction
- Converting Map Keys to Set
- Converting Map Values to Set
- Converting Map Entries to Set
- Conclusion
Introduction
In Java, a Map is a collection that maps keys to values, with each key mapping to at most one value. A Set is a collection that contains no duplicate elements. Converting a map to a set allows you to work with the keys, values, or entries in a manner that enforces uniqueness.
Converting Map Keys to Set
To convert the keys of a map to a set, you can use the keySet method provided by the Map interface.
Example
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class MapToSetExample {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("apple", 1);
map.put("banana", 2);
map.put("cherry", 3);
// Convert map keys to set
Set<String> keySet = map.keySet();
System.out.println("Map: " + map);
System.out.println("Set of keys: " + keySet);
}
}
Explanation
- A
HashMapis created and populated with key-value pairs. - The
keySetmethod is called on the map to get a set of the keys. - The resulting set contains all the keys from the map.
Output:
Map: {apple=1, banana=2, cherry=3}
Set of keys: [apple, banana, cherry]
Converting Map Values to Set
To convert the values of a map to a set, you can use the values method provided by the Map interface and then create a new HashSet from the collection of values.
Example
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class MapToSetExample {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("apple", 1);
map.put("banana", 2);
map.put("cherry", 3);
// Convert map values to set
Set<Integer> valueSet = new HashSet<>(map.values());
System.out.println("Map: " + map);
System.out.println("Set of values: " + valueSet);
}
}
Explanation
- A
HashMapis created and populated with key-value pairs. - The
valuesmethod is called on the map to get a collection of the values. - A new
HashSetis created from the collection of values, resulting in a set of the values.
Output:
Map: {apple=1, banana=2, cherry=3}
Set of values: [1, 2, 3]
Converting Map Entries to Set
To convert the entries of a map to a set, you can use the entrySet method provided by the Map interface.
Example
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class MapToSetExample {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("apple", 1);
map.put("banana", 2);
map.put("cherry", 3);
// Convert map entries to set
Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
System.out.println("Map: " + map);
System.out.println("Set of entries: " + entrySet);
}
}
Explanation
- A
HashMapis created and populated with key-value pairs. - The
entrySetmethod is called on the map to get a set of the entries. - The resulting set contains all the key-value pairs from the map as
Map.Entryobjects.
Output:
Map: {apple=1, banana=2, cherry=3}
Set of entries: [apple=1, banana=2, cherry=3]
Conclusion
Converting a map to a set in Java can be accomplished in various ways, depending on whether you want to work with the keys, values, or entries of the map. The keySet method provides a simple way to get a set of the keys, the values method combined with a HashSet allows you to get a set of the values, and the entrySet method provides a set of the entries. Depending on your specific use case and requirements, you can choose the method that best fits your needs.
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