Associate a value with an object

In this section, you will learn how to associate value with an object in Java util.

Associate a value with an object


In this section, you will learn how to associate value with an object in Java util. Here, you will know how to associate the value for the separate code. Values regarding to the separate code are maintained as a record of the specific person.

This program associates a value with an object. It takes four numeric value for the hash code of the several separate object and takes four other other string value to assign for the particular object which is treated as a record of the specific object by maintaining has map.

Code Description:

Here the multiple methods and APIs had been used by the program.

This class is used to making map interface to the hash table for comparing the object key and object values. It creates a new and empty identity hash map with default size. It gets maximum default size up to 21. For using out of the default range you have to specify that.

put(Object object_name, Object object_value):
Above method helps you to associate values for the specific object in the IdentityHashMap. It takes the object name of the object and it's value.

This is the interface extends the Collection class. It does not contain duplicate items. Set implements all the general Collection methods but it ensures that no any element store twice in the set.

Above method returns the view of the set record in the created map.

Iterators process all the elements of the Collection. This is the interface which is implemented for every Collections differently.

This program has also used generics which is given below:

JDK 1.5 provides one of the several extentions to the java programming language i.e. the "generics". This allows you to abstract over types. Here, in this program the code given in the program IdentityHashMap<Integer,String> map = new IdentityHashMap<Integer,String>();
determines the new version of the program fragment of old typed program using "generics".

In this code, IdentityHashMap is a generic class that takes type parameters for identifying the retrieved value whether it is false or true. This program uses the put() method of the IdentityHashMap class which puts the two types values one is the integer value and another is the string type value. So, this program firs store the passed type parameter with the IdentityHashMap class and its constructor i.e. the <Integer, String>.

Here is the code of program:

import java.util.*;

public class AssociateValue{
  public static void main(String[] argsthrows IOException{
  String str;
  IdentityHashMap<Integer,String> map = 
  BufferedReader in = new BufferedReader(new InputStreamReader(;
  System.out.print("Enter the hash code for the first object: ");
  int a = Integer.parseInt(in.readLine());
  System.out.print("Enter the text value for this code: ");
  map.put(new Integer(a), in.readLine());
  System.out.print("Enter the hash code for the second object: ");
  int b = Integer.parseInt(in.readLine());
  System.out.print("Enter the text value for this code: ");
  map.put(new Integer(b), in.readLine());
  System.out.print("Enter the hash code for the third object: ");
  int c = Integer.parseInt(in.readLine());
  System.out.print("Enter the text value for this code: ");
  map.put(new Integer(c), in.readLine());
  System.out.print("Enter the hash code for the fourth object: ");
  int d = Integer.parseInt(in.readLine());
  System.out.print("Enter the text value for this code: ");
  map.put(new Integer(d), in.readLine());
  Set set = map.entrySet();
  Iterator it = set.iterator();
  Map.Entry me = (Map.Entry);
  System.out.print(me.getKey() ": ");
  catch(NumberFormatException ne){
  System.out.println(ne.getMessage() " is not a legal value.");

Download this example

Share on Google+Share on Google+

Associate a value with an object

Posted on: April 16, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Associate a value with an object   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image