HashMap实现方式两种:
- 数组加链表
- 数组加红黑树
这里只简单实现了第一种方式,并且未实现拓容。
散列函数用了最简单的取余法。
顺便加上了泛型,简单练习一下
package ThashMap;
/**
* @date 2019/10/26 - 19:32
*/
public class Node<T, V> {
T key;
V value;
Node<T, V> next;
public Node(T key, V value) {
this.key = key;
this.value = value;
}
public Node() {
}
}
package ThashMap;
/**
* @author 马洁
* @date 2019/10/26 - 19:32
*/
public class Link<T, V> {
private Node<T, V> head;
public Link(Node<T, V> node) {
this.head = node;
head.next = null;
}
public Link() {
this(new Node<>());
}
/**
* 插入一个键值对,如果该节点的键和其他节点键重复,则覆盖值。
* @param key 键
* @param value 值
*/
public void insert(T key, V value) {
Node<T, V> tempNode = find(key);
if (tempNode != null) {
tempNode.value = value;
} else {
Node<T, V> node = new Node<>(key, value);
node.next = head.next;
head.next = node;
}
}
/**
* 根据指定key找相应结点
* @param key 键
* @return 找到的相应结点
*/
public Node<T, V> find(T key) {
Node<T, V> parent = findParent(key);
if (parent == null) {
return null;
} else {
return parent.next;
}
}
/**
* 根据指定key删除相应结点
* @param key 键
* @return 是否删除成功
*/
public boolean delete(T key) {
Node<T, V> parent = findParent(key);
if (parent == null) {
return false;
}
parent.next = parent.next.next;
return true;
}
/**
* 更新指定key的值
* @param key 键
* @param newValue 新值
* @return 是否更新成功
*/
public boolean update(T key, V newValue) {
Node<T, V> parent = findParent(key);
if (parent == null) {
return false;
}
parent.next.value = newValue;
return true;
}
/**
* 根据给定key找到其父节点
* @param key 键
* @return 对应的父节点
*/
private Node<T, V> findParent(T key) {
if (head == null) {
return null;
}
Node<T, V> node = head.next;
Node<T, V> parent = head;
while (node != null) {
if (node.key == key) {
return parent;
}
parent = node;
node = node.next;
}
return null;
}
/**
* 返回头结点
* @return 头结点
*/
public Node<T, V> getHead() {
return head;
}
}
package ThashMap;
/**
* @date 2019/10/26 - 19:33
*/
public class MyHashMap<T, V> {
Link<T, V>[] map;
private static final int INIT_SIZE = 10;
private int size;
public MyHashMap(int size) {
this.map = new Link[size];
this.size = size;
}
public MyHashMap() {
this(INIT_SIZE);
}
public void put(T key, V value) {
if (this.map[getIndex(key)] == null) {
this.map[getIndex(key)] = new Link<>();
}
this.map[getIndex(key)].insert(key, value);
}
public V get(T key) {
return this.map[getIndex(key)].find(key).value;
}
public boolean delete(T key) {
return this.map[getIndex(key)].delete(key);
}
public boolean update(T key, V value) {
return this.map[getIndex(key)].update(key, value);
}
public Integer getIndex(T key) {
return key.hashCode() % size;
}
public Link<T, V>[] getMap() {
return map;
}
}
package ThashMap;
/**
* @date 2019/10/26 - 19:33
*/
public class Main {
public static void main(String[] args) {
MyHashMap<Integer, String> map = new MyHashMap<>();
map.put(1, "a");
map.put(4, "d");
map.put(2, "b");
map.put(3, "c");
map.put(5, "e");
map.put(6, "f");
map.put(11, "b");
map.put(1, "aa");
print(map);
System.out.println("查找5对应的值" + map.get(5));
System.out.println("=====");
map.delete(1);
System.out.println("删除1对应的键值对");
print(map);
System.out.println("更新2的值为del");
map.update(2, "del");
print(map);
}
public static void print(MyHashMap map) {
System.out.println("map值如下:");
Link[] links = map.getMap();
int i = 0;
for (Link link:
links) {
System.out.println("第" + (i++) + "个数组");
if (link != null) {
Node node = link.getHead();
while (node != null) {
System.out.println("key = " + node.key + ", value = " + node.value);
node = node.next;
}
}
}
System.out.println("=====");
}
}