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