I've done a minimal implementation of a HashMap. Invite comments.
public class HashMap<K, V> {
private final int size = 16;
private Node<K, V>[] arr;
public HashMap(){
arr = new Node[size];
}
class Node<K, V> {
private int index;
private K key;
private V value;
private Node<K, V> next;
protected Node(K key, V value){
this.key = key;
this.value = value;
}
}
private int getHashCodeForKey(K key){
int result = 7;
return 31 * result + (key != null ? key.hashCode() : 0);
}
public V get(K key){
int hashcode = getHashCodeForKey(key);
int index = hashcode & size;
Node head = arr[index];
while(head != null){
if(head.key == key) return (V) head.value;
head = head.next;
}
return null;
}
public void put(K key, V value){
Node curr = new Node(key, value);
int hashcode = getHashCodeForKey(key);
int index = hashcode & size;
if(arr[index] == null){
arr[index] = curr;
}else{
Node head = arr[index];
curr.next = head;
}
}
}