Java Collection Tutorial - Java IdentityHashMap.keySet()


IdentityHashMap.keySet() has the following syntax.

public Set <K> keySet()


In the following code shows how to use IdentityHashMap.keySet() method.

//  w  w  w .jav  a2s  .  c  om
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.Set;

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");

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

    // create a set view
    Set<Object> nset=objMap.keySet();
    System.out.println("Set view is: " + nset);

The code above generates the following result.