Hash Map
Hash Map
Hash Map
www.myjavainterview.in
Ravi Bisht
@backend.interview.preparation
Hashmap
In Java, a HashMap is a data structure
that implements the Map interface and
is part of the Java Collections
Framework.
CLASS DEFINITION
public class HashMap<K,V>
extends AbstractMap<K,V>
implements Map<K,V>, Cloneable, Serializable
1. Key-Value Pairs
HashMap is a data structure that stores
key-value pairs. Each key in the
hashmap must be unique, and the key
is used to access its corresponding
value.
KEY
A key is a unique identifier within the
hashmap.
It is used to access the associated value
Keys must be unique, meaning that no
two entries in the hashmap can have
the same key.
VALUE
The value is the data associated with a
specific key.
It could be any data type, such as
numbers, strings, objects, or even other
hashmaps.
2. Uniqueness of Keys
Keys in a HashMap must be unique
If you attempt to add a key-value pair
with a key that already exists in the
map, the new value will overwrite the
existing one
Keys serve as unique identifiers for the
values stored in the hashmap. No two
entries can have the same key
If you attempt to insert a key-value pair
with a key that already exists in the
hashmap, the new value may replace
the existing value associated with that
key
3. Null Values
A HashMap can have a null key and
also allows multiple null values. The
Java HashMap implementation allows
for one null key and multiple null
values
OUTPUT
5. not synchronized
Hashmap is not synchronized. If
multiple threads access a hash map
concurrently, and at least one of the
threads modifies the map structurally, it
must be synchronized externally. The
map c be "wrapped" using the
Collections.synchronizedMap method.
Map m = Collections.synchronizedMap
(new HashMap(...));
6. fail-fast iterators
@backend.interview.preparation
Medium Account
@backend.interview.preparation
Youtube Account
@backend.interview.preparation
Linked In Account
@ravirameshbisht
Telegram Account
@backendinterviewpreparation