HashMap values() in Java

values(): This method is available in java.util.HashMap class of Java.

Syntax:

Collection<V> java.util.HashMap.values()

This method returns a Collection view of the values contained in this map.

Parameters: NA

Returns: a view of the values contained in this map.

Exceptions: NA

Approach

Java

import java.util.HashMap;

public class HashMapvalues {
    public static void main(String[] args) {
        HashMap<String, Integer> hashMap =
new HashMap<String, Integer>();

        hashMap.put("Hello", 1);
        hashMap.put("World", 2);

        System.out.println(hashMap.values());

    }
}

Output:

[1, 2]


No comments:

Post a Comment