Stop blowing up on a null key, but rather return that the element is not found.
This commit is contained in:
parent
b0531e1649
commit
3f2efac95e
@ -387,6 +387,7 @@ public class LinkedTreeMap<K extends Comparable<K>, V>
|
||||
}
|
||||
|
||||
private TreeNode<K, V> find(K key) {
|
||||
if (key != null) {
|
||||
for (TreeNode<K, V> entry = root; entry != null; ) {
|
||||
int compareVal = key.compareTo(entry.key);
|
||||
if (compareVal < 0) {
|
||||
@ -397,6 +398,7 @@ public class LinkedTreeMap<K extends Comparable<K>, V>
|
||||
return entry;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -56,6 +56,24 @@ public class LinkedTreeMapTest extends TestCase {
|
||||
assertEquals(map.size(), map.entrySet().size());
|
||||
}
|
||||
|
||||
public void testGetAndContainsNullKey() throws Exception {
|
||||
LinkedTreeMap<String, Integer> map = new LinkedTreeMap<String, Integer>();
|
||||
assertFalse(map.containsKey(null));
|
||||
assertNull(map.get(null));
|
||||
|
||||
map.put("A", 1);
|
||||
assertFalse(map.containsKey(null));
|
||||
assertNull(map.get(null));
|
||||
}
|
||||
|
||||
public void testDisallowPutForNullKeys() throws Exception {
|
||||
LinkedTreeMap<String, Integer> map = new LinkedTreeMap<String, Integer>();
|
||||
try {
|
||||
map.put(null, 1);
|
||||
fail();
|
||||
} catch (NullPointerException expected) {}
|
||||
}
|
||||
|
||||
public void testSingleElement() throws Exception {
|
||||
LinkedTreeMap<String, Integer> map = new LinkedTreeMap<String, Integer>();
|
||||
map.put("A", 1);
|
||||
|
Loading…
Reference in New Issue
Block a user