Very Important:How to design HashMap

1.Data Structure need to store the key-value Pair
Make an Entry Class to Store the HashMap Entries
Variable: key value and next
next(Entry) variable is used to avoid the collision pf hashmap using chaining(LinkedList)

  1. put() method to put new entries in hashmap.
    Identify the bucket using hash (hashcode%SIZE)

a. If no element exists in that bucket: put it as new Entry.
b. If element already exists:
If element is duplicate, replace old one, otherwise find the last element in the chain and add new entry to next pointer of last element.

  1. get() method : return the element in the hashmap
    Identify the element bucket by calculate the hash (hashcode%SIZE) of the key, and return the element using equals method.

  2. Define size in power on 2
    Rehashing is good when the size is exponential of 2.
    Set initial size = 2^4 = 16.
    To expanding HashMap, make size to its double using power, i.e. 2^5= 32.
    To reducing HashMap, make size to its half using power, i.e. 2^3= 8.

public class MyHashMap {
      // for better re-sizing is taken as 2^4
      private static final int SIZE = 16;

      private Entry table[] = new Entry[SIZE];

      /**
       * To store the Map data in key and value pair.
       * Used linked list approach to avoid the collisions
       */
      class Entry {
            final String key;
            String value;
            Entry next;

            Entry(String k, String v) {
                  key = k;
                  value = v;
            }

            public String getValue() {
                  return value;
            }

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

            public String getKey() {
                  return key;
            }
      }

      /**
       * Returns the entry mapped to key in the HashMap.
       */
      public Entry get(String k) {
            int hash = k.hashCode() % SIZE;
            Entry e = table[hash];

            // Bucket is identified by hashCode and traversed the bucket
            // till element is not found.
            while(e != null) {
                  if(e.key.equals(k)) {
                        return e;
                  }
                  e = e.next;
            }
            return null;
      }

      /**
       * If the map previously contained a mapping for the key, the old
       * value is replaced.
       */
      public void put(String k, String v) {
            int hash = k.hashCode() % SIZE;
            Entry e = table[hash];

            if(e != null) {
                  // If we will insert duplicate key-value pair,
                  // Old value will be replaced by new one.
                  if(e.key.equals(k)) {
                        e.value = v;
                  } else {
                        // Collision: insert new element at the end of list
                        // in the same bucket
                        while(e.next != null) {
                              e = e.next;
                        }
                        Entry entryInOldBucket = new Entry(k, v);
                        e.next = entryInOldBucket;
                  }
            } else {
                  // create new bucket for new element in the map.
                  Entry entryInNewBucket = new Entry(k, v);
                  table[hash] = entryInNewBucket;
            }
      }

      public static void main(String[] args) {
            MyHashMap myHashMap = new MyHashMap();

            myHashMap.put("Awadh", "SSE");
            myHashMap.put("Rahul", "SSE");
            myHashMap.put("Sattu", "SE");
            myHashMap.put("Gaurav", "SE");

            Entry e = myHashMap.get("Awadh");
            System.out.println(""+e.getValue());
      }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值