📄 abstracthashedmap.java
字号:
} else { while ( newCapacity < proposedCapacity ) { newCapacity <<= 1; // multiply by two } if ( newCapacity > MAXIMUM_CAPACITY ) { newCapacity = MAXIMUM_CAPACITY; } } return newCapacity; } /** * Calculates the new threshold of the map, where it will be resized. This * implementation uses the load factor. * * @param newCapacity * the new capacity * @param factor * the load factor * @return the new resize threshold */ protected int calculateThreshold(int newCapacity, float factor) { return (int) (newCapacity * factor); } // ----------------------------------------------------------------------- /** * Gets the <code>next</code> field from a <code>HashEntry</code>. Used * in subclasses that have no visibility of the field. * * @param entry * the entry to query, must not be null * @return the <code>next</code> field of the entry * @throws NullPointerException * if the entry is null * @since Commons Collections 3.1 */ protected HashEntry entryNext(HashEntry entry) { return entry.next; } /** * Gets the <code>hashCode</code> field from a <code>HashEntry</code>. * Used in subclasses that have no visibility of the field. * * @param entry * the entry to query, must not be null * @return the <code>hashCode</code> field of the entry * @throws NullPointerException * if the entry is null * @since Commons Collections 3.1 */ protected int entryHashCode(HashEntry entry) { return entry.hashCode; } /** * Gets the <code>key</code> field from a <code>HashEntry</code>. Used * in subclasses that have no visibility of the field. * * @param entry * the entry to query, must not be null * @return the <code>key</code> field of the entry * @throws NullPointerException * if the entry is null * @since Commons Collections 3.1 */ protected Object entryKey(HashEntry entry) { return entry.key; } /** * Gets the <code>value</code> field from a <code>HashEntry</code>. * Used in subclasses that have no visibility of the field. * * @param entry * the entry to query, must not be null * @return the <code>value</code> field of the entry * @throws NullPointerException * if the entry is null * @since Commons Collections 3.1 */ protected Object entryValue(HashEntry entry) { return entry.value; } // ----------------------------------------------------------------------- /** * Gets an iterator over the map. Changes made to the iterator affect this * map. * <p> * A MapIterator returns the keys in the map. It also provides convenient * methods to get the key and value, and set the value. It avoids the need * to create an entrySet/keySet/values object. It also avoids creating the * Map.Entry object. * * @return the map iterator */ private MapIterator mapIterator() { if ( size == 0 ) { return EmptyMapIterator.INSTANCE; } return new HashMapIterator( this ); } /** * MapIterator implementation. */ protected static class HashMapIterator extends HashIterator implements MapIterator { protected HashMapIterator(AbstractHashedMap parent) { super( parent ); } public Object next() { return super.nextEntry( ).getKey( ); } public Object getKey() { HashEntry current = currentEntry( ); if ( current == null ) { throw new IllegalStateException( AbstractHashedMap.GETKEY_INVALID ); } return current.getKey( ); } public Object getValue() { HashEntry current = currentEntry( ); if ( current == null ) { throw new IllegalStateException( AbstractHashedMap.GETVALUE_INVALID ); } return current.getValue( ); } public Object setValue(Object value) { HashEntry current = currentEntry( ); if ( current == null ) { throw new IllegalStateException( AbstractHashedMap.SETVALUE_INVALID ); } return current.setValue( value ); } } // ----------------------------------------------------------------------- /** * Gets the entrySet view of the map. Changes made to the view affect this * map. To simply iterate through the entries, use {@link #mapIterator()}. * * @return the entrySet view */ public Set entrySet() { if ( entrySet == null ) { entrySet = new EntrySet( this ); } return entrySet; } /** * Creates an entry set iterator. Subclasses can override this to return * iterators with different properties. * * @return the entrySet iterator */ protected Iterator createEntrySetIterator() { if ( size( ) == 0 ) { return Collections.EMPTY_MAP.keySet( ).iterator( ); } return new EntrySetIterator( this ); } /** * EntrySet implementation. */ protected static class EntrySet extends AbstractSet { /** The parent map */ protected final AbstractHashedMap parent; protected EntrySet(AbstractHashedMap parent) { super( ); this.parent = parent; } public int size() { return parent.size( ); } public void clear() { parent.clear( ); } public boolean contains(Object entry) { if ( entry instanceof Map.Entry ) { Map.Entry e = (Map.Entry) entry; Entry match = parent.getEntry( e.getKey( ) ); return (match != null && match.equals( e )); } return false; } public boolean remove(Object obj) { if ( obj instanceof Map.Entry == false ) { return false; } if ( contains( obj ) == false ) { return false; } Map.Entry entry = (Map.Entry) obj; Object key = entry.getKey( ); parent.remove( key ); return true; } public Iterator iterator() { return parent.createEntrySetIterator( ); } } /** * EntrySet iterator. */ protected static class EntrySetIterator extends HashIterator { protected EntrySetIterator(AbstractHashedMap parent) { super( parent ); } public Object next() { return super.nextEntry( ); } } // ----------------------------------------------------------------------- /** * Gets the keySet view of the map. Changes made to the view affect this * map. To simply iterate through the keys, use {@link #mapIterator()}. * * @return the keySet view */ public Set keySet() { if ( keySet == null ) { keySet = new KeySet( this ); } return keySet; } /** * Creates a key set iterator. Subclasses can override this to return * iterators with different properties. * * @return the keySet iterator */ protected Iterator createKeySetIterator() { if ( size( ) == 0 ) { return Collections.EMPTY_MAP.keySet().iterator(); } return new KeySetIterator( this ); } /** * KeySet implementation. */ protected static class KeySet extends AbstractSet { /** The parent map */ protected final AbstractHashedMap parent; protected KeySet(AbstractHashedMap parent) { super( ); this.parent = parent; } public int size() { return parent.size( ); } public void clear() { parent.clear( ); } public boolean contains(Object key) { return parent.containsKey( key ); } public boolean remove(Object key) { boolean result = parent.containsKey( key ); parent.remove( key ); return result; } public Iterator iterator() { return parent.createKeySetIterator( ); } } /** * KeySet iterator. */ protected static class KeySetIterator extends EntrySetIterator { protected KeySetIterator(AbstractHashedMap parent) { super( parent ); } public Object next() { return super.nextEntry( ).getKey( ); } } // ----------------------------------------------------------------------- /** * Gets the values view of the map. Changes made to the view affect this * map. To simply iterate through the values, use {@link #mapIterator()}. * * @return the values view */ public Collection values() { if ( values == null ) { values = new Values( this ); } return values; } /** * Creates a values iterator. Subclasses can override this to return * iterators with different properties. * * @return the values iterator */ protected Iterator createValuesIterator() { if ( size( ) == 0 ) { return Collections.EMPTY_MAP.keySet().iterator(); } return new ValuesIterator( this ); } /** * Values implementation. */ protected static class Values extends AbstractCollection { /** The parent map */ protected final AbstractHashedMap parent; protected Values(AbstractHashedMap parent) { super( ); this.parent = parent; } public int size() { return parent.size( ); } public void clear() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -