Java Collection Tutorial - Java IdentityHashMap .containsKey (Object key)








Syntax

IdentityHashMap.containsKey(Object key) has the following syntax.

public boolean containsKey(Object key)

Example

In the following code shows how to use IdentityHashMap.containsKey(Object key) method.

// www  .  ja  v a 2s .  c  om
import java.util.IdentityHashMap;
import java.util.Map;

public class Main {
  public static void main(String[] argv) throws Exception {
    Map<Object, Object> objMap = new IdentityHashMap<Object, Object>();

    Object o1 = new Integer(123);
    Object o2 = new Integer(123);
    objMap.put(o1, "first");
    objMap.put(o2, "from java2s.com");

    Object v1 = objMap.get(o1);
    System.out.println(v1);
    Object v2 = objMap.get(o2);
    System.out.println(v2);

    // check if key 3 exists
    boolean isavailable = objMap.containsKey(o1);

    System.out.println(isavailable);
  }
}

The code above generates the following result.