📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 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 (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
HashMap.keySet().stream()
method in Java is used to create a Stream
of the keys contained in the HashMap
. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.Table of Contents
- Introduction
keySet().stream()
Method Syntax- Examples
- Using
keySet().stream()
to Iterate Over Keys - Real-World Use Case: Filtering Keys Based on a Condition
- Using
- Conclusion
Introduction
The HashMap.keySet().stream()
method is a combination of HashMap.keySet()
and Set.stream()
. It provides a Stream
of the keys contained in the HashMap
. A Stream
is a sequence of elements supporting sequential and parallel aggregate operations. This method can be useful for performing complex operations on the keys of a HashMap
.
keySet().stream() Method Syntax
The syntax for obtaining a Stream
of keys from a HashMap
is as follows:
hashMap.keySet().stream()
- The method does not take any parameters.
- The method returns a
Stream
of the keys in theHashMap
.
Examples
Using keySet().stream()
to Iterate Over Keys
The keySet().stream()
method can be used to create a Stream
for iterating over the keys in a HashMap
.
Example
import java.util.HashMap; import java.util.stream.Stream; public class KeyStreamExample { public static void main(String[] args) { // Creating a HashMap with String keys and Integer values HashMap<String, Integer> people = new HashMap<>(); // Adding entries to the HashMap people.put("Ravi", 25); people.put("Priya", 30); people.put("Vijay", 35); // Getting the key stream Stream<String> keyStream = people.keySet().stream(); // Using the key stream to iterate over the keys keyStream.forEach(key -> System.out.println("Key: " + key)); } }
Output:
Key: Ravi Key: Priya Key: Vijay
Real-World Use Case: Filtering Keys Based on a Condition
In a real-world scenario, you might use the keySet().stream()
method to filter keys based on a certain condition, such as finding keys that start with a specific letter.
Example
import java.util.HashMap; import java.util.stream.Collectors; import java.util.stream.Stream; public class FilterKeysExample { public static void main(String[] args) { // Creating a HashMap with String keys and Integer values HashMap<String, Integer> people = new HashMap<>(); // Adding entries to the HashMap people.put("Ravi", 25); people.put("Priya", 30); people.put("Vijay", 35); people.put("Rohit", 40); // Getting the key stream Stream<String> keyStream = people.keySet().stream(); // Filtering keys that start with "R" Stream<String> filteredKeys = keyStream.filter(key -> key.startsWith("R")); // Collecting the filtered keys into a list and printing them System.out.println("Keys that start with 'R': " + filteredKeys.collect(Collectors.toList())); } }
Output:
Keys that start with 'R': [Ravi, Rohit]
Conclusion
The HashMap.keySet().stream()
method in Java provides a way to create a Stream
of the keys contained in the HashMap
. By understanding how to use this method, you can efficiently perform complex operations on the keys, such as filtering, mapping, and reducing. This method is useful in various scenarios, such as iterating over keys, filtering keys based on conditions, and performing aggregate operations on keys in a HashMap
.
Comments
Post a Comment
Leave Comment