Very Important:How to design HashMap
来源:互联网 发布:淘宝店铺全屏装修 编辑:程序博客网 时间:2024/06/03 23:04
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)
- 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.
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.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()); }}
- Very Important:How to design HashMap
- How to use libsvm for regression in matlab (It is very important)
- CPU Design HOW-TO
- How to Design Programs
- How to sort HashMap ??
- HashMap how to work
- Very important URL
- English is very important!
- Sum is very important!
- How to Use Design Patterns
- How to use design patterns
- How to Use Design Patterns
- How to design your model
- 《How to design programs》引子
- how to design MongoDB schema
- linux 学习url-very important
- How to Use Design Patterns(转载)
- How to Use Design Patterns(ZT)
- Easy 24题 Swap Nodes in Pairs
- 测试
- Android之beginTransaction
- 测试代码助手
- 单片机_LCD显示屏
- Very Important:How to design HashMap
- 第四章的习题
- HDU-3367-Pseudoforest
- 然而像eclipse这样的IDE环境要与JDK对应
- 二叉树系列---标记二叉树中每个节点的next指针
- Spark+kafka+SparkStreaming实例
- 二叉树系列---path-sum
- 二叉树系列---path-sum-II
- 二叉树系列---判断二叉树是否为平衡二叉树