Export (0) Print
Expand All

HashMap Class

Visual Studio 2005

Represents a unique collection of key-value pairs. The elements are sorted according to the hash value of the key, and each key can exist only once in the collection.

Package: java.util

Assembly: vjslib (in vjslib.dll)


public class java.util.HashMap
    extends java.util.AbstractMap
    implements java.util.Map, java.lang.Cloneable, java.io.Serializable

The following example demonstrates the containsKey, containsValue, entrySet, get, isEmpty, keySet, put, remove, size and values methods of the HashMap class.

// hashmap_overview.jsl

import java.util.*;

public class Program
{
    public static void main(String[] args)
    {
        // Create a HashMap with three key/value pairs.
        HashMap hm = new HashMap();
        hm.put("One", "1");
        hm.put("Two", "2a");
        hm.put("Two", "2b");
        hm.put("Three", "3");

        // Iterate over the HashMap to see what we just put in.
        Set set = hm.entrySet();
        Iterator setIter = set.iterator();
        while (setIter.hasNext())
        {
            System.out.println(setIter.next());
        }

        // Print the size of the HashMap.  Hint, it won't be 4!
        if (!hm.isEmpty())
        {
            System.out.println("The HashMap contians " +
                hm.size() + " entries.");
        }
        else
        {
            System.out.println("The HashMap is empty!");
        }

        // Print out the keys and the values.
        Set keys = hm.keySet();
        Iterator keysIter = keys.iterator();
        while (keysIter.hasNext())
        {
            System.out.println("key: " + keysIter.next());
        }

        Collection values = hm.values();
        Iterator valuesIter = values.iterator();
        while (valuesIter.hasNext())
        {
            System.out.println("value: " + valuesIter.next());
        }

        // Look for a value corresponding to a key.
        if (hm.containsKey("Two"))
        {
            Object v = hm.get("Two");
            System.out.println("The value corresponding to the " +
                "key \"Two\" is " + v.toString());
        }

        // Look for the value "2a".
        if (!hm.containsValue("2a"))
        {
            System.out.println("The value \"2a\" does not exist " +
                "because it was replaced by the value \"2b\".");
        }

        // Remove an entry from the HashMap.
        if (hm.containsKey("Three"))
        {
            Object v = hm.remove("Three");
            System.out.println("The value " + v.toString() +
                " was removed from the HashMap.");
        }
    }
}

/*
Output:
[One, 1]
[Two, 2b]
[Three, 3]
The HashMap contians 3 entries.
key: One
key: Two
key: Three
value: 1
value: 2b
value: 3
The value corresponding to the key "Two" is 2b
The value "2a" does not exist because it was replaced by the value "2b".
The value 3 was removed from the HashMap.
*/

Community Additions

ADD
Show:
© 2014 Microsoft