🎓 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
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
Streamof 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.
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