TwoKeyHashMappublic class TwoKeyHashMap extends AbstractMap Reductive hash with two keys |
Fields Summary |
---|
static final float | DEFAULT_LOAD_FACTOR | static final int | DEFAULT_INITIAL_SIZE | private Set | entrySet | private Collection | values | private int | size | private int | arrSize | private int | modCount | private Entry[] | arr | private float | loadFactor | int | threshold |
Constructors Summary |
---|
public TwoKeyHashMap()Constructs an empty HashMap
this(DEFAULT_INITIAL_SIZE, DEFAULT_LOAD_FACTOR);
| public TwoKeyHashMap(int initialCapacity)Constructs an empty HashMap
this(initialCapacity, DEFAULT_LOAD_FACTOR);
| public TwoKeyHashMap(int initialCapacity, float initialLoadFactor)Constructs an empty HashMap
if (initialCapacity < 0) {
throw new IllegalArgumentException("initialCapacity should be >= 0");
}
if (initialLoadFactor <= 0) {
throw new IllegalArgumentException("initialLoadFactor should be > 0");
}
loadFactor = initialLoadFactor;
if (initialCapacity == Integer.MAX_VALUE) {
initialCapacity--;
}
arrSize = initialCapacity > 0 ? initialCapacity : 1;
threshold = (int) (arrSize * loadFactor);
arr = new Entry[arrSize + 1];
|
Methods Summary |
---|
public void | clear()Clears the map
modCount++;
size = 0;
Arrays.fill(arr, 0, arr.length, null);
| public boolean | containsKey(java.lang.Object key1, java.lang.Object key2)Returns true if this map contains a mapping for the specified keys
return findEntry(key1, key2) != null;
| org.apache.harmony.luni.util.TwoKeyHashMap$Entry | createEntry(int hashCode, E key1, K key2, V value, org.apache.harmony.luni.util.TwoKeyHashMap$Entry next)Creates new entry
return new Entry<E, K, V>(hashCode, key1, key2, value, next);
| java.util.Iterator | createEntrySetIterator()Creates entries iterator
return new EntryIteratorImpl();
| java.util.Iterator | createValueCollectionIterator()Creates values iterator
return new ValueIteratorImpl();
| public java.util.Set | entrySet()Returns a collection view of the mappings
if (entrySet == null) {
entrySet = new EntrySetImpl();
}
return entrySet;
| private final org.apache.harmony.luni.util.TwoKeyHashMap$Entry | findEntry(java.lang.Object key1, java.lang.Object key2)
if (key1 == null && key2 == null) {
return arr[arrSize];
}
int hash = key1.hashCode() + key2.hashCode();
int index = (hash & 0x7fffffff) % arrSize;
Entry<E, K, V> e = arr[index];
while (e != null) {
if (hash == e.hash && key1.equals(e.getKey1()) && key2.equals(e.getKey2())) {
return e;
}
e = e.next;
}
return null;
| public V | get(java.lang.Object key1, java.lang.Object key2)Return the value by keys
Entry<E, K, V> e = findEntry(key1, key2);
if (e != null) {
return e.value;
}
return null;
| public boolean | isEmpty()Returns true if this map contains no key-value mappings
return size == 0;
| public V | put(E key1, K key2, V value)Associates the specified value with the specified keys in this map
if (key1 == null && key2 == null) {
int index = arrSize;
if (arr[index] == null) {
arr[index] = createEntry(0, null, null, value, null);
size++;
modCount++;
return null;
} else {
V oldValue = arr[index].value;
arr[index].value = value;
return oldValue;
}
}
int hash = key1.hashCode() + key2.hashCode();
int index = (hash & 0x7fffffff) % arrSize;
Entry<E, K, V> e = arr[index];
while (e != null) {
if (hash == e.hash && key1.equals(e.getKey1()) && key2.equals(e.getKey2())) {
V oldValue = e.value;
e.value = value;
return oldValue;
}
e = e.next;
}
arr[index] = createEntry(hash, key1, key2, value, arr[index]);
size++;
modCount++;
if (size > threshold) {
rehash();
}
return null;
| void | rehash()Rehash the map
int newArrSize = (arrSize + 1) * 2 + 1;
if (newArrSize < 0) {
newArrSize = Integer.MAX_VALUE - 1;
}
Entry<E, K, V>[] newArr = new Entry[newArrSize + 1];
for (int i = 0; i < arr.length - 1; i++) {
Entry<E, K, V> entry = arr[i];
while (entry != null) {
Entry<E, K, V> next = entry.next;
int newIndex = (entry.hash & 0x7fffffff) % newArrSize;
entry.next = newArr[newIndex];
newArr[newIndex] = entry;
entry = next;
}
}
newArr[newArrSize] = arr[arrSize]; // move null entry
arrSize = newArrSize;
// The maximum array size is reached, increased loadFactor
// will keep array from further growing
if (arrSize == Integer.MAX_VALUE) {
loadFactor *= 10;
}
threshold = (int) (arrSize * loadFactor);
arr = newArr;
| public V | remove(java.lang.Object key1, java.lang.Object key2)Removes the mapping for the keys
Entry<E, K, V> e = removeEntry(key1, key2);
return null != e ? e.value : null;
| private final org.apache.harmony.luni.util.TwoKeyHashMap$Entry | removeEntry(java.lang.Object key1, java.lang.Object key2)
if (key1 == null && key2 == null) {
int index = arrSize;
if (arr[index] != null) {
Entry<E, K, V> ret = arr[index];
arr[index] = null;
size--;
modCount++;
return ret;
}
return null;
}
int hash = key1.hashCode() + key2.hashCode();
int index = (hash & 0x7fffffff) % arrSize;
Entry<E, K, V> e = arr[index];
Entry<E, K, V> prev = e;
while (e != null) {
if (hash == e.hash && key1.equals(e.getKey1()) && key2.equals(e.getKey2())) {
if (prev == e) {
arr[index] = e.next;
} else {
prev.next = e.next;
}
size--;
modCount++;
return e;
}
prev = e;
e = e.next;
}
return null;
| public int | size()Returns the number of mappings
return size;
| public java.util.Collection | values()Returns a collection view of the values
if (values == null) {
values = new ValuesCollectionImpl();
}
return values;
|
|