HashMap put,get,remov 简单实现
使用泛型
package Test;
/**
* 手工实现HashMap
* 实现put方法增加键值对
* 实现键重复覆盖
* 实现增加
* 实现toString
* 实现get
* 实现remov
* @author pmc
*
*/
public class HashMapTest3<K,V>{
Node3[] table;//位桶数组
int size;//存放的键值对个数
public HashMapTest3() {
this(16);//2的整数次幂
}
public HashMapTest3(int index){
table =new Node3[index];
}
public void put(K key,V value){
//定义对象
Node3 newNode=new Node3();
newNode.hash=myHash(key.hashCode(),table.length);
newNode.key=key;
newNode.value=value;
newNode.next=null;
Node3 temp=table[newNode.hash];
//扩容
int sum=0;
for(int i=0;i<table.length;i++){
if(table[i]!=null){
sum++;
}
if(sum>=(table.length*0.75)){
HashMapTest3 newtemp=new HashMapTest3((table.length*2));
System.arraycopy(table, 0, newtemp.table, 0, table.length-1);
table=newtemp.table;
}
}
boolean isNull=false;//重复开关
Node3 templist=new Node3();
if(temp==null){
table[newNode.hash]=newNode;//此处元素为空的时候,放新节点进去
size++;
}else{
while(temp!=null){
//判断重复
if(temp.key.equals(newNode.key)){//temp.key==newNode.key
System.out.println("覆盖该元素:"+key);
temp.value=newNode.value;
isNull=true;
break;
}else{
//不重复
templist=temp;//保留上一个对象
temp=temp.next;//null
}
}
if(isNull==false){
templist.next=newNode;
size++;
}
}
}
public static int myHash(int v,int length){
// System.out.println("hash:"+(v&(length-1)));//位运算效率高
// System.out.println("hash:"+(v%(length-1)));//取模算效率低
return v&(length-1);
}
@Override
public String toString() {
StringBuilder sb=new StringBuilder("{");
for(int i=0;i<table.length;i++){
Node3 temp=table[i];
while (temp!=null) {
// sb.append(temp.hash+":"+temp.key+":"+temp.value);
sb.append(temp.value);
sb.append(",");
temp=temp.next;
}
}
sb.setCharAt(sb.length()-1, '}');
return sb.toString()+"元素:"+size;
}
public V get(K key){
int hash=myHash((int)key,table.length);//hash值
Node3 temp=table[hash];
while(temp!=null){
if(temp.key.equals(key)){
return (V)temp.value;
}
temp=temp.next;
}
return null;
}
public void remov(K key){
int hash=myHash((int)key,table.length);//hash值
Node3 temp=table[hash];//中间元素
Node3 up=temp;//上一个元素
boolean isDel=false;//开关
while(temp!=null){
Node3 down=temp.next;//下一个元素
//第一个元素
if(table[hash].key.equals(key)){
table[hash]=down;
isDel=true;
break;
}
//第二个元素以后,包含第二个元素
if(temp.key.equals(key)){
up.next=down;
isDel=true;
}
up=temp;
temp=temp.next;
}
if(isDel==true){
size--;
System.out.println("已删除该元素:"+key);
}else{
System.out.println("没有该元素:"+key);
}
}
public static void main(String[] args) {
HashMapTest3<Object,Object> t1=new HashMapTest3<Object,Object>();
t1.put(0,"A");
t1.put(0,"sa");
t1.put(1,"B");
t1.put(2,"C");
t1.put(3,"D");
t1.put(4,"E");
t1.put(5,"F");
t1.put(6,"G");
t1.put(7,"H");
t1.put(8,"I");
t1.put(9,"J");
t1.put(10,"K");
t1.put(11,"L");
t1.put(12,"M");//设定大于等于0.75的容量扩容,扩容后length就变成table.length*2等于(16*2)=32
t1.put(28,"Y");
t1.put(32,"Z");
t1.put(96,"X");
t1.put(63,"P");
t1.remov(96);
System.out.println(t1);
// for(int i=0;i<100;i++){
// System.out.println(i+"="+myHash(i,32));//0,10,16
// }
System.out.println(t1.get(28));
}
}
Node3--Entry<K,V>
package Test;
/**
*
* @author pmc
*
*/
public class Node3<K,V>{
int hash;
K key;
V value;
Node3 next;
}