The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > java.util  [50 examples]

e337. Associating a Value with an Object

This example demonstrates how to associate a value with an arbitrary object. The technique involves saving the object and the associated value as a key/value pair in an IdentityHashMap. A HashMap cannot be used for this purpose since if two objects happen to equal via the Object.equals() method, one of the objects will not be stored.
    // Create the map
    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

 Related Examples
e331. Generating a Random Number
e332. Breaking a String into Words
e333. Creating a Custom Event
e334. Implementing a Simple Event Notifier
e335. Listing All Available Locales
e336. Setting the Default Locale

See also: Arrays    Bits    Collections    Dates    Hash Tables    Lists    Property Files    Sets    Sorted Collections    Time    Timers   


© 2002 Addison-Wesley.