This example shows how to get all the keys of the HashMap using the keySet method. This example also shows how to iterate all keys of HashMap using the keySet method.
How to get all keys of the HashMap using the keySet method?
The keySet
method of the HashMap class returns a Set view of all the keys contained in the map object.
1 |
public Set<K> keySet() |
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
public class HashMapKeySetExample { public static void main(String[] args) { //create new HashMap HashMap<Integer, String> hMapNumbers = new HashMap<Integer, String>(); //add key-value pairs hMapNumbers.put(1, "One"); hMapNumbers.put(2, "Two"); hMapNumbers.put(3, "Three"); /* * To get all the keys from HashMap object, * use the keySet method */ Set<Integer> keys = hMapNumbers.keySet(); System.out.println(keys); } } |
Output
1 |
[1, 2, 3] |
Important Note:
The keySet
method returns a Set view of all the keys. This view is backed by the original HashMap object. It means that any changes you make in this set are reflected in the map and vice versa as given in the below example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
//create new HashMap HashMap<Integer, String> hMapNumbers = new HashMap<Integer, String>(); //add key-value pairs hMapNumbers.put(1, "One"); hMapNumbers.put(2, "Two"); hMapNumbers.put(3, "Three"); //get the key set Set<Integer> keys = hMapNumbers.keySet(); System.out.println(keys); //remove a key-value mapping from the HashMap hMapNumbers.remove(2); //the key will be removed from the key set as well System.out.println(keys); |
Output
1 2 |
[1, 2, 3] [1, 3] |
How to iterate over all keys of HashMap?
Get the set view of all the keys of the map object using the keySet
method as given above. Once you have the key set, obtain the Iterator object using the iterator
method of the set. Then use the hasNext
and next
method to iterate over all the keys of the HashMap.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
//create new HashMap HashMap<Integer, String> hMapNumbers = new HashMap<Integer, String>(); //add key-value pairs hMapNumbers.put(1, "One"); hMapNumbers.put(2, "Two"); hMapNumbers.put(3, "Three"); //get the key set Set<Integer> keys = hMapNumbers.keySet(); //get the iterator for key set Iterator<Integer> iterator = keys.iterator(); System.out.println("Iterating over all HashMap keys"); while( iterator.hasNext() ){ System.out.println( iterator.next() ); } |
Output
1 2 3 4 |
Iterating over all HashMap keys 1 2 3 |
You can also remove a key while iterating over the HashMap key set using the remove
method of the iterator. Please remember that the corresponding key-value pair will be removed from the HashMap object too as the set is backed by the original HashMap object.
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 |
//create new HashMap HashMap<Integer, String> hMapNumbers = new HashMap<Integer, String>(); //add key-value pairs hMapNumbers.put(1, "One"); hMapNumbers.put(2, "Two"); hMapNumbers.put(3, "Three"); //get the key set Set<Integer> keys = hMapNumbers.keySet(); //get the iterator for key set Iterator<Integer> iterator = keys.iterator(); Integer key = null; while( iterator.hasNext() ){ //get the key key = iterator.next(); //remove if the key equals to 2 if(key.equals(2)){ iterator.remove(); } } System.out.println("Key Set contains: " + keys); System.out.println("HashMap contains: " + hMapNumbers); |
Output
1 2 |
Key Set contains: [1, 3] HashMap contains: {1=One, 3=Three} |
Note:
The set obtained via the keySet
method does not support add and addAll operations. If the original map is modified while the iteration is in progress except through the remove
method of an iterator, the iteration result is undefined.
This example is a part of the Java HashMap tutorial with examples.
Please let me know your views in the comments section below.