Java Hashtable performance for contains() method.

Z

zer0frequency

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 !!!!
 
M

Mark Thornton

zer0frequency said:
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).
(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
 
A

andrewh1

First off there is no need to post to different groups. You will
antagonize many who otherwise might help you.
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...

This code works for me with one exception. You need to case the object
you get back as a MyObject.

As Mark pointed out, hashtable checks for equality using equals() and
i.equals(j) is true in this case.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,246
Members
46,839
Latest member
MartinaBur

Latest Threads

Top