Hashtable values seems not probably retrieved...

I

I'm New to Java

Hi guru,

Before I actually implement hashtable and for testing purpose,
I have tried to write 2 records a hashtable with String as key and
a class object as the value. Something looks like:

record 1: key = "key1" (String) value = Obj1 (Object of some user
defined class);
record 2: key = "key2" (String) value = Obj2 (Object of some user
defined class);

However, when I use hashtable.get("key1") or hashtable.get("key2"),
it always returns me with the same object!

Quite weird to me!
Any idea?

Newbie Patrick
 
S

Sam Jervis

record 1: key = "key1" (String) value = Obj1 (Object of some user
defined class);
record 2: key = "key2" (String) value = Obj2 (Object of some user
defined class);

However, when I use hashtable.get("key1") or hashtable.get("key2"),
it always returns me with the same object!

Quite weird to me!
Any idea?

Not from what you've said. Could you show us the code that you've
written? Mine would look like:

Hashtable hashtable = new Hashtable();

hashtable.put("key1", Obj1);
hashtable.put("key2", Obj2);

System.out.println("key1 = " + hashtable.get("key1"));
System.out.println("key2 = " + hashtable.get("key2"));

Sam
 
N

Newbie Programmer

Sam Jervis said:
Not from what you've said. Could you show us the code that you've
written? Mine would look like:

Hashtable hashtable = new Hashtable();

hashtable.put("key1", Obj1);
hashtable.put("key2", Obj2);

System.out.println("key1 = " + hashtable.get("key1"));
System.out.println("key2 = " + hashtable.get("key2"));

Sam

Hi Sam,

Thanks for your reply. Below is the test program I have constructed.
The program simplies:
(1) Optionally creates a hashtable;
(2) Write 2 records to the hashtable. Key is of String type, the value
is an object of class recStruct.
(3) After the 2 records were written to the hashtable, I retrieve
values from the hashtable using the keys "key_1" and "key_2".
However, the Vector object retrieved is not what I expected.

The code is may be redundant (cos' I'm new to java). You may
just cut&paste and run it in your environment.

Here is the code:

import java.util.*;
import java.io.*;

public class Test41 implements Serializable {
final String MY_HASH = "d:/my_hash";
static Hashtable hashtable_ptr = null;

public static void main(String[] args) {
Test41 t41 = new Test41();
Vector tmp_vector = new Vector();

t41.loadFile();

// write 1st record to hashtable
tmp_vector.clear();
tmp_vector.add("vector_1a");
tmp_vector.add("vector_1b");
t41.writeData("key_1", "string_1", tmp_vector);

// write 2nd record to hashtable
tmp_vector.clear();
tmp_vector.add("vector_2a");
tmp_vector.add("vector_2b");
t41.writeData("key_2", "string_2", tmp_vector);

t41.getData("key_1");
t41.getData("key_2");

t41.writeFile();
}

void loadFile() {
try {
FileInputStream fis = new FileInputStream(MY_HASH);
ObjectInputStream ois = new ObjectInputStream(fis);
hashtable_ptr = (Hashtable) ois.readObject();
} catch (Exception e) {
// ignore exceptions
}

// if fileList is null, create a new hashtable
if (hashtable_ptr == null)
hashtable_ptr = new Hashtable();
}

void writeFile() {
try {
FileOutputStream fos = new FileOutputStream(MY_HASH);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(hashtable_ptr);
oos.close();
} catch (Exception e) {
e.printStackTrace();
}
}

void writeData(String p_key, String p_string_fld, Vector p_vector_fld) {
try {
hashtable_ptr.put(p_key, new recStruct(p_string_fld,
p_vector_fld));
} catch (Exception e) {
System.out.println("WriteData: " + e.getLocalizedMessage());
}
}

void getData(String key) {
recStruct c = new recStruct();
c = (recStruct)hashtable_ptr.get(key);

if (c == null) {
return;
}

System.out.println("For key: " + key);
System.out.println(" String field = " + c.string_fld);
System.out.println(" Vector field = " + c.vector_fld);
}

class recStruct implements Serializable {
String string_fld;
Vector vector_fld = new Vector();

recStruct() {
string_fld = null;
vector_fld.clear();
}

recStruct(String p_string_fld, Vector p_vector_fld) {
string_fld = p_string_fld;
vector_fld = p_vector_fld;
}

void clear() {
string_fld = null;
vector_fld.clear();
}
}
}

Here is the result:

For key: key_1
String field = string_1
Vector field = [vector_2a, vector_2b]
For key: key_2
String field = string_2
Vector field = [vector_2a, vector_2b]


Thanks again.
Newbie
 
R

Ryan Stewart

Newbie Programmer said:
// write 1st record to hashtable
tmp_vector.clear();
tmp_vector.add("vector_1a");
tmp_vector.add("vector_1b");
t41.writeData("key_1", "string_1", tmp_vector);

