The following program uses hashcode to reach indexes
// Map subscripts can be any objects.
import java.io.*;
import java .util.*;
public class nouns {
public static void yaz(String a[]){
int i;
System.out.println("\n\nKeys of Map : ");
for(i=0; i<26;i++){
System.out.print(a[i]+" , ");
}
System.out.println();
}
public static void main(String[] args) throws
IOException {
String[]
key={"sign","close","insist","prefer","exist","assemble",
"deny","try","store","exclude","persuade","improve","acquire",
"seize","obey","emphasize","differ","recover","prosper","propose",
"shrink","break","explode","encourage","advertise","produce"};
String[]
noun={"signage","closeness","insistence","preference",
"existence","assemblage","denial","trial","storage","exclusion",
"persuasion","improvement","acquisition","seizure","obedience",
"emphasis","difference","recovery","prosperity","proposal",
"shrinkage","breakage","explosion","encouragement",
"advertisement","producer"};
Map table=new HashMap();
int i;
for(i=0; i<26;i++){
table.put(key[i],noun[i]);
}
BufferedReader in=new BufferedReader(
new InputStreamReader(System.in));
for(;;) {
yaz(key);
System.out.print("\nEnter a verb to make it into noun
: ");
String verb=in.readLine();
if (verb==null) break;
Object o=table.get(verb);
System.out.println("\n\n\t" +verb+" --> "+o);
}
}
}
--- abhay <[EMAIL PROTECTED]> wrote:
> Hello,
>
> Just a small doubt. Has anybody ever used hashCode()
> ... the
> java.lang.Object provides ?
> To be honest i dont know how to use this in our
> applications .
> (Can anybody suggest any way to use hashCode() ? )
>
> The API says ...
>
> "As much as is reasonably practical, the hashCode
> method defined by
> class Object does return distinct integers for
> distinct objects. (This
> is typically implemented by converting the internal
> address of the
> object into an integer, but this implementation
> technique is not
> required by the JavaTM programming language.) "
>
> So can we safely assume that its the address of the
> object in memory ?
> (a pointer in C or C++)
> So if java a developer need not be concerned about
> pointers, what is
> this method doing here ?
>
> What is more striking is that the API says ..
> "If two objects are equal according to the
> equals(Object) method, then
> calling the hashCode method on each of the two
> objects must produce the
> same integer result"
>
> This means if you have a two objects with equal
> values inside (for
> instance ..
> String str1 = new String("Abhay");
> String str2 = new String("Abhay");
>
> and if hashCode() returns the address then does it
> means that these 2
> objects are physically one and
> the same in the memory ? I dont get it ...
>
> Please share your knowledge..
>
> Thanks.
>
> regards
> abhay kulkarni
>
>
>
> ____________________________________________________
> To change your JDJList options, please visit:
> http://www.sys-con.com/java/list.cfm
>
> Be respectful! Clean up your posts before replying
> ____________________________________________________
>
__________________________________________________
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
http://mailplus.yahoo.com
____________________________________________________
To change your JDJList options, please visit:
http://www.sys-con.com/java/list.cfm
Be respectful! Clean up your posts before replying
____________________________________________________