我目前正在处理一项让我在 Java 中创建 Map 类的任务,并且在使用我似乎无法修复的“put”方法时遇到了错误。本质上,当测试运行时,地图中的新节点不会被创建,我似乎无法弄清楚为什么。先感谢您!
班级:
public class MyMap<K extends Comparable<K>, V> {
private class MapNode {
private K key;
private V value;
private MapNode left;
private MapNode right;
public MapNode(K theKey, V theValue) {
key = theKey;
value = theValue;
left = null;
right = null;
}
}
private MapNode root;
public MyMap() {
root = null;
}
/**
* Associates key to value and stores mapping If key exists, replaces value
* with a new value
*
* @param key
* @param value
* @return value replaced; null if no value
*/
public V put(K key, V value) {
return put(key, value, root);
}
private V put(K key, V value, MapNode ref) {
V temp;
if (ref == null) {
ref = new MapNode(key, value);
return null;
} else {
if (ref.key.compareTo(key) == 0) {
temp = ref.value;
ref.value = value;
return temp;
} else if (key.compareTo(ref.key) < 0)
return put(key, value, ref.left);
else
return put(key, value, ref.right);
}
}
/**
* Return value to which key is mapped
*
* @param key
* @return value of key; null
*/
public V get(K key) {
return get(key, root);
}
private V get(K key, MapNode ref) {
if (ref == null) {
return null;
} else {
if (ref.key.compareTo(key) == 0)
return ref.value;
else if (key.compareTo(ref.key) < 0)
return get(key, ref.left);
else if (key.compareTo(ref.key) > 0)
return get(key, ref.right);
else
return null;
}
}
/**
* Returns true if Map already uses the key
*
* @param key
* @return true; false
*/
public boolean containsKey(K key) {
return containsKey(key, root);
}
private boolean containsKey(K key, MapNode ref) {
if (ref == null) {
return false;
} else {
if (ref.key.compareTo(key) == 0)
return true;
else if (key.compareTo(ref.key) < 0)
return containsKey(key, ref.left);
else if (key.compareTo(ref.key) > 0)
return containsKey(key, ref.right);
else
return false;
}
}
}
测试:
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class MyMapTest {
@Test
public void testMyMap(){
MyMap<String, Integer> m = new MyMap<String, Integer>();
assertFalse(m.containsKey("one"));
assertEquals(null, m.get("one"));
assertEquals(null, m.put("one", 1));
assertTrue(m.containsKey("one"));
}
}