Hashmap KeySet Method in java with examples using NetBeans
Description:
Did you know that there is a convenient way to use the Hashmap keySet method when using Map in Java?
This time, I will explain how to use the keySet method to solve such problems in an easy-to-understand manner!
How to use Hashmap keySet Method in Java
The keySet method is used for variables of the Map interface and returns the set value of the key contained in the Map. Here we will explain the basic syntax of the Hashmap keySet method and how to use it.
the basic syntax of the KeySet Method
Use the keySet method as follows:
1 |
Map.keySet() |
How to get the stored keys using Hashmap KeySet Method in Java Program:
In the below program, the key value is obtained using the keySet method on the Map.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
package com.mycompany.javabasics; import java.util.*; /**  *  * @author Fawadkhan  */ public class HashmapKeySetExample {        public static void main(String[] args) throws Exception {              // variable declaration for HashMap        Map<Integer, String> data = new HashMap<Integer, String>();              // data store in hashmap variable        data.put( 1, "Fawad khan");        data.put( 2, "Hamza khan");        data.put( 3, "Sana");        data.put( 4, "Shaista");        data.put( 5, "khan");        data.put( 6, "xyz");              // use keySet method to get the key        Set<Integer> MapSet = data.keySet();          // Check the value of the variable MapSet of the Set interface        for (Iterator<Integer> n = MapSet.iterator(); n.hasNext();)        {            System.out.println(n.next());        }    } } |
Output:
If you use the Hashmap keySet method, the set value of the Map added by the put method will be returned. If you check the contents of the MapSet obtained by the keySet method in a loop, you can see that the key value of the Map has been obtained.
Example: How to get the stored values using Hashmap keySet method:
As explained in the Basic Syntax, you can get the set value of a Map using the hashmap keySet method. Therefore, it is convenient to use the keySet method when you want to process all the values ​​added to the Map.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
package com.mycompany.javabasics; import java.util.*; /**  *  * @author Fawadkhan  */ public class HashmapKeySetExample {    public static void main(String[] args) throws Exception {        // variable declaration for HashMap        Map<Integer, String> data = new HashMap<Integer, String>();        // data store in hashmap variable        data.put(1, "Fawad khan");        data.put(2, "Hamza khan");        data.put(3, "Sana");        data.put(4, "Shaista");        data.put(5, "khan");        data.put(6, "xyz");        // use keySet method to get the key        Set<Integer> MapSet = data.keySet();        //for loop is used to get the number of elements value of the variable using KeySet        for (Integer nKey : data.keySet()) {            System.out.println(data.get(nKey));        }    } } |
Output:
first, the variable data of the Map interface is declared and the value is added using the put method. Next, the keySet method is used for data, and the extended for statement retrieves the sequential values.
Exception handling with Hashmap keySet method:
keySet is a method whose values ​​are preserved by shallow copying. A shallow copy is used in the sense of a shallow copy, and means that the data referenced by the copy source object and the copy destination object are the same.
In other words, if the value referenced by the copied object changes, the value of the copied object will also change . Below is a sample of adding a value to the copy source Map after getting the Set value with keySet.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
package com.mycompany.javabasics; import java.util.*; /**  *  * @author Fawadkhan  */ public class HashmapKeySetExample {    public static void main(String[] args) throws Exception {        // map declaration        Map<String, String> data = new HashMap<String, String>();        // Store data in Map        data.put("1", "Fawad khan");        data.put("2", "Hamza khan");        data.put("3", "sana");        // use keySet to get the key        Set<String> MapSet = data.keySet();        // Store more data in Map        data.put("4", "shaista");        data.put("5", "Fahad");        data.put("6", "john");        data.put("7", "xyz");        // check the value of the variable MapSet of the Set interface in a loop        for (Iterator<String> n = MapSet.iterator(); n.hasNext();) {            System.out.println(n.next());        }    } } |
Output:
first, the variable data of the Map interface is declared and the value is added using the put method. Then the keySet method is used for data to get the Set value.
After that, I am adding more data to the Map using the put method. If you check the value of the MapSet variable of the Set interface obtained by keySet in a loop, you can see that the value of “4”, “5”, “6”, “7” added by the put method is also added to MapSet after the Set value is obtained by the keySet method.
As mentioned above, if you change the contents of the Map after obtaining the value using the keySet method, the Set value returned by keySet will also change.
Therefore, if you use keySet carelessly, the value will increase or decrease due to subsequent Map operations (addition, deletion, etc.), so unexpected problems may occur. When using keySet, be very aware that keySet is a shallow copy.
summary
in this article, I explained the basic syntax of the HashMap keySet method, and how to use it. The hashmap keySet method is convenient when you want to get the values ​​of Map in a loop, but if you use it incorrectly, it can cause problems, so be sure to remember its contents.
If you forget how to use keySet, please remember this article!