⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hashmap.java

📁 java源代码 请看看啊 提点宝贵的意见
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        int i = indexFor(hash, table.length);        Entry e = table[i];         while (e != null) {            if (e.hash == hash && eq(k, e.key))                 return true;            e = e.next;        }        return false;    }    /**     * Returns the entry associated with the specified key in the     * HashMap.  Returns null if the HashMap contains no mapping     * for this key.     */    Entry getEntry(Object key) {        Object k = maskNull(key);        int hash = hash(k);        int i = indexFor(hash, table.length);        Entry e = table[i];         while (e != null && !(e.hash == hash && eq(k, e.key)))            e = e.next;        return e;    }      /**     * Associates the specified value with the specified key in this map.     * If the map previously contained a mapping for this key, the old     * value is replaced.     *     * @param key key with which the specified value is to be associated.     * @param value value to be associated with the specified key.     * @return previous value associated with specified key, or <tt>null</tt>     *	       if there was no mapping for key.  A <tt>null</tt> return can     *	       also indicate that the HashMap previously associated     *	       <tt>null</tt> with the specified key.     */    public Object put(Object key, Object value) {        Object k = maskNull(key);        int hash = hash(k);        int i = indexFor(hash, table.length);        for (Entry e = table[i]; e != null; e = e.next) {            if (e.hash == hash && eq(k, e.key)) {                Object oldValue = e.value;                e.value = value;                e.recordAccess(this);                return oldValue;            }        }        modCount++;        addEntry(hash, k, value, i);        return null;    }    /**     * This method is used instead of put by constructors and     * pseudoconstructors (clone, readObject).  It does not resize the table,     * check for comodification, etc.  It calls createEntry rather than     * addEntry.     */    private void putForCreate(Object key, Object value) {        Object k = maskNull(key);        int hash = hash(k);        int i = indexFor(hash, table.length);        /**         * Look for preexisting entry for key.  This will never happen for         * clone or deserialize.  It will only happen for construction if the         * input Map is a sorted map whose ordering is inconsistent w/ equals.         */        for (Entry e = table[i]; e != null; e = e.next) {            if (e.hash == hash && eq(k, e.key)) {                e.value = value;                return;            }        }        createEntry(hash, k, value, i);    }    void putAllForCreate(Map m) {        for (Iterator i = m.entrySet().iterator(); i.hasNext(); ) {            Map.Entry e = (Map.Entry) i.next();            putForCreate(e.getKey(), e.getValue());        }    }    /**     * Rehashes the contents of this map into a new array with a     * larger capacity.  This method is called automatically when the     * number of keys in this map reaches its threshold.     *     * If current capacity is MAXIMUM_CAPACITY, this method does not     * resize the map, but but sets threshold to Integer.MAX_VALUE.     * This has the effect of preventing future calls.     *     * @param newCapacity the new capacity, MUST be a power of two;     *        must be greater than current capacity unless current     *        capacity is MAXIMUM_CAPACITY (in which case value     *        is irrelevant).     */    void resize(int newCapacity) {        Entry[] oldTable = table;        int oldCapacity = oldTable.length;        if (oldCapacity == MAXIMUM_CAPACITY) {            threshold = Integer.MAX_VALUE;            return;        }        Entry[] newTable = new Entry[newCapacity];        transfer(newTable);        table = newTable;        threshold = (int)(newCapacity * loadFactor);    }    /**      * Transfer all entries from current table to newTable.     */    void transfer(Entry[] newTable) {        Entry[] src = table;        int newCapacity = newTable.length;        for (int j = 0; j < src.length; j++) {            Entry e = src[j];            if (e != null) {                src[j] = null;                do {                    Entry next = e.next;                    int i = indexFor(e.hash, newCapacity);                      e.next = newTable[i];                    newTable[i] = e;                    e = next;                } while (e != null);            }        }    }    /**     * Copies all of the mappings from the specified map to this map     * These mappings will replace any mappings that     * this map had for any of the keys currently in the specified map.     *     * @param m mappings to be stored in this map.     * @throws NullPointerException if the specified map is null.     */    public void putAll(Map m) {        int numKeysToBeAdded = m.size();        if (numKeysToBeAdded == 0)            return;        /*         * Expand the map if the map if the number of mappings to be added         * is greater than or equal to threshold.  This is conservative; the         * obvious condition is (m.size() + size) >= threshold, but this         * condition could result in a map with twice the appropriate capacity,         * if the keys to be added overlap with the keys already in this map.         * By using the conservative calculation, we subject ourself         * to at most one extra resize.         */        if (numKeysToBeAdded > threshold) {            int targetCapacity = (int)(numKeysToBeAdded / loadFactor + 1);            if (targetCapacity > MAXIMUM_CAPACITY)                targetCapacity = MAXIMUM_CAPACITY;            int newCapacity = table.length;            while (newCapacity < targetCapacity)                newCapacity <<= 1;            if (newCapacity > table.length)                resize(newCapacity);        }        for (Iterator i = m.entrySet().iterator(); i.hasNext(); ) {            Map.Entry e = (Map.Entry) i.next();            put(e.getKey(), e.getValue());        }    }      /**     * Removes the mapping for this key from this map if present.     *     * @param  key key whose mapping is to be removed from the map.     * @return previous value associated with specified key, or <tt>null</tt>     *	       if there was no mapping for key.  A <tt>null</tt> return can     *	       also indicate that the map previously associated <tt>null</tt>     *	       with the specified key.     */    public Object remove(Object key) {        Entry e = removeEntryForKey(key);        return (e == null ? e : e.value);    }    /**     * Removes and returns the entry associated with the specified key     * in the HashMap.  Returns null if the HashMap contains no mapping     * for this key.     */    Entry removeEntryForKey(Object key) {        Object k = maskNull(key);        int hash = hash(k);        int i = indexFor(hash, table.length);        Entry prev = table[i];        Entry e = prev;        while (e != null) {            Entry next = e.next;            if (e.hash == hash && eq(k, e.key)) {                modCount++;                size--;                if (prev == e)                     table[i] = next;                else                    prev.next = next;                e.recordRemoval(this);                return e;            }            prev = e;            e = next;        }           return e;    }    /**     * Special version of remove for EntrySet.     */    Entry removeMapping(Object o) {        if (!(o instanceof Map.Entry))            return null;        Map.Entry entry = (Map.Entry)o;        Object k = maskNull(entry.getKey());        int hash = hash(k);        int i = indexFor(hash, table.length);        Entry prev = table[i];        Entry e = prev;        while (e != null) {            Entry next = e.next;            if (e.hash == hash && e.equals(entry)) {                modCount++;                size--;                if (prev == e)                     table[i] = next;                else                    prev.next = next;                e.recordRemoval(this);                return e;            }            prev = e;            e = next;        }           return e;    }    /**     * Removes all mappings from this map.     */    public void clear() {        modCount++;        Entry tab[] = table;        for (int i = 0; i < tab.length; i++)             tab[i] = null;        size = 0;    }    /**     * Returns <tt>true</tt> if this map maps one or more keys to the     * specified value.     *     * @param value value whose presence in this map is to be tested.     * @return <tt>true</tt> if this map maps one or more keys to the     *         specified value.     */    public boolean containsValue(Object value) {	if (value == null)             return containsNullValue();	Entry tab[] = table;        for (int i = 0; i < tab.length ; i++)            for (Entry e = tab[i] ; e != null ; e = e.next)                if (value.equals(e.value))                    return true;	return false;    }    /**     * Special-case code for containsValue with null argument     **/    private boolean containsNullValue() {	Entry tab[] = table;        for (int i = 0; i < tab.length ; i++)            for (Entry e = tab[i] ; e != null ; e = e.next)                if (e.value == null)                    return true;	return false;    }    /**     * Returns a shallow copy of this <tt>HashMap</tt> instance: the keys and     * values themselves are not cloned.     *     * @return a shallow copy of this map.     */    public Object clone() {        HashMap result = null;	try { 	    result = (HashMap)super.clone();	} catch (CloneNotSupportedException e) { 	    // assert false;	}        result.table = new Entry[table.length];        result.entrySet = null;        result.modCount = 0;        result.size = 0;        result.init();        result.putAllForCreate(this);        return result;    }    static class Entry implements Map.Entry {        final Object key;        Object value;        final int hash;        Entry next;        /**         * Create new entry.         */        Entry(int h, Object k, Object v, Entry n) {             value = v;             next = n;            key = k;            hash = h;        }        public Object getKey() {            return unmaskNull(key);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -