 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
zer0frequency Guest
|
Posted: Fri Jul 09, 2004 7:27 pm Post subject: Java Hashtable performance for contains() method. |
|
|
Hi all,
I am working on a conversion code (from C++ to JAVA) - now in C++
there is a Rougwave Hashtable that can create hashtables with the
contains() or equals() method which does lookups based on values....
However in JAVA the hashtable looksup objects based on
Object-Reference, so the following lookup fails in java and works in
C,
(Hashtable containing an Object called MyObject with Key as a simple
object as Integer)
Java version:
Hashtable myHash = new Hashtable();
Integer i = new Integer(1);
Integer j = new Integer(1);
// i & j are same values but different objects in
memory
MyObject myObj = new MyObject();
MyObject tmpObj = null;
// Adding myObj into myHash
myHash.put(i, myObj);
// Now logically tmpObj should get the myObj ref.
tmpObj = myHash.get(j);
// But tmpObj will be null cuz i & j are different
objects...
Now to overcome this problem, I wrote my own methods, which get
Objects from hashtable based on the keyType - i.e. for the key as
Integer, I wrote hash.getUsingInt() - which extracts Enumeration from
the hash, and then compares the keys (Integer's values) to find the
match...
This works, but I am sure it makes the processing slow... (I think?)
Please help if you got any ideas to improve performance !!!!
|
|
| Back to top |
|
 |
Mark Thornton Guest
|
Posted: Fri Jul 09, 2004 7:40 pm Post subject: Re: Java Hashtable performance for contains() method. |
|
|
zer0frequency wrote:
| Quote: | Hi all,
I am working on a conversion code (from C++ to JAVA) - now in C++
there is a Rougwave Hashtable that can create hashtables with the
contains() or equals() method which does lookups based on values....
However in JAVA the hashtable looksup objects based on
Object-Reference, so the following lookup fails in java and works in
C,
|
java.util.HashMap and java.util.Hashtable both use Object.equals to test
for equality. The class java.util.IdentityHashMap uses == (i.e.
reference equality).
| Quote: | (Hashtable containing an Object called MyObject with Key as a simple
object as Integer)
Java version:
Hashtable myHash = new Hashtable();
Integer i = new Integer(1);
Integer j = new Integer(1);
// i & j are same values but different objects in
memory
MyObject myObj = new MyObject();
MyObject tmpObj = null;
// Adding myObj into myHash
myHash.put(i, myObj);
// Now logically tmpObj should get the myObj ref.
tmpObj = myHash.get(j);
// But tmpObj will be null cuz i & j are different
objects...
|
Wrong. This code should retrieve the myObj value.
Mark Thornton
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|