Home Java Example Java Util Associate a value with an object

Related Tutorials


 
 

Share on Google+Share on Google+

Associate a value with an object

Advertisement
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.

IdentityHashMap():
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.

Set:
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.

IdentityHashMap.EntrySet():
Above method returns the view of the set record in the created map.

Iterator:
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:

Generics:
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.io.*;
import java.util.*;

public class AssociateValue{
  public static void main(String[] argsthrows IOException{
  try{
  String str;
  IdentityHashMap<Integer,String> map = 
new 
IdentityHashMap<Integer,String>();
  BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  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());
  System.out.println(map);
  Set set = map.entrySet();
  Iterator it = set.iterator();
  while(it.hasNext()){
  Map.Entry me = (Map.Entry)it.next();
  System.out.print(me.getKey() ": ");
  System.out.println(me.getValue());
  }
  }
  catch(NumberFormatException ne){
  System.out.println(ne.getMessage() " is not a legal value.");
  System.exit(0);
  }
  }
}

Download this example

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 16, 2007

Related Tutorials

Discuss: Associate a value with an object   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com