如果这是你要问的,你可以做这样的事情:
import java.util.Hashtable;
public class Test {
public static void main(String[] args) {
Hashtable<Integer, String> table1 = new Hashtable<Integer, String>();
table1.put(0, "String");
Hashtable<Integer, Record> table2 = new Hashtable<Integer, Record>();
table2.put(0, new Record());
System.out.println("Table 1:");
someFunction(table1);
System.out.println("Table 2:");
someFunction(table2);
Hashtable<Integer, Integer> table3 = new Hashtable<>();
System.out.println("Table 3:");
someFunction(table3);
}
static void someFunction(Hashtable hash) {
if (hash != null && hash.size() > 0) {
Object o = hash.elements().nextElement();
if (o instanceof String) {
System.out.println("It's a String!");
} else if (o instanceof Record) {
System.out.println("It's a Record!");
}
} else {
System.out.println("It's an empty table and you can't find the type of non-existing elements");
}
}
}
class Record {
String Name;
String FName;
String Phone;
// and its setter and getter.
}
编辑:正如其他人所指出的,哈希表不能为空。然后你需要做一些我刚刚编辑的事情