HashTable-(HashMap)

Run Settings
LanguageJavaScript
Language Version
Run Command
// So this is the second data structure the we are going to learn which is Hashtable. // No Hashtable is === to HashMap in Java its just different name. // Hashtable are unorder because there is no fix index like array. (Array are ordered) // In hashtable we store data in key-value pair. // suppose from basket you want to store 10 apples. So in it will be stored like apples : 10 where apples is key and 10 is value. // Now next question is where they are going to be stored. So this thing is decide by hashbox (blackbox) all you do is provide it key // and value and this will created one hash value for it and store it in memory. // NOW SEE SIMPLY HOW HASHTABLE WORKS : // You have key : value =====> You provide this Key : Value to hash function ======> It generates MD5 hash key => then store key value into that MD5 generated address // grapes : 10000 ===> hash function ====> asdasdweasd (generated key) ====> asdasdweasd : {grapes : 10000} (This is how things work). // In simple word hash function will decide where we will have that key value in memory. // Hash Collection /* * lookup : O(1) * Search : O(1) * insert : O(1) * delete : O(1) * See here everything is happening based on key now all we to to provide key and hashtable will return value for that specific key and that's why all the operation in hashtable is O(1). * NOW IF ALL THE OPERATION ARE O(1) THEN WHY DON'T WE USE HASHTABLE EVERY WHERE BUT AS WE KNOW EVERYTHING HAS PROS AND CONS NOW WE SEEN PROS BUT LETS NOW TALK ABOUT CONS : Hash collision : where the hash function generates the same index for more than one key * Now above word (Hash collision) is the single big reason why we are not using this everywhere. So what is that ? Sometime hash function generates same indexes for multiple keys as our system has limited memory. now is we will have same index for multiple keys then it will hard for use to do all operation in O(1) because now we are not sure that each index have one single value and becasue of that all above operation become O(n) in worst case. To demonstrate : https://www.cs.usfca.edu/~galles/visualization/OpenHash.html (Use this website for real example) To understand more : https://en.wikipedia.org/wiki/Hash_table. */ /* MI : Now see we are simply storing data in key : value pair so understand this before es6 in javascript objects === hashtable there is no different in both this but only problem with that is in object all the string get converted into string so even if you have integer key it will also get converted into string. NOW TO SOLVE ABOVE PROBLEM IN ES6 JAVASCRIPT INTERDUCED BELOW TWO THINGS : 1. let map = new Map() ; // Java ki copy. 2. let set = new Set() ; // it only stores keys wiil learn more about it. Now two bennefits of Map : 1. Now keys does not get converted into string it will be what you put like int , boolean or any key that you want. 2. It maintain order of keys : (Need to learn more about it) Regarding Set it helps us to store the keys. (But will see more in prectical). */
Editor Settings
Theme
Key bindings
Full width
Lines