Hashtable values seems not probably retrieved...

Discussion in 'Java' started by I'm New to Java, Jul 14, 2004.

  1. 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
    I'm New to Java, Jul 14, 2004
    #1
    1. Advertising

  2. I'm New to Java

    Sam Jervis Guest

    > 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
    Sam Jervis, Jul 14, 2004
    #2
    1. Advertising

  3. "Sam Jervis" <> wrote in message
    news:vb7Jc.46914$I%...
    > > 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


    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
    Newbie Programmer, Jul 14, 2004
    #3
  4. I'm New to Java

    Ryan Stewart Guest

    "Newbie Programmer" <> wrote in message
    news:cd31uf$1ub5$...
    > // 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.
    Ryan Stewart, Jul 14, 2004
    #4
  5. "Ryan Stewart" <> wrote in message
    news:...
    > "Newbie Programmer" <> wrote in message
    > news:cd31uf$1ub5$...
    > > // 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.
    >
    >


    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.
    Newbie Programmer, Jul 14, 2004
    #5
  6. I'm New to Java

    Murray Guest

    > 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.
    Murray, Jul 14, 2004
    #6
  7. "Murray" <> wrote in message
    news:40f54a38$0$18194$...
    > > 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.
    >
    >

    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?
    Newbie Programmer, Jul 14, 2004
    #7
  8. Newbie Programmer <> scribbled the following:
    > "Murray" <> wrote in message
    > news:40f54a38$0$18194$...
    >> > 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.
    >>

    > 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.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "War! Huh! Good God, y'all! What is it good for? We asked Mayor Quimby."
    - Kent Brockman
    Joona I Palaste, Jul 14, 2004
    #8
  9. I'm New to Java

    Eric Sosman Guest

    Newbie Programmer wrote:
    >
    > 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?

    --
    Eric Sosman, Jul 14, 2004
    #9
  10. I'm New to Java

    Murray Guest


    > 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 ;-)
    Murray, Jul 15, 2004
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. bloodsugarsuckerfish

    Hashtable in Cahe seems to have items removed

    bloodsugarsuckerfish, Mar 7, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    499
    Sergey Zyuzin
    Mar 7, 2008
  2. John Nagle
    Replies:
    0
    Views:
    205
    John Nagle
    Aug 12, 2010
  3. John Nagle
    Replies:
    2
    Views:
    297
  4. John Wilson
    Replies:
    2
    Views:
    135
    John Wilson
    Jul 16, 2003
  5. mike

    Other Values Retrieved from Select

    mike, Mar 13, 2005, in forum: Javascript
    Replies:
    4
    Views:
    75
Loading...

Share This Page