一个简单的hashmap

这个博客展示了如何从头实现一个简单的HashMap数据结构。代码包括了put、get、contains和remove等基本操作,并通过测试用例验证了其实现的正确性。测试中使用了自定义的MyKey类来覆盖默认的hashCode和equals方法,以确保映射的正确性。
摘要由CSDN通过智能技术生成

实现一个简单的HashMap实现。

public class MyHashMap {
	
	private static class Node{
		public Node next;
		public Object key;
		public Object value;
	}

	
	private int nodeTotal = 0;
	private Node[] objArray;
	
	public MyHashMap(){
		this(16);
	}
	
	public MyHashMap(int maxSize){
		objArray = new Node[maxSize];
	}
	
	public void put(Object k, Object v) throws Exception {
		// calucate the hash key 
		if(k==null) throw new Exception("key is null");
		int pos = Math.abs(k.hashCode())%objArray.length;
		
		Node n = new Node();
		n.key = k;
		n.value = v;
		if(objArray[pos]==null){
			objArray[pos] = n;
		}else{
			Node tempNode = objArray[pos];
			while(tempNode.next!=null){
				tempNode = tempNode.next;
			}
			tempNode.next = n;
		}
		
	}
	
	public Object get(Object k) throws Exception {
		if(k==null) throw new Exception("key is null");
		int pos = Math.abs(k.hashCode())%objArray.length;
		if(objArray[pos]!=null){
			Node tempNode = objArray[pos];
			while(tempNode!=null){
				if(tempNode.key.equals(k)){
					return tempNode.value;
				}
				tempNode = tempNode.next;
			}
		}
		return null;
	}
	
	public boolean contains(Object k) throws Exception {
		if(k==null) throw new Exception("key is null");
		int pos = Math.abs(k.hashCode())%objArray.length;
		if(objArray[pos]!=null){
			Node tempNode = objArray[pos];
			while(tempNode!=null){
				if(tempNode.key.equals(k)){
					return true;
				}
				tempNode = tempNode.next;
			}
		}
		return false;
	}
	
	public Object remove(Object k) throws Exception {
		if(k==null) throw new Exception("key is null");
		int pos = Math.abs(k.hashCode())%objArray.length;
		if(objArray[pos]!=null){
			Node tempNode = objArray[pos], prevNode = null;
			while(tempNode!=null){
				if(tempNode.key.equals(k)){
					if(prevNode==null){
						objArray[pos] = tempNode.next;
					}else{
						prevNode.next = tempNode.next;
					}
					return tempNode.value;
				}
				prevNode = tempNode;
				tempNode = tempNode.next;
			}
		}
		return null;
	}
	
	public void clear(){
		for(int i=0; i<objArray.length; ++i){
			objArray[i] = null;
		}
	}
	

// below is testing code

	private static class MyKey {
	    private final String key;
	    
	    MyKey(String key) {
	        this.key = key;
	    }
	    
	    public int hashCode() {
	        return 5;
	    }
	    
	    public boolean equals(Object other) {
	        return (other instanceof MyKey) && key.equals(((MyKey)other).key);
	    }
	}
	
	public static void main(String[] args) throws Exception {
		/*
		String k1 = "key001"; String v1 = "value1";
		String k2 = "key002"; String v2 = "value2";
		MyHashMap testMap = new MyHashMap();
		testMap.put(k1, v1);
		testMap.put(k2, v2);
		System.out.println(testMap.get(k1));
		System.out.println(testMap.get(k2));
		*/
		

		MyKey k1 = new MyKey("key001"); String v1 = "value1";
		MyKey k2 = new MyKey("key002"); String v2 = "value2";
		MyHashMap testMap = new MyHashMap();
		testMap.put(k1, v1);
		testMap.put(k2, v2);
		System.out.println(testMap.get(k1));
		System.out.println(testMap.get(k2));
		System.out.println(testMap.remove(k2));
		System.out.println(testMap.get(k2));
		
		
	}
	
	

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值