Map Values() Method in Java With Examples Last Updated : 10 Sep, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report Map Values() method returns the Collection view of the values contained in this map. The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa. Syntax: map.values() Parameter: This method does not take any parameter. Return value: It returns a collection view of all the values present in the map. Example 1: Java // Java program to illustrate the // use of Map.Values() Method import java.util.*; public class GfG { // Main Method public static void main(String[] args) { // Initializing a Map of type HashMap Map<Integer, String> map = new HashMap<Integer, String>(); map.put(12345, "student 1"); map.put(22345, "student 2"); map.put(323456, "student 3"); map.put(32496, "student 4"); map.put(32446, "student 5"); map.put(32456, "student 6"); System.out.println(map.values()); } } Output: [student 4, student 3, student 6, student 1, student 2, student 5]As you can see, the output of the above example is returning a collection view of values. So any change in the map will be reflected in the collection view automatically. So always take the generic of collection same as the generic of the values of the map otherwise it will give an error. Example 2: Java // Java program to illustrate the // use of Map.Values() Method import java.util.*; public class GfG { // Main Method public static void main(String[] args) { // Initializing a Map of type HashMap Map<Integer, String> map = new HashMap<Integer, String>(); map.put(12345, "student 1"); map.put(22345, "student 2"); map.put(323456, "student 3"); map.put(32496, "student 4"); map.put(32446, "student 5"); map.put(32456, "student 6"); Collection<String> collectionValues = map.values(); System.out.println("<------OutPut before modification------>\n"); for(String s: collectionValues){ System.out.println(s); } map.put(3245596, "student 7"); System.out.println("\n<------OutPut after modification------>\n"); for(String s: collectionValues){ System.out.println(s); } } } Output: <------OutPut before modification------> student 4 student 3 student 6 student 1 student 2 student 5 <------OutPut after modification------> student 4 student 3 student 6 student 1 student 2 student 7 student 5 Comment More infoAdvertise with us Next Article Map size() Method in Java With Examples A AshishVishwakarma1 Follow Improve Article Tags : Java Java-Collections Java-Functions java-map Practice Tags : JavaJava-Collections Similar Reads Map put() Method in Java with Examples The Map put() method associates the specified value with the specified key in the map. The map put() method is used to insert new key-value pairs inside a map in Java. If the map already contains the mapping for the specified key, the old value is replaced by the new specified value. Example: Java / 3 min read Map putAll() Method in Java with Examples This method is used to copy all of the mappings from the specified map to this map. Syntax: void putAll(Map m) Parameters: This method has the only argument map m, which contains key-value mappings to be copied to given map. Returns: This method returns previous value associated with the key if pres 2 min read Map remove() Method in Java with Examples The map.remove() method is used to remove the mapping for a key from this map if it is present in the map. Syntax: V remove(Object key)Parameters: This method has the only argument key, whose mapping is to be removed from the map.Returns: This method returns the value to which this map previously as 2 min read Map size() Method in Java With Examples Map size() method in Java is used to get the total number entries i.e, key-value pair. So this method is useful when you want total entries present on the map. If the map contains more than Integer.MAX_VALUE elements returnInteger.MAX_VALUE Syntax: int size(); Parameter: This method does not take an 1 min read TreeMap values() Method in Java with Examples In Java, the values() method of the TreeMap class is present inside java.util package which is used to create a collection out of the values of the map. It basically returns a Collection view of the values in the TreeMap. --> java.util package --> TreeMap class --> values() Method Syntax: T 2 min read SortedMap values() method in Java with Examples The values() method of SortedMap interface in Java is used to create a collection out of the values of the map. It basically returns a Collection view of the values in the Map. Syntax: SortedMap.values() Parameters: The method does not accept any parameters. Return Value: The method is used to retur 2 min read Like