/** * Puts the values contained in a supplied Map into the Map starting at * the specified index. * * @param index the index in the Map to start at. * @param map the Map containing the entries to be added. * @throws IndexOutOfBoundsException if the index is out of range [0, size] */ public void putAll(int index, final Map<? extends K, ? extends V> map) { if (index < 0 || index > insertOrder.size()) { throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + insertOrder.size()); } for (final Map.Entry<? extends K, ? extends V> entry : map.entrySet()) { final K key = entry.getKey(); final boolean contains = containsKey(key); // The return value of put is null if the key did not exist OR the value was null // so it cannot be used to determine whether the key was added put(index, entry.getKey(), entry.getValue()); if (!contains) { // if no key was replaced, increment the index index++; } else { // otherwise put the next item after the currently inserted key index = indexOf(entry.getKey()) + 1; } } }