• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hashcode

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Can a hashcode overridden from Object, can return other than int.

Because i can an object which has teh value that sis greater then the int can hold.No way of reducing the value of my object, because i have to check against the same object.I even tried dividing the value by some int to make it smaller value so taht it can fit in int, this case recults me in same hashcode for different objects.

String pid = "P1/000032";
String cid = "20"
public int hashCode(){
StringBuffer tempProj = new StringBuffer(50);
tempProj.append(pid.replaceAll("/","").replaceAll("T","3").replaceAll("P","2").replaceAll("C","1").replaceAll("D","4"));

long lValue = Long.parseLong(tempProj.toString())/(Integer.parseInt(cid)*99);
return (int)lValue;
}

Above code returns 10606 for pid = "P1/000032" and also "P1/000034".Help please.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not in scope of SCJP. Moving to Java in General (Intermediate)
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's perfectly OK if different objects have the same hashCode. equal() objects must have the same hashCode, but unequal objects just should have different ones. In the general case, there's no way to guarantee uniqueness, and that's just fine.
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If an object's hashcode is mainly used as in index into Hashtable's buckets, I doubt if a Hashtable could have more than 2+ billion buckets.
hashcode is a way of mapping a number (everything on a computer is a number) evenly into a smaller space.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the result of your calculation is too big, you can use the modulus operator (%) to limit it to a particular range. As noted above, this means that to unequal objects will probably have equal hashcodes, but this is okay and typically unavoidable.

Layne
[ September 11, 2005: Message edited by: Layne Lund ]
 
I found some pretty shells, some sea glass and this lovely tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic