Java Hashtable values() Example

The values() method of java.util.Hashtable class will 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

Signature

public Collection<V> values()

This 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

Example

The following example shows the usage of java.util.Hashtable.values() method.

import java.util.Hashtable;

public class ValuesMethodHashtableExample 
{
    public static void main(String args[])
    {
        // create an empty Hashtable
        Hashtable<Integer,String>  hashtable1 = new Hashtable<Integer,String>();
        
        // use put() method to put elements to the Hashtable
        hashtable1.put(1,"Element1");
        hashtable1.put(2,"Element2");
        hashtable1.put(4,"Element4");
        hashtable1.put(5,"Element5");
        hashtable1.put(3,"Element3");
        
        //use values() method to get the values of the hashtable
        System.out.println("**values of \"hashtable1\"**");
        System.out.println(hashtable1.values());
    }
}

Output

**values of "hashtable1"**
[Element5, Element4, Element3, Element2, Element1]

Leave a Reply

Your email address will not be published. Required fields are marked *