I have a Hashtable of type Hashtable
I've loaded several strings as keys, one of which is "ABCD"
However, later when I go to look up "ABCD", the Hashtable returns null instead of the associated object. Further the keyset contains "ABCD", but a request to containsKey("ABCD") returns false.
Is this because String objects are inherently different objects?
If so, what is the write way to store information in a Hashtable if I want to use Strings as keys?
public class Field {
private String name;
private DataType dataType;
public Field(String name, DataType dataType) {
this.name = name;
this.dataType = dataType;
}
public String getName() {
return name;
}
public DataType getDataType() {
return dataType;
}
public String toString() {
return name;
}
}
public class Record {
private Hashtable<String, Data> content;
public Record(Field[] fieldList) {
this.fieldList = fieldList;
content = new Hashtable<String, Data>();
System.out.println(fieldList.length);
for(Field f : fieldList) {
content.put(f.getName(), new Data());
}
}
public void add(String field, String s) {
// ERROR OCCURS HERE IN THIS METHOD !!!
System.out.println(field);
for(String ss : content.keySet()) {
System.out.print(" [ " + ss + " ] ");
}
System.out.println();
System.out.println(content.containsKey(field));
System.out.println(content.get(field));
content.get(field).add(s);
}
}
public class Data {
private Vector<String> lines;
private int index;
public Data() {
lines = new Vector<String>();
index = 0;
}
public void add(String s) {
System.out.println("adding");
lines.add(s);
}
public String nextLine() {
try {
return lines.elementAt(index++);
} catch (ArrayIndexOutOfBoundsException aioobe) {
return null;
}
}
}