AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Java Hashtable performance for contains() method.

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> comp.lang.java.api
View previous topic :: View next topic  
Author Message
zer0frequency
Guest





PostPosted: Fri Jul 09, 2004 7:27 pm    Post subject: Java Hashtable performance for contains() method. Reply with 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,

(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





PostPosted: Fri Jul 09, 2004 7:40 pm    Post subject: Re: Java Hashtable performance for contains() method. Reply with quote



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
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> comp.lang.java.api All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.