package Study.Collection;
/**
* <p>Description: </p>
* 增加泛型,封装
* @author zhangyan
* @date 2019/1/29 17:33
*/
public class YanHashMap02<K,V> {
/**
* 位桶数组。bucket array
*/
Node3[] table;
/**
* 存放的键值对的个数
*/
int size;
/**
* 定义构造函数
*/
public YanHashMap02() {
//长度一般定义成2的整数幂
table = new Node3[16];
}
/**
* put方法
*
* @param key
* @param value
*/
public void put(K key, V value) {
//定义了新的节点对象
Node3 newNode = new Node3();
newNode.hash = myHash(key.hashCode(), table.length);
int hash = newNode.hash;
newNode.key = key;
newNode.value = value;
newNode.next = null;
/**
* temp是对应hash值的节点
*/
Node3 temp = table[hash];
//正在遍历的最后一个元素
Node3 iterLast = null;
boolean keyRepeat = false;
if (temp == null) {
//此处数组元素为空,则直接将新节点放进去
table[hash] = newNode;
size++;
} else {
//此处数组元素不为空,则遍历对应链表,对链表进行操作
while (temp != null) {
//判断key如果重复,则覆盖
if (temp.key.equals(key)) {
keyRepeat = true;
//只是覆盖value即可,其他的值(hash,key,next)保持不变
temp.value = value;
//覆盖之后就不用遍历链表后面的节点
break;
} else {
//key不重复,则遍历下一个。
iterLast = temp;
temp = temp.next;
}
}
//没有发生key重复的情况,则添加到链表最后一个节点
if(!keyRepeat){
iterLast.next = newNode;
size++;
}
}
}
/**
* get()方法
* @param key
* @return
*/
public V get(K key) {
//拿到对应的hash值
int hash = myHash(key.hashCode(), table.length);
V value = null;
if (table[hash] != null) {
Node3 temp = table[hash];
while (temp != null) {
//如果相等,则说明找到了键值对,返回相应的value
if (temp.key.equals(key)) {
value = (V)temp.value;
break;
} else {
temp = temp.next;
}
}
}
return value;
}
/**
* 重写toString()
*
* @return
*/
@Override
public String toString() {
//{10:aa, 20:bb}
StringBuilder sb = new StringBuilder("{");
//遍历table数组
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 void main(String[] args) {
YanHashMap02<Integer, String> m = new YanHashMap02<>();
m.put(10, "aa");
m.put(20, "bb");
m.put(30, "cc");
System.out.println(m);
System.out.println(m.get(10));
}
/**
* 返回Hash值
*
* @param v
* @param length
* @return
*/
public static int myHash(int v, int length) {
// System.out.println("hash in myHash:"+(v&(length-1))); //直接位运算,效率高
// System.out.println("hash in myHash:"+(v%(length-1))); //取模运算,效率低
return v & (length - 1);
}
}
节点:
public class Node3<K,V> {
int hash;
K key;
V value;
Node3<K,V> next;
}
增加泛型的思路也很简单,代码中注释也很清楚。
当然HashMap还有其他的方法,比如remove(),clone() 等等,HashMap还有扩容机制。
这里就不一一实现了,和前面的ArrayList,LinkedList基本差不多。
大家可以自己试着实现一下!!!