Note that there are some explanatory texts on larger screens.

plurals
  1. POImplementing a Map in Java
    primarykey
    data
    text
    <p>I'm currently working on an assignment that has me creating a Map class in Java, and I've run into an error while working with the 'put' method that I cannot seem to fix. Essentially, when the test is run, the new Node within the Map will not be created, and I cannot seem to figure out why. Thank you in advance!</p> <p>Class:</p> <pre><code>public class MyMap&lt;K extends Comparable&lt;K&gt;, V&gt; { 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) &lt; 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) &lt; 0) return get(key, ref.left); else if (key.compareTo(ref.key) &gt; 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) &lt; 0) return containsKey(key, ref.left); else if (key.compareTo(ref.key) &gt; 0) return containsKey(key, ref.right); else return false; } } } </code></pre> <p>Test:</p> <pre><code>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&lt;String, Integer&gt; m = new MyMap&lt;String, Integer&gt;(); assertFalse(m.containsKey("one")); assertEquals(null, m.get("one")); assertEquals(null, m.put("one", 1)); assertTrue(m.containsKey("one")); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload