/** * Appends the specified element to the end of this list. * * @param e element to be appended to this list * @return {@code true} (as specified by {@link Collection#add}) */ publicbooleanadd(E e) { finalReentrantLocklock=this.lock; lock.lock(); try { Object[] elements = getArray(); intlen= elements.length; Object[] newElements = Arrays.copyOf(elements, len + 1); newElements[len] = e; setArray(newElements); returntrue; } finally { lock.unlock(); } }
public E get(int index) { return get(getArray(), index); }
/** * Adds a mapping from the specified key to the specified value, * replacing the previous mapping from the specified key if there * was one. */ publicvoidput(int key, E value) { inti= ContainerHelpers.binarySearch(mKeys, mSize, key);
if (i >= 0) { mValues[i] = value; } else { i = ~i;
if (i < mSize && mValues[i] == DELETED) { mKeys[i] = key; mValues[i] = value; return; }
if (mGarbage && mSize >= mKeys.length) { gc();
// Search again because indices may have changed. i = ~ContainerHelpers.binarySearch(mKeys, mSize, key); }
mKeys = GrowingArrayUtils.insert(mKeys, mSize, i, key); mValues = GrowingArrayUtils.insert(mValues, mSize, i, value); mSize++; } }
/** * Inserts an element into the array at the specified index, growing the array if there is no * more room. * * @param array The array to which to append the element. Must NOT be null. * @param currentSize The number of elements in the array. Must be less than or equal to * array.length. * @param element The element to insert. * @return the array to which the element was appended. This may be different than the given * array. */ @SuppressWarnings("unchecked") publicstatic <T> T[] insert(T[] array, int currentSize, int index, T element) { if (currentSize + 1 <= array.length) { System.arraycopy(array, index, array, index + 1, currentSize - index); array[index] = element; return array; }
/** * Given the current size of an array, returns an ideal size to which the array should grow. * This is typically double the given size, but should not be relied upon to do so in the * future. */ publicstaticintgrowSize(int currentSize) { return currentSize <= 4 ? 8 : currentSize * 2; }
/** * Returns an index for which {@link #valueAt} would return the * specified value, or a negative number if no keys map to the * specified value. * <p>Beware that this is a linear search, unlike lookups by key, * and that multiple keys can map to the same value and this will * find only one of them. * <p>Note also that unlike most collections' {@code indexOf} methods, * this method compares values using {@code ==} rather than {@code equals}. */ publicintindexOfValue(E value) { if (mGarbage) { gc(); }
for (inti=0; i < mSize; i++) { if (mValues[i] == value) { return i; } }
/** * Returns a power of two size for the given target capacity. */ staticfinalinttableSizeFor(int cap) { intn= cap - 1; n |= n >>> 1; n |= n >>> 2; n |= n >>> 4; n |= n >>> 8; n |= n >>> 16; return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1; }
As a general rule, the default load factor (.75) offers a good tradeoff between time and space costs. Higher values decrease the space overhead but increase the lookup cost(reflected in most of the operations of the HashMap class, including get and put). The expected number of entries in the map and its load factor should be taken into account when setting its initial capacity, so as to minimize the number of rehash operations. If the initial capacity is greater than the maximum number of entries divided by the load factor, no rehash operations will ever occur.