public V put(K key, V value) { if (table == null || table.length == 0) { table = resize(); } Node<K, V> p; int n = table.length, i, hash = hash(key); if ((p = table[i = (n - 1) & hash]) == null) { table[i] = new Node<>(hash, key, value, null); } else { while (true) { if (p.hash == hash && Objects.equals(p.key, key)) { V oldVal = p.value; p.value = value; return oldVal; } if (p.next == null) { break; } p = p.next; } p.next = new Node<>(hash, key, value, null); } if (++size == n) { table = resize(); } return null; }
public boolean insert(T toInsert) { Node current = root; Node parent = null; while (current != null) { parent = current; if (less(toInsert, current.value)) current = current.left; else current = current.right; } current = new Node(); current.value = toInsert; if (parent == null) return true; current.parent = parent; if (less(toInsert, parent.value)) parent.left = current; else parent.right = current; return true; }