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

📄 hashtable.java

📁 java源代码 请看看啊 提点宝贵的意见
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
	Entry newMap[] = new Entry[newCapacity];	modCount++;	threshold = (int)(newCapacity * loadFactor);	table = newMap;	for (int i = oldCapacity ; i-- > 0 ;) {	    for (Entry old = oldMap[i] ; old != null ; ) {		Entry e = old;		old = old.next;		int index = (e.hash & 0x7FFFFFFF) % newCapacity;		e.next = newMap[index];		newMap[index] = e;	    }	}    }    /**     * Maps the specified <code>key</code> to the specified      * <code>value</code> in this hashtable. Neither the key nor the      * value can be <code>null</code>. <p>     *     * The value can be retrieved by calling the <code>get</code> method      * with a key that is equal to the original key.      *     * @param      key     the hashtable key.     * @param      value   the value.     * @return     the previous value of the specified key in this hashtable,     *             or <code>null</code> if it did not have one.     * @exception  NullPointerException  if the key or value is     *               <code>null</code>.     * @see     Object#equals(Object)     * @see     #get(Object)     */    public synchronized Object put(Object key, Object value) {	// Make sure the value is not null	if (value == null) {	    throw new NullPointerException();	}	// Makes sure the key is not already in the hashtable.	Entry tab[] = table;	int hash = key.hashCode();	int index = (hash & 0x7FFFFFFF) % tab.length;	for (Entry e = tab[index] ; e != null ; e = e.next) {	    if ((e.hash == hash) && e.key.equals(key)) {		Object old = e.value;		e.value = value;		return old;	    }	}	modCount++;	if (count >= threshold) {	    // Rehash the table if the threshold is exceeded	    rehash();            tab = table;            index = (hash & 0x7FFFFFFF) % tab.length;	} 	// Creates the new entry.	Entry e = new Entry(hash, key, value, tab[index]);	tab[index] = e;	count++;	return null;    }    /**     * Removes the key (and its corresponding value) from this      * hashtable. This method does nothing if the key is not in the hashtable.     *     * @param   key   the key that needs to be removed.     * @return  the value to which the key had been mapped in this hashtable,     *          or <code>null</code> if the key did not have a mapping.     * @throws  NullPointerException  if the key is <code>null</code>.     */    public synchronized Object remove(Object key) {	Entry tab[] = table;	int hash = key.hashCode();	int index = (hash & 0x7FFFFFFF) % tab.length;	for (Entry e = tab[index], prev = null ; e != null ; prev = e, e = e.next) {	    if ((e.hash == hash) && e.key.equals(key)) {		modCount++;		if (prev != null) {		    prev.next = e.next;		} else {		    tab[index] = e.next;		}		count--;		Object oldValue = e.value;		e.value = null;		return oldValue;	    }	}	return null;    }    /**     * Copies all of the mappings from the specified Map to this Hashtable     * These mappings will replace any mappings that this Hashtable had for any     * of the keys currently in the specified Map.      *     * @param t Mappings to be stored in this map.     * @throws NullPointerException if the specified map is null.     * @since 1.2     */    public synchronized void putAll(Map t) {	Iterator i = t.entrySet().iterator();	while (i.hasNext()) {	    Map.Entry e = (Map.Entry) i.next();	    put(e.getKey(), e.getValue());	}    }    /**     * Clears this hashtable so that it contains no keys.      */    public synchronized void clear() {	Entry tab[] = table;	modCount++;	for (int index = tab.length; --index >= 0; )	    tab[index] = null;	count = 0;    }    /**     * Creates a shallow copy of this hashtable. All the structure of the      * hashtable itself is copied, but the keys and values are not cloned.      * This is a relatively expensive operation.     *     * @return  a clone of the hashtable.     */    public synchronized Object clone() {	try { 	    Hashtable t = (Hashtable)super.clone();	    t.table = new Entry[table.length];	    for (int i = table.length ; i-- > 0 ; ) {		t.table[i] = (table[i] != null) 		    ? (Entry)table[i].clone() : null;	    }	    t.keySet = null;	    t.entrySet = null;            t.values = null;	    t.modCount = 0;	    return t;	} catch (CloneNotSupportedException e) { 	    // this shouldn't happen, since we are Cloneable	    throw new InternalError();	}    }    /**     * Returns a string representation of this <tt>Hashtable</tt> object      * in the form of a set of entries, enclosed in braces and separated      * by the ASCII characters "<tt>,&nbsp;</tt>" (comma and space). Each      * entry is rendered as the key, an equals sign <tt>=</tt>, and the      * associated element, where the <tt>toString</tt> method is used to      * convert the key and element to strings. <p>Overrides to      * <tt>toString</tt> method of <tt>Object</tt>.     *     * @return  a string representation of this hashtable.     */    public synchronized String toString() {	int max = size() - 1;	StringBuffer buf = new StringBuffer();	Iterator it = entrySet().iterator();	buf.append("{");	for (int i = 0; i <= max; i++) {	    Map.Entry e = (Map.Entry) (it.next());            Object key = e.getKey();            Object value = e.getValue();            buf.append((key   == this ? "(this Map)" : key) + "=" +                        (value == this ? "(this Map)" : value));	    if (i < max)		buf.append(", ");	}	buf.append("}");	return buf.toString();    }    private Enumeration getEnumeration(int type) {	if (count == 0) {	    return emptyEnumerator;	} else {	    return new Enumerator(type, false);	}    }    private Iterator getIterator(int type) {	if (count == 0) {	    return emptyIterator;	} else {	    return new Enumerator(type, true);	}    }    // Views    /**     * Each of these fields are initialized to contain an instance of the     * appropriate view the first time this view is requested.  The views are     * stateless, so there's no reason to create more than one of each.     */    private transient volatile Set keySet = null;    private transient volatile Set entrySet = null;    private transient volatile Collection values = null;    /**     * Returns a Set view of the keys contained in this Hashtable.  The Set     * is backed by the Hashtable, so changes to the Hashtable are reflected     * in the Set, and vice-versa.  The Set supports element removal     * (which removes the corresponding entry from the Hashtable), but not     * element addition.     *     * @return a set view of the keys contained in this map.     * @since 1.2     */    public Set keySet() {	if (keySet == null)	    keySet = Collections.synchronizedSet(new KeySet(), this);	return keySet;    }    private class KeySet extends AbstractSet {        public Iterator iterator() {	    return getIterator(KEYS);        }        public int size() {            return count;        }        public boolean contains(Object o) {            return containsKey(o);        }        public boolean remove(Object o) {            return Hashtable.this.remove(o) != null;        }        public void clear() {            Hashtable.this.clear();        }    }    /**     * Returns a Set view of the entries contained in this Hashtable.     * Each element in this collection is a Map.Entry.  The Set is     * backed by the Hashtable, so changes to the Hashtable are reflected in     * the Set, and vice-versa.  The Set supports element removal     * (which removes the corresponding entry from the Hashtable),     * but not element addition.     *     * @return a set view of the mappings contained in this map.     * @see   Map.Entry     * @since 1.2     */    public Set entrySet() {	if (entrySet==null)	    entrySet = Collections.synchronizedSet(new EntrySet(), this);	return entrySet;    }    private class EntrySet extends AbstractSet {        public Iterator iterator() {	    return getIterator(ENTRIES);        }        public boolean contains(Object o) {            if (!(o instanceof Map.Entry))                return false;            Map.Entry entry = (Map.Entry)o;            Object key = entry.getKey();            Entry tab[] = table;            int hash = key.hashCode();            int index = (hash & 0x7FFFFFFF) % tab.length;            for (Entry e = tab[index]; e != null; e = e.next)                if (e.hash==hash && e.equals(entry))                    return true;            return false;        }        public boolean remove(Object o) {            if (!(o instanceof Map.Entry))                return false;            Map.Entry entry = (Map.Entry)o;            Object key = entry.getKey();            Entry tab[] = table;            int hash = key.hashCode();            int index = (hash & 0x7FFFFFFF) % tab.length;            for (Entry e = tab[index], prev = null; e != null;                 prev = e, e = e.next) {                if (e.hash==hash && e.equals(entry)) {                    modCount++;                    if (prev != null)                        prev.next = e.next;                    else                        tab[index] = e.next;                    count--;                    e.value = null;                    return true;                }            }            return false;        }        public int size() {            return count;        }        public void clear() {            Hashtable.this.clear();        }    }    /**     * Returns a Collection view of the values contained in this Hashtable.     * The Collection is backed by the Hashtable, so changes to the Hashtable     * are reflected in the Collection, and vice-versa.  The Collection     * supports element removal (which removes the corresponding entry from     * the Hashtable), but not element addition.     *     * @return a collection view of the values contained in this map.     * @since 1.2     */    public Collection values() {	if (values==null)	    values = Collections.synchronizedCollection(new ValueCollection(),                                                        this);        return values;    }    private class ValueCollection extends AbstractCollection {        public Iterator iterator() {	    return getIterator(VALUES);        }        public int size() {            return count;        }        public boolean contains(Object o) {            return containsValue(o);        }        public void clear() {            Hashtable.this.clear();        }    }    // Comparison and hashing    /**

⌨️ 快捷键说明

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