Getting Set View of Keys from HashMap in Java


To get the set view of keys from a HashMap in Java, we can use the in-built method named ‘keySet()’. Here, the HashMap is a class that is used to implement Map Interface. It stores its element in key-value pairs. The Key is an object that is used to fetch and receive value associated with it. It has access to all the methods of Map Interface, it does not have any additional methods of its own. Duplicate values are not allowed, although we can store null values and keys.

Java Program to Get Set View of Keys from HashMap

keySet() Method

It is used with the instance of HashMap collection. This method does not require any parameter and returns the set view of all keys available in the specified collection

Syntax

HashMapObject.keySet()

To use the HashMap collection, we need to create its instance using the following syntax:

Syntax

HashMap<TypeOfKey, TypeOfValue> nameOfMap = new HashMap<>();

Approach

  • The first step is to import the ‘java.util’ package. It will enable the use of HashMap class.

  • Create an instance of HashMap collection. Here, the key will be of type String and values will be of Integer type.

  • Now, append some elements to it using the built-in method ‘put()’.

  • Take a for-each loop and use the ‘keySet()’ method to iterate through keys and print them.

Example 1

The following example illustrates the use of ‘keySet()’ method with for-each loop.

import java.util.*;
public class Maps {
   public static void main(String[] args) {
      HashMap<String, Integer> workers = new HashMap<>();
      // Adding elements in the workers map
      workers.put("Vaibhav", 4000);
      workers.put("Ansh", 3000);
      workers.put("Vivek", 1500);
      workers.put("Aman", 2000);
      workers.put("Tapas", 2500);
         // printing all the keys of workers map
      System.out.println("List of keys in the map: ");
      for (String unKey : workers.keySet()) { // iterate through keys
         System.out.println("Name: " + unKey);
      }
   }
}

Output

List of keys in the map:
Name: Vivek
Name: Aman
Name: Tapas
Name: Vaibhav
Name: Ansh

Approach

  • Create an instance of HashMap collection. Here, the key and values both will be of Integer type.

  • Store the keys to a set and iterate through the key set using an iterator.

  • Now, take a while to check the available keys and print them.

Example 2

The following example illustrates the use of ‘keySet()’ method with the iterator.

import java.util.*;
public class Maps {
   public static void main(String[] args) {
      HashMap<Integer, Integer> cart = new HashMap<>();
      // Adding elements in the cart map
      cart.put(10, 400);
      cart.put(20, 300);
      cart.put(30, 150);
      cart.put(40, 200);
      cart.put(50, 250);
      // printing keys of cart map
      System.out.println("List of keys in the map: ");
      Set keys = cart.keySet(); // storing keys to the set
      Iterator itr = keys.iterator(); // iterating through keys
      while (itr.hasNext()) { // check and print keys
         System.out.println("Quantity: " + itr.next());
      }
   }
}

Output

List of keys in the map: 
Quantity: 50
Quantity: 20
Quantity: 40
Quantity: 10
Quantity: 3

Example 3

In the following example, we will get the set view without using the for-each loop and iterator

import java.util.*;
public class Maps {
   public static void main(String[] args) {
      HashMap<Integer, Integer> cart = new HashMap<>();
      // Adding elements in the cart map
      cart.put(10, 400);
      cart.put(20, 300);
      cart.put(30, 150);
      cart.put(40, 200);
      cart.put(50, 250);
      // printing keys of cart map
      System.out.println("List of keys in the map: ");
      Set keys = cart.keySet(); // storing keys to the set
      Iterator itr = keys.iterator(); // iterating through keys
      while (itr.hasNext()) { // check and print keys
         System.out.println("Quantity: " + itr.next());
      }
   }
}

Output

List of keys in the map:
Quantity: 50
Quantity: 20
Quantity: 40
Quantity: 10
Quantity: 30

Conclusion

The HashMap Class and the Map Interface are part of the Collection Framework. The collection allows the grouping of objects in a single unit. In this article, we started by defining the HashMap class and then we discussed a few Java example programs to get a set view from Java HashMap

Updated on: 21-Jul-2023

115 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements