File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes hashCode() doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "hashCode() doubt" Watch "hashCode() doubt" New topic
Author

hashCode() doubt

vinod ns
Greenhorn

Joined: Apr 04, 2004
Posts: 2
class ValuePair
{
public int a,b;
public boolean equals(Object other)
{
try
{
ValuePair o=(ValuePair)other;
return(a==o.a && b==o.b) || a==o.b && b==o.a);
}
catch(ClassCastException e)
{
return false;
}
}
public int hashCode()
{
//implementation here.
}
}
I cant understand the logic of this question
plz help me.

*** For a non negative operand >> and >>> produce the samw result ***
j zaal
Greenhorn

Joined: Apr 01, 2004
Posts: 5
What is the question?
greetings Jos
Ajay Ajay
Greenhorn

Joined: Mar 28, 2004
Posts: 5
Hi dude,
I'll try to complete the remaining code and I'll give explanation for it.
V1 is a reference variable to Object Valuepair.
V2 is a Object class(Root for all the classes)reference variable pointing to object valuepair.
equals is a method of object class and we are overriding here to check whether the contents of both objects are equal.Whenever we r overriding equals() method,we have to implement hashcode().

class ValuePair
{
public int a,b;
public boolean equals(Object other)
{
try
{
/* Object Casting should take place because the Object class reference variable points to the Object of valuepair*/
ValuePair o=(ValuePair)other;
return(a==o.a && b==o.b) || a==o.b && b==o.a);
}
catch(ClassCastException e)
{
return false;
}
}
public int hashCode()
{
//implementation here.
}
}
public class AppClass
{
public static void main(String []args)
{
valuepair v1 = new valuepair();
Object v2 = new valuepair();
if (V1.equals(v2))
System.out.println("Both object are equal");
else
System.out.println("Both object are not equal");
}
}
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
I think the question was to implement public int hashCode() ???
Any implementation that provides a "reasonably unique" number AND will always provide the same number for identical data will do.
Many implementations will do something with prime numbers.

might work as an example (I've not tested this for being "unique enough"....).


42
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: hashCode() doubt
 
Similar Threads
Quiz
equals and hashcode
Khalid Mock - Hashcode Q
how to analyis which is correct hashcode?
hashcode()