• 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

Why the hashtable.hashCode() returns negative numbers

 
Ranch Hand
Posts: 620
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all
Im trying to construct hashtable that contains hashtables as values and hashCode of the hash tables as the keys
The thing is that the hashCode() values are always negative numbers and they are not unique even so the values of the hashtable
Different. how can it be ? Doesn�t it suppose to be unique number? And not negative?
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, wrong on both counts.

A hash code can be any value that will fit in an "int". That includes negative numbers.

There is no guarantee that different objects will have different hash codes. The guarantee is that equal objects will have equal hash codes. It is in fact legal (but inadvisable) for a class to return the same hash code for all instances.

Basically, when implementing hashCode(), one first ensures that equal objects generate the same hash code, then tries to ensure that different objects produce a reasonable spread of hash codes. But one is constrained by a need for hashCode() to run quickly, so sometimes one compromises hashing quality for speed.
 
ben josh
Ranch Hand
Posts: 620
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So this leads me to the question how can I generate unique identifier to hashtable object
When its keys and values are fixed what I mean is when the hashtable have N keys and corresponding X values
The generated identifier will always be the same
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Meir Yan:
Im trying to construct hashtable that contains hashtables as values and hashCode of the hash tables as the keys



Why?
 
ben josh
Ranch Hand
Posts: 620
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i need unique key to the hashtable
can it be Produced from the hashtable itself based on its content?
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using hash codes for mutable objects, such as collections but also many user defined objects, is in most cases a BAAAAAD idea. If the object changes, its hash code may change, and as a result it will never be found again when retrieving it from a hash set. (The same goes for compareTo / comparators and tree sets - I've tried ).

Let's say you use the hash code as the key. Then you add something to the hash table. As a result, its hash code changes. You use that hash code, and you will not get the object you were looking for. The same holds for any other value you derive from the hash table's contents.

You need something that will never change within the lifetime of the hash table, and truth be told I couldn't think of any property it has. You might want to subclass the hash table, give it a read-only name field, then use that as the key, but I don't see any non-similar approach working.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And using a hashcode as a unique identifier is also a bad idea. It's true that two equal objects must have equal hashcodes, but it isn't true that two objects that are not equal must have different hashcodes. So there's no guarantee of uniqueness.
 
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Meir Yan:
i need unique key to the hashtable



Do you need a unique for your object? or to use in a HashMap?

If you want a unique key, why not just use a Java GUID?
The HashMap/Table, etc. manages the values for you, I don't see why you would want to do it yourself.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Meir Yan:
i need unique key to the hashtable



Why?

I'm wondering what you would use a Hashtable of Hashtables for, and whether there perhaps might be a better solution.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Meir Yan:
i need unique key to the hashtable
can it be Produced from the hashtable itself based on its content?


So, you have a Hashtable and you want to compute a unique key based on the content of the Hashtable.

That is theoretically impossible if the unique key has less bits than the bits of the data in the Hashtable together. Here's the proof:

Suppose you want to make a key with 32 bits. Then there are 2^32 possible keys. Suppose that you have some data in your Hashtable, and that data takes up more than 32 bits - for example, you have three integers, which is 96 bits.

Now you can see that the data in the Hashtable can be in 2^96 different states, and you need a unique key for each state. But your key only has 32 bits, so you can't assign a unique key for each possible state. You would need a key of at least 96 bits (the size of the data itself), if you need a unique key for every possible state.

So if the object has N bits of data then you cannot compute a unique key that is less than N bits from the data itself.
[ September 05, 2007: Message edited by: Jesper Young ]
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jesper Young:

So if the object has N bits of data then you cannot compute a unique key that is less than N bits from the data itself.



Unless you can find a way to represent the same data with less bits, of course.
 
reply
    Bookmark Topic Watch Topic
  • New Topic