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,14 +387,16 @@ public class LinkedTreeMap<K extends Comparable<K>, V>
|
||||||
}
|
}
|
||||||
|
|
||||||
private TreeNode<K, V> find(K key) {
|
private TreeNode<K, V> find(K key) {
|
||||||
for (TreeNode<K, V> entry = root; entry != null; ) {
|
if (key != null) {
|
||||||
int compareVal = key.compareTo(entry.key);
|
for (TreeNode<K, V> entry = root; entry != null; ) {
|
||||||
if (compareVal < 0) {
|
int compareVal = key.compareTo(entry.key);
|
||||||
entry = entry.left;
|
if (compareVal < 0) {
|
||||||
} else if (compareVal > 0) {
|
entry = entry.left;
|
||||||
entry = entry.right;
|
} else if (compareVal > 0) {
|
||||||
} else {
|
entry = entry.right;
|
||||||
return entry;
|
} else {
|
||||||
|
return entry;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
|
|
@ -56,6 +56,24 @@ public class LinkedTreeMapTest extends TestCase {
|
||||||
assertEquals(map.size(), map.entrySet().size());
|
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 {
|
public void testSingleElement() throws Exception {
|
||||||
LinkedTreeMap<String, Integer> map = new LinkedTreeMap<String, Integer>();
|
LinkedTreeMap<String, Integer> map = new LinkedTreeMap<String, Integer>();
|
||||||
map.put("A", 1);
|
map.put("A", 1);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user