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

  Рет қаралды 193,479

Anuj Bhaiya

Anuj Bhaiya

Күн бұрын

Пікірлер
@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 3 жыл бұрын
thanx a lot bro, bohot bohot shukriya bhaijan,,,maine noten kr lia hai
@frankfernandes718
@frankfernandes718 3 жыл бұрын
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 2 жыл бұрын
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.
@AI_for_funn
@AI_for_funn Жыл бұрын
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.
@AI_for_funn
@AI_for_funn Жыл бұрын
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
@DensonGeorge18
@DensonGeorge18 3 жыл бұрын
I am not a beginner. I still find the DSA 1 series very helpful to revise the concepts and learn new things for interviews.
@GuruPrasadShukla
@GuruPrasadShukla 2 жыл бұрын
best video on hashmaps in whole coder community!
@nandabawane9117
@nandabawane9117 3 жыл бұрын
No wondor.... Your Videos are always be useful...
@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 ♥️
@tanmoydutta5846
@tanmoydutta5846 2 жыл бұрын
I was also asked the internal implementation of this in JPMC interview (technical round).
@chetantailor3620
@chetantailor3620 3 жыл бұрын
Another worth 16 minutes 👌💫
@awwush
@awwush 3 жыл бұрын
are Abhi aapka hi videos dekhra tha ARRAYS superb!!
@Vishal-242
@Vishal-242 3 жыл бұрын
Most clear explanation I have seen
@itsShashwat
@itsShashwat 3 жыл бұрын
Great video @Anuj Bhaiya
@surajdhotre2889
@surajdhotre2889 3 жыл бұрын
U really gr8 bhaiya.Your video hleping me a lots....god bless u 😇
@factsEcho--
@factsEcho-- 3 жыл бұрын
Yes video was helpful 🙂🙂
@vivekshokeen1192
@vivekshokeen1192 3 жыл бұрын
YES, IT WAS HELPFUL! bhaiya
@ShankarKumar-ko8lt
@ShankarKumar-ko8lt 2 жыл бұрын
This video is very Helpful for me Thank you Anuj Bhaiya.
@tsp57
@tsp57 3 ай бұрын
Beautifully exlpained!
@shivisharma4267
@shivisharma4267 3 жыл бұрын
Bhaiya please make a video on roadmap to cloud computing🙏🙏🙏
@rameezkhan2220
@rameezkhan2220 2 жыл бұрын
The way of explanation is excellent👌👌👌
@gmanikantasai3655
@gmanikantasai3655 8 ай бұрын
Awesome explanation brother💯💯
@AjayKumar-tl1qy
@AjayKumar-tl1qy 2 жыл бұрын
Calculating hash and index number are two different things while putting value in the hash map. for index calculation hash map uses (n-1) & hash where n is bucket length. its never uses hash as index number.
@sachingl4266
@sachingl4266 2 жыл бұрын
Thanks
@unboxingsillystuffs4920
@unboxingsillystuffs4920 2 жыл бұрын
Thank you Anuj..you explained very well.
@sonam3531
@sonam3531 2 жыл бұрын
Thanks , it was good information.
@sumeet115
@sumeet115 Жыл бұрын
Great explanation. Thanks 👍 ..
@ashwithchandra2622
@ashwithchandra2622 2 жыл бұрын
thanks bhayya nice explanation😘
@agyaani8060
@agyaani8060 3 жыл бұрын
Thankuu so much bhaiya❤❤
@aishapi6320
@aishapi6320 10 ай бұрын
Awesome content and presentation
@shubhammahawar6206
@shubhammahawar6206 2 жыл бұрын
Thanks for explaining.
@antorsaha6065
@antorsaha6065 3 жыл бұрын
Thank you bhaiya for this series. This is most useful dsa series.😍
@AnujBhaiya
@AnujBhaiya 3 жыл бұрын
You're welcome ☺️
@venkatasaikrishnamarri1158
@venkatasaikrishnamarri1158 2 жыл бұрын
perfect tutorial
@sathishrajasekar1155
@sathishrajasekar1155 3 жыл бұрын
Thank you Anuj, The Video was helpful.
@richasharma6361
@richasharma6361 3 жыл бұрын
Hello bhaiya, bhaiya bug bounting ke uper video bhi banye with practical knowledge
@chaitanyaasati
@chaitanyaasati 3 жыл бұрын
Yeah, It helped to understand the concept.
@AbhayKumar-lb6fl
@AbhayKumar-lb6fl 3 жыл бұрын
Yes, it was helpful.
@kalpeshsaubhri
@kalpeshsaubhri Жыл бұрын
Bhai end me jo BST wala concept btaya usse maja aa gya.
@acoustic_ng8706
@acoustic_ng8706 3 жыл бұрын
Very helpful ! Thank you Anuj bhai !
@gauravkhare1836
@gauravkhare1836 4 ай бұрын
Yes, it is very helpful
@8750795655
@8750795655 2 жыл бұрын
Great, very well and easily explained
@mousumi721
@mousumi721 3 жыл бұрын
Thank you bhaiya for this series🌸.i'm really grateful.
@rajanwalia4tech
@rajanwalia4tech 3 жыл бұрын
I didn't knew about that java8 implement hashmap using self balancing BST. Yes video was helpful.
@devendersinghrathore8837
@devendersinghrathore8837 2 жыл бұрын
Yes. It is really helpful.
@ashutoshpandey4171
@ashutoshpandey4171 3 жыл бұрын
Thanks for the effort, anuj bhaiya
@kashisharora1018
@kashisharora1018 2 жыл бұрын
Very nice explanation
@raghavendraraviteja1055
@raghavendraraviteja1055 2 жыл бұрын
Thanks, it is helpful
@ZSHADOW18
@ZSHADOW18 3 жыл бұрын
16:24 the video was not helpful but DAMM HELPFUL !
@er.skelafahmed
@er.skelafahmed 3 жыл бұрын
Yes bhaiya its really helpful thank you 😀
@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.
@nishantaggarwal830
@nishantaggarwal830 3 жыл бұрын
It was very interesting and helpful
@dharmeshkhasiya6737
@dharmeshkhasiya6737 2 жыл бұрын
Thanks for explaining
@mruduladdipalli5417
@mruduladdipalli5417 2 жыл бұрын
EK Number Bhai
@jiteshjs99
@jiteshjs99 4 ай бұрын
Great explanation :)
@learntocode8000
@learntocode8000 3 жыл бұрын
Yes, It was Helpful
@rahulsrivastava1040
@rahulsrivastava1040 3 жыл бұрын
Anuj bhaiya op❣️❣️
@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 🤗
@YashvardhanBaid
@YashvardhanBaid Жыл бұрын
Amazing explanation, thanks man!
@ritikarai1232
@ritikarai1232 3 жыл бұрын
Yes it was helpful😁
@stith_pragya
@stith_pragya Жыл бұрын
Thank You So Much bhaiya,............🙏🙏🙏
@jainikprajapati1632
@jainikprajapati1632 2 жыл бұрын
Yes it is useful 👍 thanks 😀
@madhusmitamishra7801
@madhusmitamishra7801 3 жыл бұрын
Watched this before 15mins of my interview and interviewer asked the same question. Thank you ☺️.
@mrityunjoybarman9098
@mrityunjoybarman9098 3 жыл бұрын
You always make helpful videos vaiya...
@RupeshYadav-kt5dv
@RupeshYadav-kt5dv Жыл бұрын
Good job 👏 👍 👌
@TheCultureCraze23
@TheCultureCraze23 3 жыл бұрын
great bhaiya
@keshav19_20
@keshav19_20 3 жыл бұрын
Nice bhaiya Bhaiya linked list kb se chalu ho gi??
@meetsoni1938
@meetsoni1938 2 жыл бұрын
Yes it is helpful 😃
@swati0909
@swati0909 3 ай бұрын
Awesome 👍
@surajchavan1000
@surajchavan1000 2 жыл бұрын
Yes, very helpful
@PAWANKUMAR-el2zk
@PAWANKUMAR-el2zk 3 жыл бұрын
Bhaiya make more such kind of videos on all collection's or tree👍🏻
@shashwatmishra7056
@shashwatmishra7056 Жыл бұрын
A guy from Amazon Seattle, Washington, interviewed me today asked me the same concept
@Naveenkumar-qs1qd
@Naveenkumar-qs1qd 3 ай бұрын
sandar jabardast jinda baad
@abilashappat9640
@abilashappat9640 3 жыл бұрын
Very helpful indeed
@surajwaghmare4653
@surajwaghmare4653 Жыл бұрын
Very Helpful
@AhamedKabeer-wn1jb
@AhamedKabeer-wn1jb 3 жыл бұрын
Thank you Bhaiya..
@anupamdubey5736
@anupamdubey5736 3 жыл бұрын
😯😯Where's that CAP/BEANIE! Loved the explanation!!❤❤
@ranjeet5806
@ranjeet5806 2 жыл бұрын
Great explanation, really helpful.
@nitin5865
@nitin5865 3 жыл бұрын
Perfectly explained 👍
@anuj5927
@anuj5927 2 жыл бұрын
Yes it was helpful :)
@shashwatsharma9904
@shashwatsharma9904 3 жыл бұрын
next video pls make on working of hashset
@nileshmutthe5457
@nileshmutthe5457 2 жыл бұрын
This was helpful
@nandiniverma5273
@nandiniverma5273 2 жыл бұрын
It was helpful , Thankyou for deep Knowledge
@itz_me_imraan02
@itz_me_imraan02 3 жыл бұрын
The videos are too gud... Plz try to maintain consistency in DSA course🙏🙏...
@codehustler8582
@codehustler8582 2 жыл бұрын
YES IT HELPS
@thebugger7269
@thebugger7269 Жыл бұрын
Sir please make a video on hash mam and concurrent hash map how perform different internally
@arre_amay
@arre_amay 3 жыл бұрын
maza aa gaya bhai
@harshitsrivastava9903
@harshitsrivastava9903 3 жыл бұрын
Thanks a lot !!
@deev1032
@deev1032 2 жыл бұрын
very helpful
@av98
@av98 10 ай бұрын
Helpful
@sumitrana2628
@sumitrana2628 3 жыл бұрын
Very 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.
@AdityaKumar-ic8wz
@AdityaKumar-ic8wz 3 жыл бұрын
Make a video for beginners On how to become a free lancer
@CHALLENGESTORY
@CHALLENGESTORY Жыл бұрын
2:05 ye sare k sare operation aapke "Big O of 1" bola kya?
@ankitdubey6813
@ankitdubey6813 3 жыл бұрын
This series is the most useful ❤
@himanshusinghparmar5816
@himanshusinghparmar5816 2 жыл бұрын
yes it was helpful
@samridhisinha8225
@samridhisinha8225 3 жыл бұрын
too good bhaiya :)
@parthparmar9798
@parthparmar9798 2 жыл бұрын
thanks for the extremely good content and hardwork.
@superbudy8338
@superbudy8338 3 жыл бұрын
very helpful thanku for making these kind of videos :)
@debasispaul4410
@debasispaul4410 2 жыл бұрын
it was helpful
@tejasbavkar2811
@tejasbavkar2811 2 жыл бұрын
thank you..
@piyushkesharwani1987
@piyushkesharwani1987 3 жыл бұрын
Great one, Please make a video on the Internal working of "HEAP" also.
@shubham6215
@shubham6215 3 жыл бұрын
Thanks
@kunalmishra6869
@kunalmishra6869 3 жыл бұрын
Beautifully explained ❤️
01. Internal Working of HashMap & Java-8 Enhancement
19:11
Ankit Wasankar
Рет қаралды 123 М.
Правильный подход к детям
00:18
Beatrise
Рет қаралды 11 МЛН
VIP ACCESS
00:47
Natan por Aí
Рет қаралды 30 МЛН
How does HashMap internally works | Java Interview Questions in Hindi
17:04
Learn Code With Durgesh
Рет қаралды 107 М.
12. Hashmap Internal Implementation in java  | Hashmap in java | Implementing your HashMap in Java
30:09
Introduction to HashMap & HashTable in Java
1:39:46
Kunal Kushwaha
Рет қаралды 125 М.
Правильный подход к детям
00:18
Beatrise
Рет қаралды 11 МЛН