The LinkedHashMap.keySet()
method in Java is used to obtain a set view of the keys contained in the LinkedHashMap
.
Table of Contents
- Introduction
keySet
Method Syntax- Examples
- Iterating Over Keys in a LinkedHashMap
- Modifying Values Using Key Set
- Real-World Use Case
- Example: Displaying Usernames
- Conclusion
Introduction
The LinkedHashMap.keySet()
method is a member of the LinkedHashMap
class in Java. It returns a Set
view of the keys contained in the map. This Set
is backed by the LinkedHashMap
, so changes to the map are reflected in the set, and vice-versa. This method is useful when you need to iterate over the keys or perform operations based on the keys.
keySet() Method Syntax
The syntax for the keySet
method is as follows:
public Set<K> keySet()
- The method does not take any parameters.
- The method returns a set view of the keys contained in the map.
Examples
Iterating Over Keys in a LinkedHashMap
The keySet
method can be used to iterate over the keys in a LinkedHashMap
.
Example
import java.util.LinkedHashMap;
import java.util.Set;
public class KeySetExample {
public static void main(String[] args) {
// Creating a LinkedHashMap with String keys and Integer values
LinkedHashMap<String, Integer> people = new LinkedHashMap<>();
// Adding entries to the LinkedHashMap
people.put("Ravi", 25);
people.put("Priya", 30);
people.put("Vijay", 35);
// Getting the key set
Set<String> keySet = people.keySet();
// Iterating over the key set
for (String key : keySet) {
System.out.println(key);
}
}
}
Output:
Ravi
Priya
Vijay
Modifying Values Using Key Set
You can also modify the values in the LinkedHashMap
using the key set.
Example
import java.util.LinkedHashMap;
import java.util.Set;
public class ModifyValuesUsingKeySetExample {
public static void main(String[] args) {
// Creating a LinkedHashMap with String keys and Integer values
LinkedHashMap<String, Integer> people = new LinkedHashMap<>();
// Adding entries to the LinkedHashMap
people.put("Ravi", 25);
people.put("Priya", 30);
people.put("Vijay", 35);
// Getting the key set
Set<String> keySet = people.keySet();
// Modifying the values using the key set
for (String key : keySet) {
if (key.equals("Ravi")) {
people.put(key, 26);
}
}
// Printing the modified LinkedHashMap
System.out.println("Modified LinkedHashMap: " + people);
}
}
Output:
Modified LinkedHashMap: {Ravi=26, Priya=30, Vijay=35}
Real-World Use Case
Example: Displaying Usernames
A common real-world use case for LinkedHashMap.keySet()
is displaying usernames stored in a LinkedHashMap
. For example, let’s consider a scenario where user profiles are stored with usernames as keys and profile information as values.
Example
import java.util.LinkedHashMap;
import java.util.Set;
public class DisplayUsernames {
public static void main(String[] args) {
// Creating a LinkedHashMap to store user profiles
LinkedHashMap<String, String> userProfiles = new LinkedHashMap<>();
// Adding user profiles to the LinkedHashMap
userProfiles.put("Ravi", "Age: 25, Location: Mumbai");
userProfiles.put("Priya", "Age: 30, Location: Delhi");
userProfiles.put("Vijay", "Age: 35, Location: Bangalore");
// Getting the key set
Set<String> usernames = userProfiles.keySet();
// Displaying the usernames
System.out.println("Usernames:");
for (String username : usernames) {
System.out.println(username);
}
}
}
Output:
Usernames:
Ravi
Priya
Vijay
In this example, LinkedHashMap.keySet()
is used to display usernames stored in a LinkedHashMap
, demonstrating how to iterate over and access keys in the map.
Conclusion
The LinkedHashMap.keySet()
method in Java provides a way to obtain a set view of the keys contained in the LinkedHashMap
. By understanding how to use this method, you can efficiently manage and manipulate collections of keys in your Java applications. The method allows you to iterate over keys, modify values, and perform operations based on keys, making it a versatile tool for data management.