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

📄 inthashtable.java

📁 iText是一个能够快速产生PDF文件的java类库。iText的java类对于那些要产生包含文本
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        int oldCapacity = table.length;
        Entry oldMap[] = table;

        int newCapacity = oldCapacity * 2 + 1;
        Entry newMap[] = new Entry[newCapacity];

        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;
            }
        }
    }

    /***
     * <p>Maps the specified <code>key</code> to the specified
     * <code>value</code> in this hashtable. The key cannot be
     * <code>null</code>. </p>
     *
     * <p>The value can be retrieved by calling the <code>get</code> method
     * with a key that is equal to the original key.</p>
     *
     * @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.
     * @throws  NullPointerException  if the key is <code>null</code>.
     * @see     #get(int)
     */
    public int put(int key, int value) {
        // Makes sure the key is not already in the hashtable.
        Entry tab[] = table;
        int hash = key;
        int index = (hash & 0x7FFFFFFF) % tab.length;
        for (Entry e = tab[index]; e != null; e = e.next) {
            if (e.hash == hash && e.key == key) {
                int old = e.value;
                e.value = value;
                return old;
            }
        }

        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 0;
    }

    /***
     * <p>Removes the key (and its corresponding value) from this
     * hashtable.</p>
     *
     * <p>This method does nothing if the key is not present in the
     * hashtable.</p>
     *
     * @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.
     */
    public int remove(int key) {
        Entry tab[] = table;
        int hash = key;
        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 == key) {
                if (prev != null) {
                    prev.next = e.next;
                } else {
                    tab[index] = e.next;
                }
                count--;
                int oldValue = e.value;
                e.value = 0;
                return oldValue;
            }
        }
        return 0;
    }

    /***
     * <p>Clears this hashtable so that it contains no keys.</p>
     */
    public synchronized void clear() {
    	Entry tab[] = table;
        for (int index = tab.length; --index >= 0;) {
            tab[index] = null;
        }
        count = 0;
	}
    
    /***
     * <p>Innerclass that acts as a datastructure to create a new entry in the
     * table.</p>
     */
    static class Entry {
        int hash;
        int key;
        int value;
        Entry next;

        /***
         * <p>Create a new entry with the given values.</p>
         *
         * @param hash The code used to hash the int with
         * @param key The key used to enter this in the table
         * @param value The value for this key
         * @param next A reference to the next entry in the table
         */
        protected Entry(int hash, int key, int value, Entry next) {
            this.hash = hash;
            this.key = key;
            this.value = value;
            this.next = next;
        }
        
        // extra methods for inner class Entry by Paulo
        public int getKey() {
        	return key;
        }
        public int getValue() {
        	return value;
        }
        protected Object clone() {
        	Entry entry = new Entry(hash, key, value, (next != null) ? (Entry)next.clone() : null);
        	return entry;
        }
    }
    
    // extra inner class by Paulo
    static class IntHashtableIterator implements Iterator {
        int index;
        Entry table[];
        Entry entry;
        
        IntHashtableIterator(Entry table[]) {
        	this.table = table;
        	this.index = table.length;
        }
        public boolean hasNext() {
        	if (entry != null) {
        		return true;
        	}
        	while (index-- > 0) {
        	    if ((entry = table[index]) != null) {
        	        return true;
        	    }
        	}
        	return false;
        }
        
        public Object next() {
            if (entry == null) {
                while ((index-- > 0) && ((entry = table[index]) == null));
            }
            if (entry != null) {
            	Entry e = entry;
            	entry = e.next;
            	return e;
            }
        	throw new NoSuchElementException("IntHashtableIterator");
        }
        public void remove() {
        	throw new UnsupportedOperationException("remove() not supported.");
        }
    }
    
// extra methods by Paulo Soares:

    public Iterator getEntryIterator() {
        return new IntHashtableIterator(table);
    }
    
    public int[] toOrderedKeys() {
    	int res[] = getKeys();
    	Arrays.sort(res);
    	return res;
    }
    
    public int[] getKeys() {
    	int res[] = new int[count];
    	int ptr = 0;
    	int index = table.length;
    	Entry entry = null;
    	while (true) {
    		if (entry == null)
    			while ((index-- > 0) && ((entry = table[index]) == null));
    		if (entry == null)
    			break;
    		Entry e = entry;
    		entry = e.next;
    		res[ptr++] = e.key;
    	}
    	return res;
    }
    
    public int getOneKey() {
    	if (count == 0)
    		return 0;
    	int index = table.length;
    	Entry entry = null;
    	while ((index-- > 0) && ((entry = table[index]) == null));
    	if (entry == null)
    		return 0;
    	return entry.key;
    }
    
    public Object clone() {
    	try {
    		IntHashtable t = (IntHashtable)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;
    		}
    		return t;
    	} catch (CloneNotSupportedException e) {
    		// this shouldn't happen, since we are Cloneable
    		throw new InternalError();
    	}
    }
}

⌨️ 快捷键说明

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