Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials
  

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

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

                         

Facing Programming Problem?
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

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.

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

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

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

Copyright © 2008. All rights reserved.