HashMap Implementation In Java
Chapter:
Data Structures
Last Updated:
07-07-2018 08:40:59 UTC
Program:
/* ............... START ............... */
import java.util.ArrayList;
import java.util.LinkedList;
public class HashMap<K,V> {
public class hmnodes{ //HashMap nodes
K key;
V value;
}
private int size=0; //size of hashmap
private LinkedList<hmnodes> buckets[]; //array of addresses of list
public HashMap(){
buckets=new LinkedList[4]; //initially create bucket of any size
for(int i=0;i<4;i++)
buckets[i]=new LinkedList<>();
}
public void put(K key,V value) throws Exception{
int bi=bucketIndex(key); //find the index,the new key will be inserted in linklist at that index
int fountAt=find(bi,key); //check if key already exists or not
if(fountAt==-1){
hmnodes temp=new hmnodes(); //if doesn't exist create new node and insert
temp.key=key;
temp.value=value;
buckets[bi].addLast(temp);
this.size++;
}else{
buckets[bi].get(fountAt).value=value;//if already exist modify the value
}
double lambda = (this.size*1.0)/this.buckets.length;
if(lambda>2.0){
rehash(); //rehashing function which will increase the size of bucket as soon as lambda exceeds 2.0
}
return;
}
public V get(K key) throws Exception{
int bi=bucketIndex(key);
int fountAt=find(bi,key);
if(fountAt==-1){
return null;
}else{
return buckets[bi].get(fountAt).value;
}
}
public V remove(K key) throws Exception{
int bi=bucketIndex(key);
int fountAt=find(bi,key);
if(fountAt==-1){
return null;
}else{
this.size--;
return buckets[bi].remove(fountAt).value;
}
}
public boolean containskey(K key) throws Exception{
int bi=bucketIndex(key);
int fountAt=find(bi,key);
if(fountAt==-1){
return false;
}else{
return true;
}
}
public int size(){
return this.size;
}
public boolean isempty(){
return this.size==0;
}
public ArrayList<K> keyset() throws Exception{
ArrayList<K> arr=new ArrayList<>();
for(int i=0;i<buckets.length;i++){
for(int j=0;j<buckets[i].size();j++){
arr.add(buckets[i].get(j).key);
}
}
return arr;
}
public ArrayList<V> valueset() throws Exception{
ArrayList<V> arr=new ArrayList<>();
for(int i=0;i<buckets.length;i++){
for(int j=0;j<buckets[i].size();j++){
arr.add(buckets[i].get(j).value);
}
}
return arr;
}
public void display() throws Exception{
for(int i=0;i<buckets.length;i++){
System.out.print("Bucket: "+i+" ");
for(int j=0;j<buckets[i].size();j++){
hmnodes temp=buckets[i].get(j);
System.out.print("["+temp.key+"->"+temp.value+"]");
}
System.out.println();
}
}
public int find(int bi,K key) throws Exception{
for(int i=0;i<buckets[bi].size();i++){
if(key.equals(buckets[bi].get(i).key))
return i;
}
return -1;
}
public int bucketIndex(K key) throws Exception{
int bi=key.hashCode();
return Math.abs(bi%buckets.length);
}
private void rehash() throws Exception{
LinkedList<hmnodes> ob[]= buckets;
buckets=new LinkedList[ob.length*2];
for(int i=0;i<ob.length*2;i++)
buckets[i]=new LinkedList<>();
size = 0;
for(int i=0;i<ob.length;i++){
for(int j=0;j<ob[i].size();j++){
put(ob[i].get(j).key,ob[i].get(j).value);
}
}
}
public static void main(String args[]) {
System.out.println("Hash map implementation in java ");
HashMap<Integer,String > map = new HashMap<>();
try {
map.put(1, "Value 1");
map.put(2, "Value 2");
map.put(3, "Value 3");
map.put(4, "Value 4");
map.put(5, "Value 5");
map.put(6, "Value 6");
System.out.println("Contains Key 1"+map.containskey(1));
System.out.println("Contains Key 5"+map.containskey(5));
System.out.println(map.get(1));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/* ............... END ............... */
Output
Hash map implementation in java
Contains Key 1true
Contains Key 5true
Value 1
Notes:
-
For HashMap implementation we use a class that is used for storing Key and value pairs, it is denoted as HashMap<Key, Value>.
- HashMap is a Map based collection class that is used for storing Key and value pairs.
- To Store key and value first we have to create a class with key and values. Check the class implementation hmodes class.
- Declare a variable to handle the size of the hashmap and linkedlist store the elements of hashamp.
- In the HashMap() constructor create and initialize the bucket of specific size.
- bucketIndex(Key) function will returns the index to store the element in the hashmap. We are passing key as the argument in function. First we will find the hashcode of key by using the function hashcode() in java, then will do Modulus (%) operation with bucket size to get the index.
- find(int index,K key) function is used to check whether key already exists or not. For that it wil iterate buckets and check whether key exists or not if found it will return the index of key or return -1.
Tags
Implement hashmap in java, hashmap Key and Value, hashmap functions