HashMap Implementation Java

Question: Implement the Data Structure of  HashMap

Idea:

    Create a Map class with Key, Value, and Linked list to hanlde collection
    HashMap: put operation ->
    1. generate the hash value using key with hashcode() function.
    2. if current bucket is empty, put key-value into this empty bucket
    3. else
        we need to handle the collision case if there is key exist in same bucket,
        2.1) if the kye is same, overwrite it
        2.2) else add conflicted key-value pair into end of list
    HashMap: get operation->
    1. generate the hash value using key with hashcode() function.
    2. if key is not empty
        2.1) traverse the likeded list, return value if both keys are match

       else return null

public class HashMapImplementation {
    //set initial size as 2^5=32
    private int size=32;
    private Map table[];
    HashMapImplementation(){
        table=new Map[size];
    }
    /*
    Map Operation: Put->
    Associate the specified value with the specified key in this map.
     */
    public void put(String k,String v){
       //generate hashValue
        int hash=k.hashCode() % size;
        Map m=table[hash];
        if(m==null){
            Map mapIntoNewBucket=new Map(k,v);
            table[hash]=mapIntoNewBucket;

        }else{
            //handle the collision case
            //there is one key exist in this bucket
            if(m.key.equals(k)){
                // if the key is duplicated
                // overwrite the exist key-value pair
                m.setValue(v);
            }else{  //non-duplicated key
                //insert new key into end of list
                // in same bucket
                while(m.next!=null){
                    m=m.next;
                }
                Map mapIntoSameBucket=new Map(k,v);
                m.next=mapIntoSameBucket;
            }
        }
    }
    /*
    Map Operation: get ->
    Return the specific value which is associate with specific key
    in HashMap,.
    Return null if HashMap not contain the key
     */
    public String get(String k){
        int hash=k.hashCode() % size;
        Map m=table[hash];
        //if the bucket is found, then traverse the linkedlist
        //to see if the value associate key is present?
        while(m!=null){
            //find mapping key
            if(m.key.equals(k)){
                return m.getValue();
            }
            m=m.next;
        }
        return null;
    }
    public static void main(String[] args) {
        HashMapImplementation map = new HashMapImplementation();

        map.put("John", "1");
        map.put("Dan", "2");
        map.put("Chris", "3");
        map.put("Hello", "4");

        String value = map.get("Chris");
        System.out.println("test_1: "+value);
        map.put("Chris", "5");
        String value2 = map.get("Chris");
        System.out.println("test_2: "+value2);
    }
}

class Map{
    /*
    Define a simple Map data Structure
     with key and value
     */
    String key;
    String value;
    Map next;

    Map(String k,String v){
        this.key=k;
        this.value=v;
    }
    //Map basic operation that setValue, getValue, getKey,
    public String getValue(){
        return value;
    }
    public void setValue(String v){
        this.value=v;
    }
    public String getKey(){
        return key;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值