Ok, on this occasion I will write article about java map specially hashmap. Map is interface and hashmap is one of Map interface implementation. Actually hashmap is part of java collections. Every java programmer should be familiar with HashMap. HashMap is collection that save array data with key value pairing. The key must be unique, doesn’t allow for duplication and to maintain its uniqueness the key object should override equals and hashCode methods.

Java HashMap class is very complex, therefore I will write simple concept about hashmap concept.

Simple HashMap Concept

Base on above picture, in storing hashmap key value, it uses array and linked Node object. At first getting the hashcode of key object, then with some algorithm we will get integer value, and use the integer value as index on hashmap table array. Hashmap table array on specific index contains linked node object, node object contains key valu as object properties.

Steps to create simple HashMap class:

  1. Create Node object, the node object will be used to encapsulate key, value of hashmap
static class NodeHash<K,V>{
private NodeHash<K,V> prev;
private NodeHash<K,V> next;
private V value;
private K key;
private int hash;

public NodeHash(NodeHash<K,V> prev, NodeHash<K,V> next , V value, K key, int hash ) {
this.prev = prev;
this.next = next;
this.value = value;
this.key = key;
this.hash = hash;
}
public NodeHash<K, V> getPrev() {
return prev;
}
public NodeHash<K, V> getNext() {
return next;
}
public V getValue() {
return value;
}
public K getKey() {
return key;
}
public int getHash() {
return hash;
}
public void setPrev(NodeHash<K, V> prev) {
this.prev = prev;
}
public void setNext(NodeHash<K, V> next) {
this.next = next;
}
public void setValue(V value) {
this.value = value;
}
public void setKey(K key) {
this.key = key;
}
public void setHash(int hash) {
this.hash = hash;
}

}

2. Create put method. The put method will be used to add data to hashmap

public V put(K key, V value) {
int hash = hashKey(key) & 1000;
System.out.println("put hash = " + hash);
if(table[hash] != null) {
NodeHash<K,V> nodes = (NodeHash<K, V>) table[hash];
NodeHash<K,V> nextNode = nodes;
boolean lopping = true;
while (lopping) {
//nextNode.getNext() for not null
if(nextNode.getNext() != null) {
//check the key is equal or not
if(key.equals(nextNode.getKey())) {
//replace the value
nextNode.setValue(value);
break;
}
//if key not equal execute the code below
nextNode = nextNode.getNext();
}else {
//check the key is equal or not
if(key.equals(nextNode.getKey())) {
//replace the value
nextNode.setValue(value);
break;
}
//add new NodeHash object to array table[hash]
NodeHash<K,V> newNode1 = new NodeHash(nextNode, null, value, key, hash);
nextNode.setNext(newNode1);
lopping = false;
}
}

}else {
//add new NodeHash object to array table[hash]
NodeHash<K,V> newNode = new NodeHash(null, null, value, key, hash);
table[hash] = newNode;
}
return null;
}

3. Create get method. The get method will be used to get value base on key

public V get(K key) {
int hash = hashKey(key) & 1000;
System.out.println("get hash = " + hash);
if(table[hash] != null) {
NodeHash<K,V> nodes = (NodeHash<K, V>) table[hash];
NodeHash<K,V> nextNode = nodes;
boolean lopping = true;
while (lopping) {
//lopping until null
if(nextNode.getNext() != null) {
if(key.equals(nextNode.getKey())) {
return nextNode.getValue();
}
nextNode = nextNode.getNext();
}else {
if(key.equals(nextNode.getKey())) {
return nextNode.getValue();
}
return null;
}
}
}
return null;
}

4. Create hashKey method. The hash key method will be used to get hashcode of key object.

static final int hashKey(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}

full source code is available on github : https://github.com/wahyueko22/unit-test-java-collection/blob/master/src/main/java/com/map/MockHashMap.java

i am a reader