// write 2nd record to hashtable
tmp_vector.clear();
tmp_vector.add("vector_2a");
tmp_vector.add("vector_2b");
t41.writeData("key_2", "string_2", tmp_vector); ....
void writeData(String p_key, String p_string_fld, Vector p_vector_fld) {
try {
hashtable_ptr.put(p_key, new recStruct(p_string_fld,
p_vector_fld));
} catch (Exception e) {
System.out.println("WriteData: " + e.getLocalizedMessage());
}
}
You haven't written two records to the hashtable. You've created an object,
stored a reference to it (within another class) in the hashtable, then
altered the object and stored a second reference to it in the hashtable.
 
N

Newbie Programmer

Ryan Stewart said:
p_vector_fld)
You haven't written two records to the hashtable. You've created an object,
stored a reference to it (within another class) in the hashtable, then
altered the object and stored a second reference to it in the hashtable.

Yes, I did write the two records to the hashtable. I have included
a method called t41.writeFile() which consists of:

void writeFile() {
try {
FileOutputStream fos = new FileOutputStream(MY_HASH);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(hashtable_ptr);
oos.close();
} catch (Exception e) {
e.printStackTrace();
}
}

This should write records to the filesystem, right?
Any clue? Thanks again.
 
M

Murray

Yes, I did write the two records to the hashtable. I have included
a method called t41.writeFile() which consists of:

void writeFile() {
try {
FileOutputStream fos = new FileOutputStream(MY_HASH);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(hashtable_ptr);
oos.close();
} catch (Exception e) {
e.printStackTrace();
}
}

This should write records to the filesystem, right?
Any clue? Thanks again.

I think you misunderstood Ryan's point. The reason you're getting the same
object for both keys is because you're STORING the same object for both
keys.

tmp_vector.clear();
tmp_vector.add("vector_1a");
tmp_vector.add("vector_1b");
t41.writeData("key_1", "string_1", tmp_vector);

// write 2nd record to hashtable
tmp_vector.clear();
tmp_vector.add("vector_2a");
tmp_vector.add("vector_2b");
t41.writeData("key_2", "string_2", tmp_vector);

This code is adding the same Vector object to the map each time. Java
doesn't make a copy of the object at the time you add it to the map. Rather
it just stores a reference to the object. The "value" in the both entries of
your map will still be pointing at the same object that tmp_vector is
pointing at, and any change you make to tmp_vector will be reflected in both
entries of the map (because they are referencing the same object!).

If you want to store two distinct and independent vectors in your map,
instead of simply clear()ing tmp_vector after you add the first record, you
must instead create a new Vector object.
 
N

Newbie Programmer

Murray said:
I think you misunderstood Ryan's point. The reason you're getting the same
object for both keys is because you're STORING the same object for both
keys.

tmp_vector.clear();
tmp_vector.add("vector_1a");
tmp_vector.add("vector_1b");
t41.writeData("key_1", "string_1", tmp_vector);

// write 2nd record to hashtable
tmp_vector.clear();
tmp_vector.add("vector_2a");
tmp_vector.add("vector_2b");
t41.writeData("key_2", "string_2", tmp_vector);

This code is adding the same Vector object to the map each time. Java
doesn't make a copy of the object at the time you add it to the map. Rather
it just stores a reference to the object. The "value" in the both entries of
your map will still be pointing at the same object that tmp_vector is
pointing at, and any change you make to tmp_vector will be reflected in both
entries of the map (because they are referencing the same object!).

If you want to store two distinct and independent vectors in your map,
instead of simply clear()ing tmp_vector after you add the first record, you
must instead create a new Vector object.
Thanks. I was expecting the clear() will clear the vector and give
me another brand new one, because the API said:

clear()
Removes all of the elements from this Vector.

Isn't it?
 
J

Joona I Palaste

Newbie Programmer said:
Thanks. I was expecting the clear() will clear the vector and give
me another brand new one, because the API said:
clear()
Removes all of the elements from this Vector.
Isn't it?

Where do you think it says it gives you a new Vector? You get the same
old Vector back, only this time without any elements.
In fact, there is **NO WAY** that a method call, through an object
reference, can change that reference to another object. You have to
use assignments.
 
E

Eric Sosman

Newbie said:
Thanks. I was expecting the clear() will clear the vector and give
me another brand new one, because the API said:

clear()
Removes all of the elements from this Vector.

Isn't it?

Go look at all the dirty dishes in your kitchen sink.
What a mess! Time to apply the Sink.clear() method to remove
them all. Looks much better -- but did washing the dishes
somehow exchange the old sink for a new one?
 
M

Murray

Go look at all the dirty dishes in your kitchen sink.
What a mess! Time to apply the Sink.clear() method to remove
them all. Looks much better -- but did washing the dishes
somehow exchange the old sink for a new one?

Nice analogy ;-)
 

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,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top