Write a Java program to get a collection view of the values contained in this map


The program creates a HashMap hash_map with integer keys and string values and adds some key-value pairs to it. It then retrieves the collection view of the values in the HashMap using the values() method and prints it to the console using System.out.println(). The values() method returns a Collection view of the values in the HashMap, which is printed as a string representation of a List of strings.

Source Code

import java.util.*;  
public class Get_Collection
{  
	public static void main(String args[])
	{  
		HashMap<Integer,String> hash_map = new HashMap<Integer,String>();  
		hash_map.put(1, "Blue");
		hash_map.put(2, "Green");
		hash_map.put(3, "Pink");
		hash_map.put(4, "White");
		hash_map.put(5, "Red");
		hash_map.put(6, "Black");
		hash_map.put(7, "Orange");
 
		// Checking collection
		System.out.println("Collection View is : "+ hash_map.values());
	}
}

Output

Collection View is : [Blue, Green, Pink, White, Red, Black, Orange]