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.
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
HashMapObject.keySet()
To use a HashMap collection, we need to create its instance using the following syntax:
HashMap<TypeOfKey, TypeOfValue> nameOfMap = new HashMap<>();
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.
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); } } }
List of keys in the map: Name: Vivek Name: Aman Name: Tapas Name: Vaibhav Name: Ansh
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.
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()); } } }
List of keys in the map: Quantity: 50 Quantity: 20 Quantity: 40 Quantity: 10 Quantity: 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()); } } }
List of keys in the map: Quantity: 50 Quantity: 20 Quantity: 40 Quantity: 10 Quantity: 30
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!