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

preview_player
Показать описание
Hi guys, In this video, we're going to learn how HashMap works internally.

🥳 Join our Telegram Community:

🚀 Follow me on:

💸 Use coupon code ANUJBHAIYA on GeeksforGeeks to avail discounts on courses!

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 .

amitanand
Автор

I am not a beginner. I still find the DSA 1 series very helpful to revise the concepts and learn new things for interviews.

DensonGeorge
Автор

Please add questions for every video of DSA course which would be enough to crack tech giant's for beginners

tech_wizard
Автор

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<K, V> {
private List<Entry<K, V>>[] buckets;
private int capacity;
private int size;

private static class Entry<K, V> {
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<K, V> 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<K, V> 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.

nitishprasadkushwaha
Автор

Thank you bhaiya for this series🌸.i'm really grateful.

mousumi
Автор

best video on hashmaps in whole coder community!

GuruPrasadShukla
Автор

Great, very well and easily explained

Автор

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!

anujpotdar
Автор

thanks for the extremely good content and hardwork.

parthparmar
Автор

U really gr8 bhaiya.Your video hleping me a lots....god bless u 😇

surajdhotre
Автор

I was also asked the internal implementation of this in JPMC interview (technical round).

tanmoydutta
Автор

The videos are too gud... Plz try to maintain consistency in DSA course🙏🙏...

itz_me_imraan
Автор

are Abhi aapka hi videos dekhra tha ARRAYS superb!!

awwush
Автор

Thank you Anuj..you explained very well.

unboxingsillystuffs
Автор

Yeah, It helped to understand the concept.

chaitanyaasati
Автор

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.

CodeCult
Автор

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..🙃❣️

vishaldas
Автор

This video is very Helpful for me Thank you Anuj Bhaiya.

ShankarKumar-kolt
Автор

Thank you Anuj, The Video was helpful.

sathishrajasekar
Автор

The way of explanation is excellent👌👌👌

rameezkhan