Виртуальная песочница (тм)

Wednesday, January 27, 2016

Hashtable and nulls

Если коротко, то Hashtable не поддерживает null'ы ни в каком виде:

import java.util.Hashtable;
import java.util.Properties;
public class HashtableTest {
public static void main(String[] args) {
Properties p = new Properties();
if (p instanceof Hashtable) {
System.out.println("java.util.Properties is a subclass of java.util.Hashtable");
}
try {
p.put(null, "value");
} catch (NullPointerException npe) {
System.out.println("Hashtable does NOT support addition of null KEYs!");
}
try {
p.put("key", null);
} catch (NullPointerException npe) {
System.out.println("Hashtable does NOT support addition of null VALUEs!");
}
try {
if (!p.contains(null)) {
System.out.println("Does not contain null.");
}
} catch (NullPointerException npe) {
System.out.println("Hashtable does NOT support check for null keys!");
}
}
}
/* OUTPUT:
java.util.Properties is a subclass of java.util.Hashtable
Hashtable does NOT support addition of null KEYs!
Hashtable does NOT support addition of null VALUEs!
Hashtable does NOT support check for null keys!
*/

No comments: