自定义一个HashMap(5)


package mycollection;

/**
 * 自定义一个HashMap
 * 实现get方法,根据键对象获得对应的值对象
 * @author 
 *
 */
public class TestHashMap4<K,V> {
	
	Node3<K,V>[] table;//位桶数组 bucket array
	int size;
//	Node3<K, V> first;
	
	public TestHashMap4() {
		table = new Node3[16];//数组长度为2的整数次幂
	}
	
	public void remove(K key){
		Node3<K,V> up;
		Node3<K, V> down;
		Node3<K, V> temp;
		
		int x = 0;
		int hash = myHash(key.hashCode(), table.length);
		temp = table[hash];
		up = temp;	
		while(temp != null){
			++x;
			down = temp.next;
			if(temp.key.equals(key)){
				if(x == 1){//当被删除元素为第一个元素时
					table[hash] = temp.next;
					break;
				}else{
					up.next = down;
					break;
				}
			}else{
				up = temp;
				temp = down;
			}
		}	
	}
	
	
	@SuppressWarnings("unchecked")
	public V get(K key) {
		int hash = myHash(key.hashCode(), table.length);
		Object value = null;
		
		if(table[hash] != null){
			Node3<K,V> temp = table[hash];
			
			while(temp != null){
				if(temp.key.equals(key)){
					value = temp.value;
					break;
				}else{
					temp = temp.next;
				}
				
			}
		}
		
		return (V)value;
	} 
	
	public void put(K key,V value) {
		
		//如果要完善,还需要考虑数组扩容的问题
		int x = 0;
		if(x >= (0.75*table.length)){
			Node3<K, V>[] newtable = new Node3[table.length<<1];
			table = newtable;
		}
		
		//定义节点对象
		Node3<K,V> newNode = new Node3<>();
		newNode.hash = myHash(key.hashCode(), table.length);
		newNode.key = key;
		newNode.value = value;
		newNode.next = null;
		
		Node3<K,V> temp = table[newNode.hash];
		
		Node3<K,V> iterLast = null;//正在遍历的最后一个元素
		boolean keyRepeat = false;//key是否重复
		
		if(temp == null){
			//此处数组元素为空,则直接将新节点放进去
			table[newNode.hash] = newNode;
			size++;
			x++;
		}else{
			//此处不为空则遍历对应链表
			while(temp != null){
				
				//判断key如果重复,覆盖
				if(temp.equals(key)){
					
					//System.out.println("重复了");
					
					keyRepeat = true;
					temp.value = value; //只是覆盖value值即可。 其他的值(hash,key,next)保持不变
					break;	
					
				}else{
					//key不重复,则遍历下一个
					iterLast = temp;
					temp = temp.next;
					
				}
			}
			
			if(!keyRepeat){//没有发生重复的情况,则添加到链表的最后
				iterLast.next = newNode;
				size++;
			}
		}
		
	}
	
	@Override
	public String toString() {
		//[10:aa,20:bb]
		StringBuilder sb = new StringBuilder("{");
		
		//遍历bucket数组
		for(int i = 0; i<table.length; i++){
			Node3 temp = table[i];
			//遍历链表
			while(temp != null) {
			
				sb.append(temp.key+":"+temp.value+",");
				temp = temp.next;
			}
		}
		sb.setCharAt(sb.length()-1, '}');
		return sb.toString();
		
	}
	
	public static int myHash(int v,int length){
		//作用一样 都是为了吧hashcode散列到 0-16 之间          值不一样
		// v&(length-1)  直接位运算,效率高
		// v%(length-1)	   取模运算,效率低
		
		return v&(length-1); 
	}
	
	
	public static void main(String[] args) {
		TestHashMap4<Integer,String> m = new TestHashMap4<>();
		m.put(10, "a");
		m.put(20, "b");
		m.put(30, "c");
		m.put(10, "666");
		
		
		//观察debug中的 hash为5 的next 
		m.put(53, "d");//hash值为5
		m.put(69, "e");//hash值为5
		m.put(85, "f");//hash值为5
		
		//通过debug功能 来观察相关功能的实现
		System.out.println(m);
		
		//找到hash值相同的元素   
		/*for(int i=10; i<100; i++){
			System.out.println(i+"--"+myHash(i, 16));
		}*/
		
		System.out.println(m.get(69));
		
		m.remove(69);
		
		System.out.println(m);
	}
}

	
}

package mycollection;

//HashMap
public class Node3<K,V> {
	
	int hash;
	K key;
	V value;
	Node3 next;```

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值