Java Utility Library

Java Hashtable - values() Method



The java.util.Hashtable.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 view of the values contained in this map.

Exception

NA

Example:

In the example below, the java.util.Hashtable.values() method returns a view of the values contained in the given Hashtable.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a hashtable
    Hashtable<Integer, String> Htable = new Hashtable<Integer, String>();

    //populating hashtable
    Htable.put(101, "John");
    Htable.put(102, "Marry");
    Htable.put(103, "Kim");
    Htable.put(104, "Jo");
    Htable.put(105, "Sam");

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

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

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

The output of the above code will be:

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

❮ Java.util - Hashtable