Hashmap in Java | Internal Working of Hashmap in Java | Hashmap Implementation | DSA-One Course #30

  Рет қаралды 172,454

Anuj Bhaiya

Anuj Bhaiya

3 жыл бұрын

Hi guys, In this video, we're going to learn how HashMap works internally.
🥳 Join our Telegram Community:
Telegram channel: telegram.me/realanujbhaiya
Telegram group: telegram.me/dsa_one
🚀 Follow me on:
Instagram: / anuj.kumar.sharma
Linkedin: / sharma-kumar-anuj
Twitter: / realanujbhaiya
💸 Use coupon code ANUJBHAIYA on GeeksforGeeks to avail discounts on courses!
📚 Complete DSA Playlist: • DSA-One Course - The C...
Complete Android Development Playlist: • Android Development Tu...
Hashtags:
#anujbhaiya #dsaone
Tags:
hashmap in java
internal working of hashmap in java
hashmap internal implementation in java
how hashmap works internally in java
hashmap
hashmap java 8
internal working of hashmap
hashmap java
map in java
hashmap internal working in java
hashmap internal working
anuj bhaiya
hash map
hashing in java
java hashmap
hashmap implementation
working of hashmap
hashmap working
what is hashmap
internal implementation of hashmap
hash map in java
hashmaps in java
maps in java
hashmaps
hashmap in java in hindi
hashing
design hashmap
hashing in data structure
hash maps
internal working of collections in java
hashmap anuj bhaiya
mapping in java
hash map java
java 8
hash map internal working
hashmap in java apna college
what is hashmap in java
hashmap implementation in java
hashtable in java
java map
map java
internal working of hashset
map interface in java
hashing java
hashmap in c++
how hashmap works
hashmap interview questions
hash table in java
implement hashmap
java
what is hash map
anuj bhaiya java
dsa
hashset in java
hash map in c++
hashing in c++
hashset internal implementation in java
hash table in data structure
how hashmap works internally
java 8 features
hashmap c++
hashmap vs hashtable
implementation of hashmap
hash table
hashmap working in java
hashmaps java
internal working of hashmap in java in hindi
internal working of hashset in java
maps java
what is a hashmap

