/** * Adds a new key-value mapping into this map. * <p/> * This implementation calls <code>createEntry()</code>, <code>addEntry()</code> * and <code>checkCapacity()</code>. * It also handles changes to <code>modCount</code> and <code>size</code>. * Subclasses could override to fully control adds to the map. * * @param hashIndex the index into the data array to store at * @param hashCode the hash code of the key to add * @param key the key to add * @param value the value to add */ protected void addMapping(int hashIndex, int hashCode, K key, V value) { modCount++; HashEntry<K, V> entry = createEntry(data[hashIndex], hashCode, key, value); addEntry(entry, hashIndex); size++; checkCapacity(); }
public void clear() { parent.clear(); }
public boolean remove(Object key) { boolean result = parent.containsKey(key); parent.remove(key); return result; }
/** * Puts a key-value mapping into this map. * * @param key the key to add * @param value the value to add * @return the value previously mapped to this key, null if none */ public V put(K key, V value) { int hashCode = hash((key == null) ? NULL : key); int index = hashIndex(hashCode, data.length); HashEntry<K, V> entry = data[index]; while (entry != null) { if (entry.hashCode == hashCode && isEqualKey(key, entry.getKey())) { V oldValue = entry.getValue(); updateEntry(entry, value); return oldValue; } entry = entry.next; } addMapping(index, hashCode, key, value); return null; }
/** * Puts all the values from the specified map into this map. * <p/> * This implementation iterates around the specified map and * uses {@link #put(Object, Object)}. * * @param map the map to add * @throws NullPointerException if the map is null */ public void putAll(Map<? extends K, ? extends V> map) { int mapSize = map.size(); if (mapSize == 0) { return; } int newSize = (int) ((size + mapSize) / loadFactor + 1); ensureCapacity(calculateNewCapacity(newSize)); // Have to cast here because of compiler inference problems. for (Iterator it = map.entrySet().iterator(); it.hasNext();) { Entry<? extends K, ? extends V> entry = (Entry<? extends K, ? extends V>) it.next(); put(entry.getKey(), entry.getValue()); } }
/** * Constructs a new, empty map with the specified initial capacity and * load factor. * * @param initialCapacity the initial capacity * @param loadFactor the load factor * @throws IllegalArgumentException if the initial capacity is less than one * @throws IllegalArgumentException if the load factor is less than or equal to zero */ protected AbstractHashedMap(int initialCapacity, float loadFactor) { super(); if (initialCapacity < 1) { throw new IllegalArgumentException("Initial capacity must be greater than 0"); } if (loadFactor <= 0.0f || Float.isNaN(loadFactor)) { throw new IllegalArgumentException("Load factor must be greater than 0"); } this.loadFactor = loadFactor; this.threshold = calculateThreshold(initialCapacity, loadFactor); initialCapacity = calculateNewCapacity(initialCapacity); this.data = new HashEntry[initialCapacity]; init(); }
int capacity = in.readInt(); int size = in.readInt(); init(); data = new HashEntry[capacity]; for (int i = 0; i < size; i++) { K key = (K) in.readObject(); V value = (V) in.readObject(); put(key, value); threshold = calculateThreshold(data.length, loadFactor);
threshold = calculateThreshold(newCapacity, loadFactor); data = new HashEntry[newCapacity]; } else { int index = hashIndex(entry.hashCode, newCapacity); entry.next = newEntries[index]; newEntries[index] = entry; threshold = calculateThreshold(newCapacity, loadFactor); data = newEntries;
public Iterator<Map.Entry<K, V>> iterator() { return parent.createEntrySetIterator(); } }
public boolean contains(Object value) { return parent.containsValue(value); }
public boolean contains(Object key) { return parent.containsKey(key); }
/** * Puts a key-value mapping into this map. * * @param key the key to add * @param value the value to add * @return the value previously mapped to this key, null if none */ public V put(K key, V value) { int hashCode = hash((key == null) ? NULL : key); int index = hashIndex(hashCode, data.length); HashEntry<K, V> entry = data[index]; while (entry != null) { if (entry.hashCode == hashCode && isEqualKey(key, entry.getKey())) { V oldValue = entry.getValue(); updateEntry(entry, value); return oldValue; } entry = entry.next; } addMapping(index, hashCode, key, value); return null; }
/** * Puts all the values from the specified map into this map. * <p/> * This implementation iterates around the specified map and * uses {@link #put(Object, Object)}. * * @param map the map to add * @throws NullPointerException if the map is null */ public void putAll(Map<? extends K, ? extends V> map) { int mapSize = map.size(); if (mapSize == 0) { return; } int newSize = (int) ((size + mapSize) / loadFactor + 1); ensureCapacity(calculateNewCapacity(newSize)); // Have to cast here because of compiler inference problems. for (Iterator it = map.entrySet().iterator(); it.hasNext();) { Map.Entry<? extends K, ? extends V> entry = (Map.Entry<? extends K, ? extends V>) it.next(); put(entry.getKey(), entry.getValue()); } }
/** * Constructs a new, empty map with the specified initial capacity and * load factor. * * @param initialCapacity the initial capacity * @param loadFactor the load factor * @throws IllegalArgumentException if the initial capacity is less than one * @throws IllegalArgumentException if the load factor is less than or equal to zero */ protected AbstractHashedMap(int initialCapacity, float loadFactor) { super(); if (initialCapacity < 1) { throw new IllegalArgumentException("Initial capacity must be greater than 0"); } if (loadFactor <= 0.0f || Float.isNaN(loadFactor)) { throw new IllegalArgumentException("Load factor must be greater than 0"); } this.loadFactor = loadFactor; this.threshold = calculateThreshold(initialCapacity, loadFactor); initialCapacity = calculateNewCapacity(initialCapacity); this.data = new HashEntry[initialCapacity]; init(); }
int capacity = in.readInt(); int size = in.readInt(); init(); data = new HashEntry[capacity]; for (int i = 0; i < size; i++) { K key = (K) in.readObject(); V value = (V) in.readObject(); put(key, value); threshold = calculateThreshold(data.length, loadFactor);
threshold = calculateThreshold(newCapacity, loadFactor); data = new HashEntry[newCapacity]; } else { int index = hashIndex(entry.hashCode, newCapacity); entry.next = newEntries[index]; newEntries[index] = entry; threshold = calculateThreshold(newCapacity, loadFactor); data = newEntries;
public Iterator<Map.Entry<K, V>> iterator() { return parent.createEntrySetIterator(); } }
public boolean contains(Object value) { return parent.containsValue(value); }
public boolean contains(Object key) { return parent.containsKey(key); }
/** * Puts a key-value mapping into this map. * * @param key the key to add * @param value the value to add * @return the value previously mapped to this key, null if none */ public V put(K key, V value) { int hashCode = hash((key == null) ? NULL : key); int index = hashIndex(hashCode, data.length); HashEntry<K, V> entry = data[index]; while (entry != null) { if (entry.hashCode == hashCode && isEqualKey(key, entry.getKey())) { V oldValue = entry.getValue(); updateEntry(entry, value); return oldValue; } entry = entry.next; } addMapping(index, hashCode, key, value); return null; }