Export (0) Print
Expand All

HashMap.containsKey Method

Visual Studio 2005

Determines whether a key exists in a HashMap object.

Package: java.util

Assembly: vjslib (in vjslib.dll)


public boolean containsKey(
    java.lang.Object k);

Parameters

k

The key to check the HashMap object for.

Returns true if the HashMap object contains the specified key; false otherwise.

The following example demonstrates how to determine if a given key is present in the HashMap.

// hashmap_containskey.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", new Integer(1));
        hm.put("Two", new Integer(2));
        hm.put("Three", new Integer(3));

        // Does the key "Three" exist?
        boolean threeExists = hm.containsKey("Three");
        if (threeExists)
        {
            System.out.println("The key \"Three\" exists!");
        }
        else
        {
            System.out.println("The key \"Three\" does NOT exist!");
        }

        // Does the key "Seven" exist?
        boolean sevenExists = hm.containsKey("Seven");
        if (sevenExists)
        {
            System.out.println("The key \"Seven\" exists!");
        }
        else
        {
            System.out.println("The key \"Seven\" does NOT exist!");
        }
    }
}

/*
Output:
The key "Three" exists!
The key "Seven" does NOT exist!
*/

Community Additions

ADD
Show:
© 2014 Microsoft