• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

hashCode() doubt

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ***
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the question?
greetings Jos
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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");
}
}
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"....).
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic