Это видео недоступно.
Сожалеем об этом.
Hashmap in Java | Internal Working of Hashmap in Java | Hashmap Implementation | DSA-One Course #30
HTML-код
- Опубликовано: 28 июн 2021
- 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
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 .
thanx a lot bro, bohot bohot shukriya bhaijan,,,maine noten kr lia hai
woh jo table tha red me 9:57 woh bucket hai
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)
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.
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.
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.
Please add questions for every video of DSA course which would be enough to crack tech giant's for beginners
I am not a beginner. I still find the DSA 1 series very helpful to revise the concepts and learn new things for interviews.
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!
Thanks Anuj ♥️
Another worth 16 minutes 👌💫
Most clear explanation I have seen
I was also asked the internal implementation of this in JPMC interview (technical round).
Yes, it was helpful
Bhaiya please make a video on roadmap to cloud computing🙏🙏🙏
best video on hashmaps in whole coder community!
No wondor.... Your Videos are always be useful...
Yes the videos was very helpful
Yes video was helpful 🙂🙂
Hello bhaiya, bhaiya bug bounting ke uper video bhi banye with practical knowledge
Yes, it was helpful.
Yes it was helpful.
U really gr8 bhaiya.Your video hleping me a lots....god bless u 😇
YES, IT WAS HELPFUL! bhaiya
Great explanation :)
Great video @Anuj Bhaiya
Awesome explanation brother💯💯
Sir please make a video on hash mam and concurrent hash map how perform different internally
Great, very well and easily explained
YES, IT WAS HELPFUL
Watched this before 15mins of my interview and interviewer asked the same question. Thank you ☺️.
Helpful
Thank you bhaiya for this series.
This is most useful dsa series.😍
You're welcome ☺️
are Abhi aapka hi videos dekhra tha ARRAYS superb!!
EK Number Bhai
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.
Thanks
Thanks, it is helpful
This video is very Helpful for me Thank you Anuj Bhaiya.
The way of explanation is excellent👌👌👌
Very Helpful
Yes, it was useful.
perfect tutorial
Yes. It is really helpful.
Very nice explanation
Yes, very helpful
thanks bhaiya!
Very helpful
Great explanation. Thanks 👍 ..
Yes it was Helpful
Bhaiya make more such kind of videos on all collection's or tree👍🏻
Very helpful ! Thank you Anuj bhai !
The videos are too gud... Plz try to maintain consistency in DSA course🙏🙏...
yes, it was helpful
yes it was helpful
Good job 👏 👍 👌
YES IT HELPS
Yeah, It helped to understand the concept.
Awesome content and presentation
I didn't knew about that java8 implement hashmap using self balancing BST.
Yes video was helpful.
Thanks , it was good information.
maza aa gaya bhai
Thank you Anuj..you explained very well.
thanks bhayya
nice explanation😘
Thank you Bhaiya..
Yes, It was Helpful
Nice bhaiya
Bhaiya linked list kb se chalu ho gi??
next video pls make on working of hashset
This was helpful
great bhaiya
Thanks for explaining.
Thankuu so much bhaiya❤❤
Yes it was helpful
Thank You So Much bhaiya,............🙏🙏🙏
Yes It was useful
Thanks for explaining
Yes it is helpful 😃
It was very interesting and helpful
very helpful
Amazing explanation, thanks man!
Thank you
Very helpful indeed
it was helpful
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.
Thanks for the effort, anuj bhaiya
Yes bhaiya its really helpful thank you 😀
Yes it was helpful :)
Great one, Please make a video on the Internal working of "HEAP" also.
Thank you Anuj, The Video was helpful.
thank you..
Yes it is useful 👍 thanks 😀
Thank you @Anuj bhaia. It was really help full. Please do not stop making videos like this😀.
If it was really helpful, I will never stop making videos like this 🤗
Perfectly explained 👍
Thank you bhaiya for this series🌸.i'm really grateful.
Yes it was helpful😁
Thanks
It was helpful , Thankyou for deep Knowledge
A guy from Amazon Seattle, Washington, interviewed me today asked me the same concept
Great explanation, really helpful.
You always make helpful videos vaiya...
too good bhaiya :)
Anuj bhaiya op❣️❣️
Bhai end me jo BST wala concept btaya usse maja aa gya.
😯😯Where's that CAP/BEANIE! Loved the explanation!!❤❤
Thanks a lot !!
This series is the most useful ❤