/** * Remove the element corresponding to the key, given key.hashCode() == index. * * @return If such element exists, return it. Otherwise, return null. */ protected E remove(final int index, final K key) { if (entries[index] == null) { return null; } else if (entries[index].equals(key)) { // remove the head of the linked list modification++; size--; final LinkedElement e = entries[index]; entries[index] = e.getNext(); e.setNext(null); return convert(e); } else { // head != null and key is not equal to head // search the element LinkedElement prev = entries[index]; for (LinkedElement curr = prev.getNext(); curr != null; ) { if (curr.equals(key)) { // found the element, remove it modification++; size--; prev.setNext(curr.getNext()); curr.setNext(null); return convert(curr); } else { prev = curr; curr = curr.getNext(); } } // element not found return null; } }
@Override public E get(final K key) { // validate key if (key == null) { throw new NullPointerException("key == null"); } // find element final int index = getIndex(key); for (LinkedElement e = entries[index]; e != null; e = e.getNext()) { if (e.equals(key)) { return convert(e); } } // element not found return null; }