As, I know Hash table doesn't allow null. How to handle it if I want to put key pair values? Is there any other alternative in blackberry ??
Asked
Active
Viewed 359 times
2
-
5you want to keep null as key? – Ankit Apr 05 '13 at 05:17
-
You can't do that in `HashTable`. Replace the old deprecated to use `HashTable` for a `HashMap`. – Luiggi Mendoza Apr 05 '13 at 05:17
-
2@LuiggiMendoza There is no HashMap in BlackBerry JDK – Jayamohan Apr 05 '13 at 05:20
-
`Hashtable` does not support null as a key, whereas `HashMap` does. And if you are looking for `synchronized` collection then you can use `synchronizedMap`. `Collections.synchronizedMap(new HashMap<>);` – Subhrajyoti Majumder Apr 05 '13 at 05:20
-
@Jayamohan Multimap does not allow null keys because internally it uses a `Hashtable` to map keys to sets of values (Just tested). And besides, as its name implies, you can map several values to a single key, so even if it allowed null keys it is not a direct replacement for `Hashtable`. – Mister Smith Apr 05 '13 at 08:50
1 Answers
7
You can extend Hashtable with something like this
class NullKeyHashtable extends Hashtable {
private static Object NULL = new Object();
private Object nullToNull(Object key) {
return key == null ? NULL : key;
}
public Object put(Object key, Object value) {
return super.put(nullToNull(key), value);
}
public Object get(Object key) {
return super.get(nullToNull(key));
}
...
}

Evgeniy Dorofeev
- 133,369
- 30
- 199
- 275
-
2Nice one, but you would have to override `containsKey` and `remove` as well. – Mister Smith Apr 05 '13 at 08:58