InterviewSolution
Saved Bookmarks
| 1. |
Write a program in java to get the collection view of the values present in a HashMap. |
|
Answer» We use the values() function of the HashMap to GET the collection view. //importing the required header filesimport java.util.*; public class Collection_View { public static void main(String args[]){ HashMap<String,String> hash_map = new HashMap<String,String>();//creating an empty hash map //adding key values to the hash map hash_map.put("1","Monday"); hash_map.put("2","TUESDAY"); hash_map.put("3","Wednesday"); hash_map.put("4","Thursday"); hash_map.put("5","Friday"); hash_map.put("6","SATURDAY"); hash_map.put("7","Sunday"); //printing the original hash map System.out.println("The original hash map is as follows : " + hash_map); //printing the collection view of the hash map System.out.println("The collection view is as follows : " + hash_map.values()); }}The original hash map is as follows: {1=Monday, 2=Tuesday, 3=Wednesday, 4=Thursday, 5=Friday, 6=Saturday, 7=Sunday}The collection view is as follows : [Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday] |
|