Java Utility Library

Java IdentityHashMap - values() Method



The java.util.IdentityHashMap.values() method returns a 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

public Collection<V> values()

Here, V is the type of value maintained by the container.


Parameters

No parameter is required.

Return Value

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

Exception

NA

Example:

In the example below, the java.util.IdentityHashMap.values() method returns a collection view of the values contained in the given identity hash map.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a identity hash map
    IdentityHashMap<Integer, String> MyMap = new IdentityHashMap<Integer, String>();

    //populating the map
    MyMap.put(101, "John");
    MyMap.put(102, "Marry");
    MyMap.put(103, "Kim");
    MyMap.put(104, "Jo");
    MyMap.put(105, "Sam");

    //printing the content of the map
    System.out.println("MyMap contains: " + MyMap);

    //creating a Collection view
    Collection<String> ValueView = MyMap.values();

    //printing the Collection view
    System.out.println("ValueView contains: " + ValueView);
  }
}

The output of the above code will be:

MyMap contains: {101=John, 105=Sam, 103=Kim, 102=Marry, 104=Jo}
ValueView contains: [John, Sam, Kim, Marry, Jo]

❮ Java.util - IdentityHashMap