Пікірлер: 201
@amitanand3672
@amitanand3672 3 жыл бұрын
Few other questions I have come across which everyone should prepare beforehand: 1. What is bucketing in hashmap ? 2. What is loadfactor ? 3. Why hashmap is not recommended in a multi threaded environment ? 4. Implement a hashmap using custom class as key 5. is it mandatary to override equals and hashcode method ? What will happen if not overridden ? 6. What is the difference between hashmap and concurrent hashmap also explain the working of concurrent hashmap .
@mohammadfaizanhashmi4213
@mohammadfaizanhashmi4213 2 жыл бұрын
thanx a lot bro, bohot bohot shukriya bhaijan,,,maine noten kr lia hai
@frankfernandes718
@frankfernandes718 2 жыл бұрын
woh jo table tha red me 9:57 woh bucket hai
@pankajchaudhari2413
@pankajchaudhari2413 2 жыл бұрын
Bucketing is nothing but index of that node Array Hashmap doesn't contain synchronized method or variable , that's y it is not thread safe, overcome to resolve this problem we are having HashTable (as it contains all synchronized method)
@Basukinathkr
@Basukinathkr Жыл бұрын
Great Amit. For people who are looking here for the answers - Buckets are basically the area that contains values. Loadfactor is the threshold for the percentage of the bucket when it is decided that now bucket is going to be full and then the capacity is doubled. By default, loadfactor is 75% and capacity is 16. Totally configurable via constructor. The reason a hashmap isn't recommended in a MT env because when multiple threads start to access the data while some thread is updating it, there can be issues. Whenever overriding equals, it is mandatory to override hashCode so that every time when equals is called, the values generated by hashCode remain consistent. Rest 4 and 6 are explanatory. Can be looked on internet. Easy stuff.
@nitishprasadkushwaha
@nitishprasadkushwaha Жыл бұрын
Bucketing in HashMap is the process of storing multiple values in a single location (bucket) of the internal array of the HashMap, which is indexed using a hash function. The hash function calculates the hash code of the key and uses it as an index in the array. If multiple keys have the same hash code, they are stored in the same bucket as linked nodes. Load factor is a measure of how full a HashMap is allowed to get before its capacity is automatically increased. It is a float value that ranges from 0.0 to 1.0. When the number of entries in the HashMap exceeds the load factor multiplied by the current capacity, the capacity is increased, and the entries are rehashed. HashMap is not recommended in a multi-threaded environment because it is not thread-safe. If multiple threads access a HashMap concurrently, it may result in an inconsistent state of the HashMap, leading to data loss or unexpected results. ConcurrentHashMap is recommended for use in a multi-threaded environment. Here's an example implementation of a HashMap using a custom class as a key: class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } // getters and setters @Override public int hashCode() { return Objects.hash(name, age); } @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Person)) return false; Person person = (Person) o; return age == person.age && Objects.equals(name, person.name); } } class CustomHashMap { private List[] buckets; private int capacity; private int size; private static class Entry { K key; V value; public Entry(K key, V value) { this.key = key; this.value = value; } // getters and setters } public CustomHashMap(int capacity) { this.buckets = new List[capacity]; this.capacity = capacity; this.size = 0; } public void put(K key, V value) { int index = key.hashCode() % capacity; if (buckets[index] == null) { buckets[index] = new LinkedList(); } for (Entry entry : buckets[index]) { if (entry.key.equals(key)) { entry.value = value; return; } } buckets[index].add(new Entry(key, value)); size++; } public V get(K key) { int index = key.hashCode() % capacity; if (buckets[index] == null) { return null; } for (Entry entry : buckets[index]) { if (entry.key.equals(key)) { return entry.value; } } return null; } public int size() { return size; } // other methods } It is highly recommended to override the equals() and hashCode() methods when using custom objects as keys in a HashMap. If these methods are not overridden, the default implementations in the Object class are used, which compares object references rather than object contents. This can lead to unexpected behavior, where two objects that are considered equal by their contents are not considered equal by the HashMap. As a result, the HashMap may not be able to retrieve the values correctly.
@DensonGeorge18
@DensonGeorge18 2 жыл бұрын
I am not a beginner. I still find the DSA 1 series very helpful to revise the concepts and learn new things for interviews.
@nitishprasadkushwaha
@nitishprasadkushwaha Жыл бұрын
Bucketing in HashMap is the process of storing multiple values in a single location (bucket) of the internal array of the HashMap, which is indexed using a hash function. The hash function calculates the hash code of the key and uses it as an index in the array. If multiple keys have the same hash code, they are stored in the same bucket as linked nodes. Load factor is a measure of how full a HashMap is allowed to get before its capacity is automatically increased. It is a float value that ranges from 0.0 to 1.0. When the number of entries in the HashMap exceeds the load factor multiplied by the current capacity, the capacity is increased, and the entries are rehashed. HashMap is not recommended in a multi-threaded environment because it is not thread-safe. If multiple threads access a HashMap concurrently, it may result in an inconsistent state of the HashMap, leading to data loss or unexpected results. ConcurrentHashMap is recommended for use in a multi-threaded environment. Here's an example implementation of a HashMap using a custom class as a key: class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } // getters and setters @Override public int hashCode() { return Objects.hash(name, age); } @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Person)) return false; Person person = (Person) o; return age == person.age && Objects.equals(name, person.name); } } class CustomHashMap { private List[] buckets; private int capacity; private int size; private static class Entry { K key; V value; public Entry(K key, V value) { this.key = key; this.value = value; } // getters and setters } public CustomHashMap(int capacity) { this.buckets = new List[capacity]; this.capacity = capacity; this.size = 0; } public void put(K key, V value) { int index = key.hashCode() % capacity; if (buckets[index] == null) { buckets[index] = new LinkedList(); } for (Entry entry : buckets[index]) { if (entry.key.equals(key)) { entry.value = value; return; } } buckets[index].add(new Entry(key, value)); size++; } public V get(K key) { int index = key.hashCode() % capacity; if (buckets[index] == null) { return null; } for (Entry entry : buckets[index]) { if (entry.key.equals(key)) { return entry.value; } } return null; } public int size() { return size; } // other methods } It is highly recommended to override the equals() and hashCode() methods when using custom objects as keys in a HashMap. If these methods are not overridden, the default implementations in the Object class are used, which compares object references rather than object contents. This can lead to unexpected behavior, where two objects that are considered equal by their contents are not considered equal by the HashMap. As a result, the HashMap may not be able to retrieve the values correctly.
@tech_wizard9315
@tech_wizard9315 3 жыл бұрын
Please add questions for every video of DSA course which would be enough to crack tech giant's for beginners
@mousumi721
@mousumi721 3 жыл бұрын
Thank you bhaiya for this series🌸.i'm really grateful.
@GuruPrasadShukla
@GuruPrasadShukla Жыл бұрын
best video on hashmaps in whole coder community!
@8750795655
@8750795655 2 жыл бұрын
Great, very well and easily explained
@anujpotdar3529
@anujpotdar3529 2 жыл бұрын
I was asked this yesterday, could not answer. I started searching for solutions, most of them were too long and difficult to understand. This one taught the concept very well, keep such awesome videos, and more power to you Anuj!
@AnujBhaiya
@AnujBhaiya 2 жыл бұрын
Thanks Anuj ♥️
@parthparmar9798
@parthparmar9798 2 жыл бұрын
thanks for the extremely good content and hardwork.
@unboxingsillystuffs4920
@unboxingsillystuffs4920 Жыл бұрын
Thank you Anuj..you explained very well.
@surajdhotre2889
@surajdhotre2889 3 жыл бұрын
U really gr8 bhaiya.Your video hleping me a lots....god bless u 😇
@ranjeet5806
@ranjeet5806 Жыл бұрын
Great explanation, really helpful.
@nitin5865
@nitin5865 3 жыл бұрын
Perfectly explained 👍
@sathishrajasekar1155
@sathishrajasekar1155 2 жыл бұрын
Thank you Anuj, The Video was helpful.
@tanmoydutta5846
@tanmoydutta5846 Жыл бұрын
I was also asked the internal implementation of this in JPMC interview (technical round).
@ashutoshpandey4171
@ashutoshpandey4171 2 жыл бұрын
Thanks for the effort, anuj bhaiya
@ShankarKumar-ko8lt
@ShankarKumar-ko8lt 2 жыл бұрын
This video is very Helpful for me Thank you Anuj Bhaiya.
@rameezkhan2220
@rameezkhan2220 2 жыл бұрын
The way of explanation is excellent👌👌👌
@awwush
@awwush 3 жыл бұрын
are Abhi aapka hi videos dekhra tha ARRAYS superb!!
@user-ij9zq9qf8s
@user-ij9zq9qf8s 11 ай бұрын
Amazing explanation, thanks man!
@superbudy8338
@superbudy8338 3 жыл бұрын
very helpful thanku for making these kind of videos :)
@acoustic_ng8706
@acoustic_ng8706 3 жыл бұрын
Very helpful ! Thank you Anuj bhai !
@shivisharma4267
@shivisharma4267 3 жыл бұрын
Bhaiya please make a video on roadmap to cloud computing🙏🙏🙏
@antorsaha6065
@antorsaha6065 2 жыл бұрын
Thank you bhaiya for this series. This is most useful dsa series.😍
@AnujBhaiya
@AnujBhaiya 2 жыл бұрын
You're welcome ☺️
@sumeet115
@sumeet115 Жыл бұрын
Great explanation. Thanks 👍 ..
@chaitanyaasati
@chaitanyaasati 2 жыл бұрын
Yeah, It helped to understand the concept.
@itz_me_imraan02
@itz_me_imraan02 3 жыл бұрын
The videos are too gud... Plz try to maintain consistency in DSA course🙏🙏...
@itsShashwat
@itsShashwat 3 жыл бұрын
Great video @Anuj Bhaiya
@nandiniverma5273
@nandiniverma5273 Жыл бұрын
It was helpful , Thankyou for deep Knowledge
@shashwatmishra7056
@shashwatmishra7056 8 ай бұрын
A guy from Amazon Seattle, Washington, interviewed me today asked me the same concept
@chetantailor3620
@chetantailor3620 3 жыл бұрын
Another worth 16 minutes 👌💫
@CodeCult413
@CodeCult413 2 жыл бұрын
Excellent explanation bhai. Bhai, on which basis an initial length of hash table get decided?? Is it same like an internal working of arraylist for initial length of hashtable or something else?? And bhai, please increase the frequency of videos on DSA-ONE. I'm eagerly waiting for upcoming videos.
@PAWANKUMAR-el2zk
@PAWANKUMAR-el2zk 3 жыл бұрын
Bhaiya make more such kind of videos on all collection's or tree👍🏻
@nishantaggarwal830
@nishantaggarwal830 2 жыл бұрын
It was very interesting and helpful
@vishaldas5692
@vishaldas5692 3 жыл бұрын
Yes it was helpful btw plzz make a video on how to convert that linked list into bst in hashed map as u said is done in Java 8..🙃❣️
@stith_pragya
@stith_pragya 10 ай бұрын
Thank You So Much bhaiya,............🙏🙏🙏
@dharmeshkhasiya6737
@dharmeshkhasiya6737 2 жыл бұрын
Thanks for explaining
@shubhammahawar6206
@shubhammahawar6206 2 жыл бұрын
Thanks for explaining.
@mrityunjoybarman9098
@mrityunjoybarman9098 3 жыл бұрын
You always make helpful videos vaiya...
@devendersinghrathore8837
@devendersinghrathore8837 Жыл бұрын
Yes. It is really helpful.
@skelafahmed
@skelafahmed 2 жыл бұрын
Yes bhaiya its really helpful thank you 😀
@gmanikantasai3655
@gmanikantasai3655 2 ай бұрын
Awesome explanation brother💯💯
@vivekshokeen1192
@vivekshokeen1192 2 жыл бұрын
YES, IT WAS HELPFUL! bhaiya
@madhusmitamishra7801
@madhusmitamishra7801 2 жыл бұрын
Watched this before 15mins of my interview and interviewer asked the same question. Thank you ☺️.
@abilashappat9640
@abilashappat9640 2 жыл бұрын
Very helpful indeed
@nandabawane9117
@nandabawane9117 2 жыл бұрын
No wondor.... Your Videos are always be useful...
@Vishal-242
@Vishal-242 3 жыл бұрын
Most clear explanation I have seen
@AbhayKumar-lb6fl
@AbhayKumar-lb6fl 3 жыл бұрын
Yes, it was helpful.
@raghavendraraviteja1055
@raghavendraraviteja1055 Жыл бұрын
Thanks, it is helpful
@sonam3531
@sonam3531 2 жыл бұрын
Thanks , it was good information.
@auroshisray9140
@auroshisray9140 Жыл бұрын
thanks bhaiya!
@sandeshtiwaris6829
@sandeshtiwaris6829 2 жыл бұрын
Yes, it was helpful
@thebugger7269
@thebugger7269 8 ай бұрын
Sir please make a video on hash mam and concurrent hash map how perform different internally
@ashapilkhwal6320
@ashapilkhwal6320 4 ай бұрын
Awesome content and presentation
@adityadev2825
@adityadev2825 3 жыл бұрын
Yes the videos was very helpful
@ramgopalbhajans3982
@ramgopalbhajans3982 Жыл бұрын
Yes it was helpful.
@user-gk7xb6bp7c
@user-gk7xb6bp7c 4 ай бұрын
YES, IT WAS HELPFUL
@gurkiratkaur4087
@gurkiratkaur4087 2 ай бұрын
Very helpful
@surajwaghmare4653
@surajwaghmare4653 Жыл бұрын
Very Helpful
@AhamedKabeer-wn1jb
@AhamedKabeer-wn1jb 2 жыл бұрын
Thank you Bhaiya..
@shriduttpatel1350
@shriduttpatel1350 2 жыл бұрын
Thank you @Anuj bhaia. It was really help full. Please do not stop making videos like this😀.
@AnujBhaiya
@AnujBhaiya 2 жыл бұрын
If it was really helpful, I will never stop making videos like this 🤗
@surajchavan1000
@surajchavan1000 Жыл бұрын
Yes, very helpful
@learntocode8000
@learntocode8000 2 жыл бұрын
Yes, It was Helpful
@jainikprajapati1632
@jainikprajapati1632 2 жыл бұрын
Yes it is useful 👍 thanks 😀
@av98
@av98 4 ай бұрын
Helpful
@ashwithchandra2622
@ashwithchandra2622 2 жыл бұрын
thanks bhayya nice explanation😘
@anshukumari6616
@anshukumari6616 Жыл бұрын
Yes, it was useful.
@mruduladdipalli5417
@mruduladdipalli5417 2 жыл бұрын
EK Number Bhai
@StudyiMnimal1743
@StudyiMnimal1743 2 жыл бұрын
great bhaiya
@rrtt6903
@rrtt6903 2 жыл бұрын
yes, it was helpful
@venkatasaikrishnamarri1158
@venkatasaikrishnamarri1158 Жыл бұрын
perfect tutorial
@shreyanshgupta8959
@shreyanshgupta8959 2 жыл бұрын
Yes it was Helpful
@sumitrana2628
@sumitrana2628 3 жыл бұрын
Very helpful 🙂👍🏻
@kashisharora1018
@kashisharora1018 2 жыл бұрын
Very nice explanation
@factsEcho--
@factsEcho-- 3 жыл бұрын
Yes video was helpful 🙂🙂
@agyaani8060
@agyaani8060 3 жыл бұрын
Thankuu so much bhaiya❤❤
@harshitsrivastava9903
@harshitsrivastava9903 2 жыл бұрын
Thanks a lot !!
@swayam2367
@swayam2367 11 ай бұрын
thanks bhai...👍👍
@nileshmutthe5457
@nileshmutthe5457 2 жыл бұрын
This was helpful
@anupamdubey5736
@anupamdubey5736 3 жыл бұрын
😯😯Where's that CAP/BEANIE! Loved the explanation!!❤❤
@rahulshinde6648
@rahulshinde6648 Жыл бұрын
Thank you
@indranilmetiya6972
@indranilmetiya6972 Жыл бұрын
Yes it was helpful
@deev1032
@deev1032 2 жыл бұрын
very helpful
@debasispaul4410
@debasispaul4410 2 жыл бұрын
it was helpful
@surajwaghmare4653
@surajwaghmare4653 Жыл бұрын
Yes It was useful
@tejasbavkar2811
@tejasbavkar2811 2 жыл бұрын
thank you..
@pratikjha2742
@pratikjha2742 2 жыл бұрын
yes it was helpful
@vaibhavsomani5161
@vaibhavsomani5161 3 жыл бұрын
Hash Code is basically the Memory Reference of that object so what is need for doing the hashing by Modulo and all.
@codehustler8582
@codehustler8582 2 жыл бұрын
YES IT HELPS
@rahulsrivastava1040
@rahulsrivastava1040 2 жыл бұрын
Anuj bhaiya op❣️❣️
@ankitdubey6813
@ankitdubey6813 2 жыл бұрын
This series is the most useful ❤
@piyushkesharwani1987
@piyushkesharwani1987 3 жыл бұрын
Great one, Please make a video on the Internal working of "HEAP" also.
@amanrai8010
@amanrai8010 2 жыл бұрын
Thanks
@rajanwalia4tech
@rajanwalia4tech 3 жыл бұрын
I didn't knew about that java8 implement hashmap using self balancing BST. Yes video was helpful.
@meetsoni1938
@meetsoni1938 2 жыл бұрын
Yes it is helpful 😃
@codingwithadesh6935
@codingwithadesh6935 2 жыл бұрын
Osm❤️
@pareeks
@pareeks 2 жыл бұрын
Great video. Two things TBC: 1. New values are pre-appended, not appended at end. 2. In case of collision in get(Key), value is not checked for equals() but key is checked.
@RupeshYadav-kt5dv
@RupeshYadav-kt5dv Жыл бұрын
Good job 👏 👍 👌
@anuj5927
@anuj5927 Жыл бұрын
Yes it was helpful :)
@abhinavmishra7617
@abhinavmishra7617 2 жыл бұрын
helpful
How does HashMap internally works | Java Interview Questions in Hindi
17:04
Learn Code With Durgesh
Рет қаралды 91 М.
Sprinting with More and More Money
00:29
MrBeast
Рет қаралды 191 МЛН
We Got Expelled From Scholl After This...
00:10
Jojo Sim
Рет қаралды 64 МЛН
버블티로 체감되는 요즘 물가
00:16
진영민yeongmin
Рет қаралды 64 МЛН
01. Internal Working of HashMap & Java-8 Enhancement
19:11
WebEncyclop Tutorials
Рет қаралды 102 М.
Java Full stack vs MERN stack, Which one to choose in 2024
14:37
Anuj Bhaiya
Рет қаралды 52 М.
Map and HashMap in Java with Internal Working- Interview Question
19:27
Daily Code Buffer
Рет қаралды 28 М.