Write a Java program to get all the values from a HashMap collection


First, a new HashMap collection is created with keys of type Integer and values of type String. Then, 10 key-value pairs are added to the collection using the put() method.

The code then uses a for loop to iterate over the keySet() of the HashMap collection. For each key in the set, the get() method is called to retrieve the corresponding value from the collection. The retrieved value is then printed to the console using the println() method.

Source Code

import java.util.*;
public class GetAll_Values
{
	public static void main(String[] args)
	{
		HashMap < Integer, String > stu = new HashMap < > ();
		stu.put(101, "Kim");
		stu.put(102, "Arun");
		stu.put(103, "Deepika");
		stu.put(104, "Ramesh");
		stu.put(105, "Joes");
		stu.put(106, "Abi");
		stu.put(107, "John");
		stu.put(108, "Vijay");
		stu.put(109, "Suga");
		stu.put(110, "Siva");
 
		System.out.println("All the values from a HashMap Collection ...");
		for (Integer key: stu.keySet())
		{
			System.out.println(stu.get(key));
		}
	}
}

Output

All the values from a HashMap Collection ...
Kim
Arun
Deepika
Ramesh
Joes
Abi
John
Vijay
Suga
Siva