Associating a Value with an Object with Map - Java Collection Framework

Java examples for Collection Framework:Map

Description

Associating a Value with an Object with Map

Demo Code

import java.util.IdentityHashMap;
import java.util.Map;

public class Main {
  public static void main(String[] args) throws Exception {
    Map objMap = new IdentityHashMap();

    // Add the object and value pair to the map
    Object o1 = new Integer(123);
    Object o2 = new Integer(123);
    objMap.put(o1, "first");
    objMap.put(o2, "second");

    // Retrieve the value associated with the objects
    Object v1 = objMap.get(o1); // first
    Object v2 = objMap.get(o2); // second
  }/* w  ww .j a va2  s  . c  o m*/
}

Related Tutorials