二重Arraylist简易模拟HashMap

package Day_3;

import java.util.ArrayList;
import java.util.Arrays;

public class Try_list_list {
	private int size;
	private ArrayList<ArrayList> arrayLists;
	public Try_list_list() {
		size = 10;
		arrayLists = new ArrayList<ArrayList>(size);
		for(int i=0;i<size;i++) {
			ArrayList< TestMapD3> temp = new ArrayList<TestMapD3>(10);
			arrayLists.add(temp);
		}
	}
	public Try_list_list(int initSize) throws Exception {
		if(initSize < 0)
			throw new Exception("illegel parament size");
		else {
			size = initSize;
			arrayLists = new ArrayList<ArrayList>(size);
			for(ArrayList temp : arrayLists) {
				temp = new ArrayList<TestMapD3>(10);
				arrayLists.add(temp);
			}
		}
	}
	
	/**
	 * 根据key来hashcode找下标,并判断是否存在,即不可重名key
	 * @param key
	 * @param value
	 * @throws Exception
	 */
//	public void  add(Object key,Object value) throws Exception {
//		TestMapD3 testMapD3 = new TestMapD3(key,value);
//		int arrayIndex = key.hashCode() % 9;
//		ArrayList<TestMapD3> tempArrayList = arrayLists.get(arrayIndex);
//		for(TestMapD3 tempTestMapD3:tempArrayList) {
//			if (testMapD3.key.equals(tempTestMapD3.key)) {
//				throw new Exception("key had exist,can't add again");
//			}
//		}
//		tempArrayList.add(testMapD3);
// 	}
	
	/**
	 * 根据key来hashcode找下标,但使用整体判断是否存在,即可重名key,但不能重复Node
	 * @param key
	 * @param value
	 * @throws Exception
	 */
	public void  add(Object key,Object value) throws Exception {
		TestMapD3 testMapD3 = new TestMapD3(key,value);
		int arrayIndex = testMapD3.key.hashCode() % 9;
		ArrayList<TestMapD3> tempArrayList = arrayLists.get(arrayIndex);
		for(TestMapD3 tempTestMapD3:tempArrayList) {
			if (testMapD3.equals(tempTestMapD3)) {
				throw new Exception("key had exist,can't add again");
			}
		}
		tempArrayList.add(testMapD3);
 	}
	
	public Object remove(Object key) throws Exception{
		int arrayIndex = key.hashCode() % 9;
		ArrayList<TestMapD3> tempArrayList = arrayLists.get(arrayIndex);
		for(int i = 0;i < tempArrayList.size();i++) {
			if (key.equals(tempArrayList.get(i).key)) {
				return tempArrayList.remove(i); 
			}
		}
		throw new Exception("can't remove no exsit key_value");
	}
	
	
	public Object get(Object key) throws Exception{
		int arrayIndex = key.hashCode() % 9;
		ArrayList<TestMapD3> tempArrayList = arrayLists.get(arrayIndex);
		for(int i = 0;i < tempArrayList.size();i++) {
			if (key.equals(tempArrayList.get(i).key)) {
				return tempArrayList.get(i).value;
			}
		}
		throw new Exception("can't get no exsit key_value");
	}
	
	public Object set(Object key,Object value) throws Exception {
		TestMapD3 testMapD3 = new TestMapD3(key,value);
		int arrayIndex = key.hashCode() % 9;
		ArrayList<TestMapD3> tempArrayList = arrayLists.get(arrayIndex);
		for(TestMapD3 tempTestMapD3:tempArrayList) {
			if (testMapD3.key.equals(tempTestMapD3.key)) {
				tempTestMapD3.value = value;
				return tempTestMapD3;
			}
		}
		throw new Exception("can't set no exsit key_value");
	}
	
	
	public String find_key(Object key) {
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append("[");
		int arrayIndex = key.hashCode() % 9;
		ArrayList<TestMapD3> tempArrayList = arrayLists.get(arrayIndex);
		for(int i = 0;i < tempArrayList.size();i++) {
			if (key.equals(tempArrayList.get(i).key)) {
				stringBuilder.append(tempArrayList.get(i).value+" ");
			}
		}
		stringBuilder.append("]");
		return stringBuilder.toString();
	}
	
	public static void main(String[] args) throws Exception {
		Try_list_list myList = new Try_list_list();
		myList.add("张三", "自然科学");
		myList.add("李四", "国际政治");
		System.out.println(myList.get("张三"));
		System.out.println(myList.get("李四"));
		myList.add("张三", "北京大学");
		System.out.println(myList.find_key("张三"));
//		myList.remove("aaa");
//		System.out.println(myList.get("aaa"));
	}
}



class TestMapD3{
	Object key;
	Object value;
	
	public TestMapD3(Object key, Object value) {
		super();
		this.key = key;
		this.value = value;
	}
	
	@Override
	public boolean equals(Object obj) {
		super.equals(obj);
		if(obj == null)
			return false;
		if(this == obj)
			return true;
		if(obj.getClass() != this.getClass())
			return false;
		TestMapD3 temp = (TestMapD3)obj;
		return this.key.equals(temp.key) && this.value.equals(temp.value);
	}
	
	@Override
	public int hashCode() {
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append(key.toString());
		stringBuilder.append(value.toString());
		return stringBuilder.hashCode();
	}
	
	
	
	
	
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值