-
Notifications
You must be signed in to change notification settings - Fork 366
Added Hash Map Implementation for java #593
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bhaweshgit06
wants to merge
1
commit into
vichitr:master
Choose a base branch
from
bhaweshgit06:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
|
|
||
| import java.util.ArrayList; | ||
|
|
||
| // Node class to be used in hash map implementation | ||
| class MapNode<K,V> { | ||
| K key; | ||
| V value; | ||
| MapNode<K,V> next; // linked list node with data of key and value pair and next; | ||
| MapNode(K key, V value){ | ||
| this.key = key; | ||
| this.value = value; | ||
| } | ||
| } | ||
|
|
||
| public class HashMap<K,V>{ | ||
| ArrayList<MapNode<K,V>> buckets; | ||
| int count; | ||
| int numBucket; | ||
| HashMap(){ | ||
| buckets = new ArrayList<>(); | ||
| numBucket = 5; | ||
| for(int i = 0; i< numBucket; i++) { | ||
| buckets.add(null); | ||
| } | ||
| } | ||
|
|
||
| private int HashCode(K key) { | ||
| int hc = key.hashCode(); | ||
| int bucketIndex = hc % numBucket; | ||
| return bucketIndex; | ||
| } | ||
|
|
||
|
|
||
| public void insert(K key, V value) { | ||
| int BucketIndex = HashCode(key); | ||
| // get the linked list at the bucketIndex // linked lists are already | ||
| // initialized in all the index with null | ||
| MapNode<K,V> head = buckets.get(BucketIndex); | ||
|
|
||
| while (head != null) { | ||
| // If we already have the key we just need to check if key is already present | ||
| // and just update the value and return; | ||
| if (head.key.equals(key)) { | ||
| head.value = value; | ||
|
Comment on lines
+43
to
+44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Potential NPE and return value issue in
|
||
| return; | ||
| } | ||
|
|
||
| head = head.next; | ||
|
|
||
| } | ||
| head = buckets.get(BucketIndex); | ||
|
|
||
| // if we dont found the key we have to mkae a new node and attach it to the prev | ||
| // head and update the array list index with new head. | ||
| MapNode<K, V> newNode = new MapNode<>(key, value); | ||
| newNode.next = head; | ||
| buckets.set(BucketIndex, newNode); | ||
| count++; | ||
|
|
||
| double loadFactor = loadfactor(); | ||
| if(loadFactor>= 0.7) { | ||
| rehash(); | ||
| } | ||
|
|
||
| } | ||
| public double loadfactor() { | ||
| return (1.0 * count)/numBucket; | ||
| } | ||
| private void rehash() { | ||
|
|
||
| ArrayList<MapNode<K, V>> temp = buckets; | ||
| buckets = new ArrayList<>(); | ||
| for(int i = 0; i < 2*numBucket; i++) { | ||
| buckets.add(null); | ||
| } | ||
| count = 0; | ||
| numBucket = numBucket*2; | ||
| for(int i = 0 ; i < temp.size(); i++) { | ||
| MapNode<K, V> head = temp.get(i); | ||
| while(head != null) { | ||
| K key = head.key; | ||
| V value = head.value; | ||
| insert(key,value); | ||
|
|
||
| head = head.next; | ||
|
|
||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
|
|
||
| public V getValue(K key) { | ||
| int BucketIndex = HashCode(key); | ||
|
|
||
| MapNode<K, V> head = buckets.get(BucketIndex); | ||
|
|
||
| while (head != null) { | ||
|
|
||
| if (head.key.equals(key)) { | ||
|
|
||
| return head.value; | ||
| } | ||
|
|
||
| head = head.next; | ||
|
|
||
| }return null; | ||
| } | ||
| public V removeKey(K key) { | ||
| int BucketIndex = HashCode(key); | ||
|
|
||
| MapNode<K, V> head = buckets.get(BucketIndex); | ||
| if(head.key.equals(key)) { | ||
| buckets.set(BucketIndex, head.next); | ||
| count --; | ||
| return head.value; | ||
| } | ||
| while (head.next != null) { | ||
|
|
||
| if (head.next.key.equals(key)) { | ||
| head.next = head.next.next; | ||
| count--; | ||
| return head.next.value; | ||
| } | ||
|
|
||
| head = head.next; | ||
|
|
||
| } | ||
| return null; | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bucket index can become negative:
In
HashCode(),hc % numBucketmay return a negative value ifhashCode()is negative, which can causebuckets.get(bucketIndex)to crash.Suggestion: use
Math.floorMod(hc, numBucket)for a safe non-negative index.