@Override public E put(final E element) { // validate element if (element == null) { throw new NullPointerException("Null element is not supported."); } LinkedElement e = null; try { e = (LinkedElement) element; } catch (ClassCastException ex) { throw new HadoopIllegalArgumentException( "!(element instanceof LinkedElement), element.getClass()=" + element.getClass()); } // find index final int index = getIndex(element); // remove if it already exists final E existing = remove(index, element); // insert the element to the head of the linked list modification++; size++; e.setNext(entries[index]); entries[index] = e; return existing; }
/** Print detailed information of this object. */ public void printDetails(final PrintStream out) { out.print(this + ", entries = ["); for (int i = 0; i < entries.length; i++) { if (entries[i] != null) { LinkedElement e = entries[i]; out.print("\n " + i + ": " + e); for (e = e.getNext(); e != null; e = e.getNext()) { out.print(" -> " + e); } } } out.println("\n]"); }
@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; }
private void ensureNext() { if (trackModification && modification != iterModification) { throw new ConcurrentModificationException( "modification=" + modification + " != iterModification = " + iterModification); } if (next != null) { return; } if (cur == null) { return; } next = cur.getNext(); if (next == null) { next = nextNonemptyEntry(); } }
/** * 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; } }