Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
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.

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

                         

» View all related tutorials
Related Tags: c string ide class reference io references size sed get vi key value field fields this id length max preferences

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

3 comments so far (
post your own) View All Comments Latest 10 Comments:

this site is very useful for me it gives me confidence that i can do anything with roseindia.net support
regards
G.Venu Prasad

Posted by venu parasad on Wednesday, 02.13.08 @ 11:28am | #48118

what a excellent site is this. really i have know lot thru this site. i thank each n every one who made this.

thanks...
Bhuvanesh

Posted by Bhuvanesh on Thursday, 01.10.08 @ 11:08am | #44887

This site is presenting very good concepts regarding each and every topic.
I am very thanks full to all of you for giving us such a good guidence.......

Thanks a lot
Your Reader,
Varun Kumar Goel,
Gulaothi(UP)

Posted by Varun Kumar Goel on Thursday, 07.12.07 @ 14:20pm | #21135

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.