Home >Java >javaTutorial >Get collection view of keys from HashMap in Java

Get collection view of keys from HashMap in Java

王林
王林forward
2023-09-23 14:13:021260browse

Get collection view of keys from HashMap in Java

To get a collection view of keys from a HashMap in Java, we can use the inbuilt method called "keySet()". Here, HashMap is a class used to implement the Map interface. It stores its elements in key-value pairs. Key is an object used to get and receive the value associated with it. It has access to all methods of the Map interface, it does not have any additional methods of its own. Although we can store null values ​​and keys, duplicate values ​​are not allowed.

Java program to get collection view of keys from HashMap

keySet() method

Use with instances of HashMap collections. This method does not require any parameters and returns a collection view of all available keys in the specified collection

grammar

HashMapObject.keySet()

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

grammar

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

method

  • The first step is to import the "java.util" package. It will enable the use of HashMap kind.

  • Create an instance of the HashMap collection. Here, the key is of type String and The value will be of type integer.

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

  • Adopt a for-each loop and use the "keySet()" method to iterate over the keys and Print them.

Example 1

The following example illustrates the use of the "keySet()" method with the 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

method

  • Create an instance of the HashMap collection. Here, both keys and values ​​are of type Integer.

  • Store keys into a collection and use an iterator to iterate over the key set.

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

Example 2

The following example illustrates the use of the "keySet()" method and iterator.

H3>
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 collection view without using 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

in conclusion

The HashMap class and Map interface are part of the collections framework. This collection allows objects to be grouped in a unit. In this article, we first define the HashMap class and then discuss some Java example programs to get a collection view from a Java HashMap

The above is the detailed content of Get collection view of keys from HashMap in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete