Humble apologies to all.
I didn't look closely enough at the HashMap.put() documentation. Of
course you're right; it takes two Objects. I had misinterpreted what
my 1999 Sams book said: "Any class that defines a hashCode() method
can be efficiently stored and accessed in a hash table." Since the
hashCode() methods of BitSet and String return an int, I assumed that
an int would be suitable as a key in a HashMap. I was wrong.
The Key to a hashmap is an actual key, not the hash of the key.
HashMap calls hashCode() on the key in order to partition the keys in
to easy-to-find buckets.
(Why do these classes have a hashCode() method, if it doesn't create a
unique key?)
Like whats been stated alread, hashCode() returns a "hash", which is
*likely* to be different between two objects. This lets you store them
in different locations. Think of it like sorting your clothes by
color. The color is the hash code. You know you want a red shirt, so
you first look for "reds", then you search for "shirt".
To answer why I was using a HashMap in the first place, I expect the
user to generate a large number of 12-bit BitSets. I want to do 2
things with them: de-duplicate them before adding them to a list,
then draw certain subsets from the list based on certain bits in each
BitSet being on or off. It seemed that a BitSet, with its and() and
or() methods, would support the second objective, and storing the
BitSets in a HashMap would allow easy de-duplication and speedy
searching.
A couple of things here. First, BitSets are a bit overkill for 12
bits. I suggest using a simple integer, or at least a custom wrapper
around an int. BitSets contain an array of longs, so you don't gain
any space benefit from using BitSet instead of "int". Although you
might get some clarity, depends on what your doing.
Second, you should use a Set, (not a map) to remove duplicates.
If it's true that the hashCode() method won't reliably give me a
unique key for each BitSet, then I could create my own; maybe a short
(16 bit) number could be created in which the first 12 bits are
significant. But maybe I should abandon the whole idea of hash codes
and keys, and simply put all my BitSets into a Vector, which I would
search manually using the BitSet.equals() method.
Or, put them into an ArrayList, and call List.contains() on them
Although I've created several small Java applications, I'm not a
professional Java programmer, so if I can continue using a free
environment like Xcode it would be economic. That means, at the
present, that 1.4.2 is my upper limit for Java versions.
I believe Eclipse is free, although I use IntelliJ IDEA, and it does
cost money. You can install JDK 1.5+ and use any text editor to edit
classes, although a good IDE is definitely worth a bit of money.
Thanks very much for your speedy help.
I hope this does indeed help you.
Good luck,
Daniel.