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

📄 weakhashmap.java

📁 java源代码 请看看啊 提点宝贵的意见
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
    public boolean isEmpty() {        return size() == 0;    }    /**     * Returns the value to which the specified key is mapped in this weak     * hash map, or <tt>null</tt> if the map contains no mapping for     * this key.  A return value of <tt>null</tt> does not <i>necessarily</i>     * indicate that the map contains no mapping for the key; it is also     * possible that the map explicitly maps the key to <tt>null</tt>. The     * <tt>containsKey</tt> method may be used to distinguish these two     * cases.     *     * @param   key the key whose associated value is to be returned.     * @return  the value to which this map maps the specified key, or     *          <tt>null</tt> if the map contains no mapping for this key.     * @see #put(Object, Object)     */    public Object get(Object key) {        Object k = maskNull(key);        int h = HashMap.hash(k);        Entry[] tab = getTable();        int index = indexFor(h, tab.length);        Entry e = tab[index];         while (e != null) {            if (e.hash == h && eq(k, e.get()))                return e.value;            e = e.next;        }        return null;    }      /**     * Returns <tt>true</tt> if this map contains a mapping for the     * specified key.     *     * @param   key   The key whose presence in this map is to be tested     * @return  <tt>true</tt> if there is a mapping for <tt>key</tt>;     *          <tt>false</tt> otherwise     */    public boolean containsKey(Object key) {        return getEntry(key) != null;    }    /**     * 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 h = HashMap.hash(k);        Entry[] tab = getTable();        int index = indexFor(h, tab.length);        Entry e = tab[index];         while (e != null && !(e.hash == h && eq(k, e.get())))            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 h = HashMap.hash(k);        Entry[] tab = getTable();        int i = indexFor(h, tab.length);        for (Entry e = tab[i]; e != null; e = e.next) {            if (h == e.hash && eq(k, e.get())) {                Object oldValue = e.value;                if (value != oldValue)                    e.value = value;                return oldValue;            }        }        modCount++;        tab[i] = new Entry(k, value, queue, h, tab[i]);        if (++size >= threshold)             resize(tab.length * 2);        return null;    }      /**     * 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 = getTable();        int oldCapacity = oldTable.length;        if (oldCapacity == MAXIMUM_CAPACITY) {            threshold = Integer.MAX_VALUE;            return;        }        Entry[] newTable = new Entry[newCapacity];        transfer(oldTable, newTable);        table = newTable;        /*         * If ignoring null elements and processing ref queue caused massive         * shrinkage, then restore old table.  This should be rare, but avoids         * unbounded expansion of garbage-filled tables.         */        if (size >= threshold / 2) {            threshold = (int)(newCapacity * loadFactor);        } else {            expungeStaleEntries();            transfer(newTable, oldTable);            table = oldTable;        }    }    /** Transfer all entries from src to dest tables */    private void transfer(Entry[] src, Entry[] dest) {        for (int j = 0; j < src.length; ++j) {            Entry e = src[j];            src[j] = null;            while (e != null) {                Entry next = e.next;                Object key = e.get();                if (key == null) {                    e.next = null;  // Help GC                    e.value = null; //  "   "                    size--;                } else {                    int i = indexFor(e.hash, dest.length);                      e.next = dest[i];                    dest[i] = e;                }                e = next;            }        }    }    /**     * 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.<p>     *     * @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) {        Object k = maskNull(key);        int h = HashMap.hash(k);        Entry[] tab = getTable();        int i = indexFor(h, tab.length);        Entry prev = tab[i];        Entry e = prev;        while (e != null) {            Entry next = e.next;            if (h == e.hash && eq(k, e.get())) {                modCount++;                size--;                if (prev == e)                     tab[i] = next;                else                    prev.next = next;                return e.value;            }            prev = e;            e = next;        }        return null;    }    /** Special version of remove needed by Entry set */    Entry removeMapping(Object o) {        if (!(o instanceof Map.Entry))            return null;        Entry[] tab = getTable();        Map.Entry entry = (Map.Entry)o;        Object k = maskNull(entry.getKey());        int h = HashMap.hash(k);        int i = indexFor(h, tab.length);        Entry prev = tab[i];        Entry e = prev;        while (e != null) {            Entry next = e.next;            if (h == e.hash && e.equals(entry)) {                modCount++;                size--;                if (prev == e)                     tab[i] = next;                else                    prev.next = next;                return e;            }            prev = e;            e = next;        }           return null;    }    /**     * Removes all mappings from this map.     */    public void clear() {        // clear out ref queue. We don't need to expunge entries        // since table is getting cleared.        while (queue.poll() != null)            ;        modCount++;        Entry tab[] = table;        for (int i = 0; i < tab.length; ++i)             tab[i] = null;        size = 0;        // Allocation of array may have caused GC, which may have caused        // additional entries to go stale.  Removing these entries from the        // reference queue will make them eligible for reclamation.        while (queue.poll() != null)            ;   }    /**     * 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[] = getTable();        for (int i = tab.length ; i-- > 0 ;)            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[] = getTable();        for (int i = tab.length ; i-- > 0 ;)            for (Entry e = tab[i] ; e != null ; e = e.next)                if (e.value==null)                    return true;	return false;    }    /**     * The entries in this hash table extend WeakReference, using its main ref     * field as the key. 

⌨️ 快捷键说明

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