Write a HashMap

package cn.aresoft;

/ **
* principles of the HashMap
*
* @author develp
* key to the HashMap is a data structure of stored data, this is simple. Internal how to achieve it? An array actually used (entries It),
* then each element in the array can be viewed as a list (entry). When storing a key-value pair, you take a Key and Value,
* hash algorithm will be calculated based on your Key a value, we put this value as the internal array (entries) index value (index),
* and then find the next element to store the value of the index value, if the index already exists in this element, but because each array element (entry) can be seen as a linked list can be stored attached,
* it can be inserted into the index. But here are the first interpolation method, because some force majeure will be more likely to query the newly inserted elements. Look at the code, although written bad, if it can not understand, take a look at [
*] code agricultural turn over to the link above.
@Param * <K>
* @param <V>
* /
public class MyHashMap <K, V> {
Private default_length static int = 16;
Private MyEntry <K, V> [] entries It;

public MyHashMap() {
super();
entries = new MyEntry[default_length];
}

public V put(K key, V value) {
int index = key.hashCode() % default_length;
MyEntry<K, V> previous = entries[index];
for (MyEntry entry = entries[index]; entry != null; entry = entry.next) {
if (entry.getKey().equals(key)) {
V oldValue = (V) entry.getValue();
entry.setValue(value);
return oldValue;
}
}
MyEntry<K, V> entry = new MyEntry<>(key, value);
entry.next = previous;
entries[index] = entry;
return null;
}

public V get(K key) {
int index = key.hashCode() % default_length;
for (MyEntry<K, V> entry = entries[index]; entry != null; entry = entry.next) {
if (entry.getKey().equals(key)) {
return entry.getValue();
}
}
return null;
}

private final class MyEntry<K, V> {
private K key;
private V value;
private MyEntry next;

public MyEntry(K key, V value) {
super();
this.key = key;
this.value = value;
}

public MyEntry() {
super();
}

public MyEntry(K key, V value, MyEntry next) {
super();
this.key = key;
this.value = value;
this.next = next;
}

public K getKey() {
return key;
}

public void setKey(K key) {
this.key = key;
}

public V getValue() {
return value;
}

public void setValue(V value) {
this.value = value;
}

public MyEntry getNext() {
return next;
}

public void setNext(MyEntry next) {
this.next = next;
}
}

public static void main(String[] args) {
MyHashMap<String, Integer> mhm = new MyHashMap();
mhm.put("lmy", 20);
System.out.println(mhm.get("lmy"));
}
}

 

 

 

 

 

 

 

 

https://mp.weixin.qq.com/s?__biz=MzAxOTc0NzExNg==&mid=2665514069&idx=1&sn=2996d864bbe596d0af763fba3d244fa7&chksm=80d67c16b7a1f500ec6a191eb4a0beac0e95dbd5a7bf8ee01f5ed2cb17960b9ab32c0b965949&mpshare=1&scene=23&srcid=1223e7BQGb04LAH3YZ1zDXAQ#rd

Reproduced in: https: //www.cnblogs.com/tan-chao/p/11065650.html

Guess you like

Origin blog.csdn.net/weixin_34204722/article/details/93499463