w3resource

Java: Get a collection view of the values contained in this map


12. Get Collection of Values from Map

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

Sample Solution:-

Java Code:

import java.util.*;  
public class Example12 {  
     public static void main(String args[]){  
 
  HashMap<Integer,String> hash_map= new HashMap<Integer,String>();  
    hash_map.put(1,"Red");
  hash_map.put(2,"Green");
  hash_map.put(3,"Black");
  hash_map.put(4,"White");
  hash_map.put(5,"Blue");
       
   // checking collection view of the map
   System.out.println("Collection view is: "+ hash_map.values());
 }
}

Sample Output:

Collection view is: [Red, Green, Black, White, Blue]

For more Practice: Solve these Related Problems:

  • Write a Java program to retrieve the collection of values from a map using values() and then print them.
  • Write a Java program to convert a map’s values to a list and then use Java streams to calculate the sum if they are numeric.
  • Write a Java program to filter a map’s values using streams and collect only those that satisfy a given condition.
  • Write a Java program to iterate over the collection view of a map’s values and count the frequency of a specific value.

Go to:


PREV : Get Set of Keys from Map.
NEXT : Associate Value with Key in TreeMap.

Java Code Editor:

Contribute your code and comments through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.