ArrayList+HashSet实现HashMap

直接上代码:

package Test;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;

/**
 *  Hash-based Map
 */
public class HAMap implements Iterable {

    /**
     * Represents a key-value pair.
     */
    private class Entry {
        K key;
        V value;

        Entry(K k, V v) {
            key = k;
            value = v;
        }
    }

    private static final int DEFAULT_CAPACITY = 16;
    private static final double DEFAULT_LOAD_FACTOR = 1.5;

    private ArrayList> buckets;    //主要存储结构
    private HashSet keySet;                     //存储键
    private int numBuckets;                        //hashmap主存储结构大小,用于计算负载比例
    private int numEntries;                       //.size()
    private final double loadFactor;                //负载比例,numEntries/numBuckets大于该值时buckets扩容

    /**
     * @return a set of the keys contained in this map.
     */
    public HashSet keySet() {
        return keySet;
    }

    /**
     * @return the number of entries in this map.
     */
    public int size() {
        return numEntries;
    }

    /**
     * @return the number of buckets in this map.
     */
    public int getNumBuckets() {
        return numBuckets;
    }

    /*
     ***************************
     * DO NOT MODIFY CODE ABOVE
     ***************************
     */


    /*
     ***** HELPER METHODS START *****
     */

    
	// INCLUDE your helper methods in EACH of your submissions that use them
	
	
	
	

    /*
     ***** HELPER METHODS END *****
     */


    // EXERCISE 15.2 CONSTRUCTORS

    public HAMap(int initialCapacity, double loadFactor) {
    	if(initialCapacity<0) {
    		throw new IllegalArgumentException("Illegal initial capacity: "+initialCapacity);
    	}
		this.loadFactor = loadFactor;
		this.keySet = new HashSet<>();
		this.buckets = new ArrayList<>(initialCapacity);
		for(int i=0;i(initialCapacity));
		}
		this.numBuckets = initialCapacity;
		this.numEntries = 0;
    }
    public HAMap() {
		this(DEFAULT_CAPACITY,DEFAULT_LOAD_FACTOR);
    }

    public HAMap(int initialCapacity) {
		this(initialCapacity,DEFAULT_LOAD_FACTOR);
    }


    // EXERCISE 15.3 CLEAR

    /**
     * Removes all of the entries from this map.
     */
    public void clear() {
    	int size = this.buckets.size();
    	this.buckets.clear();
    	for(int i=0;i());
    	}
    	this.keySet.clear();
    	this.numEntries = 0;
    }


    // EXERCISE 15.4 CONTAINS KEY and ITERATOR

    /**
     * @param key to be checked
     * @return true iff this map contains an entry with the specified key
     */
    public boolean containsKey(K key) {
		return this.keySet.contains(key);
    }

    /**
     * @return an Iterator that iterates over the stored keys
     */
    @Override
    public Iterator iterator() {
		return this.keySet.iterator();
    }


    // ASSIGNMENT 15.1 GET

    /**
     * @param key of the value to be returned
     * @return the value to which the specified key is mapped
     *         null if this map contains no entries of the key
     */
    public V get(K key) {
		for(int i=0;i list = this.buckets.get(i);
			for(Entry e:list) {
				if(e.key.equals(key)) return e.value;
			}
		}
		return null;
    }


    // ASSIGNMENT 15.2 PUT

    /**
     * Associates the specified value with the specified key in this map.
     * If the map previously contained an entry with that key, the old value is replaced.
     * The key is not null.
     * @param key of the entry to be added
     * @param value of the entry to be added
     */
    public void put(K key, V value) {
		if((this.numEntries/this.numBuckets)>this.loadFactor) {
			ArrayList> newBuckets = new ArrayList<>(this.buckets.size()*2);
			for(int i=0;i());
			this.numBuckets*=2;
		}
		if(key==null) return;
		for(int i=0;i list = this.buckets.get(i);
			for(Entry e:list) {
				if(e.key.equals(key)) {
					e.value = value;
					return;
				}
			}
		}
		int h = key.hashCode();
		int hash = h ^ (h >>> 7) ^ (h >>> 4);
		int index = hash & (this.buckets.size()-1);
		this.buckets.get(index).add(new Entry(key, value));
		this.keySet.add(key);
		this.numEntries+=1;
    }
	
    // ASSIGNMENT 15.3 REMOVE

    /**
     * Removes the entry for the specified key only if it is
     * currently mapped to the specified value.
     * @param key of the entry to be removed
     * @param value of the entry to be removed
     * @return the value if entry found,
     *         null otherwise
     */
    public V remove(K key, V value) {
    	if(key==null) return null;
    	V v2 = null;
    	int dellist = -1;
    	int delIndex = -1; 
    	for(int i=0;i list = this.buckets.get(i);
			for(int j=0;j list = this.buckets.get(i);
    			for(Entry e:list) {
    				if(e.key.equals(key)) {
    					delKey = true; 
    					break;
    				}
    			}
    		}
    		if(!delKey) this.keySet.remove(key);
    		return v2;
    	}
		return null;
    }
}

 

你可能感兴趣的:(Java,数据结构)