Hash Map

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Java / Q61

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

ONE NULL KEY AND


MULTIPLE NULL VALUES
4. Ordering

The order of the elements in a


HashMap is not guaranteed

If you need to maintain the order of


insertion, you can use a
LinkedHashMap
4. Ordering
The order of the elements in a
HashMap is not guaranteed. If you need
to maintain the order of insertion, you
can use a LinkedHashMap
EXAMPLE
4. Ordering
The order of the elements in a HashMap
is not guaranteed. If you need to
maintain the order of insertion, you can
use a LinkedHashMap

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.

Note that this implementation 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.

Map m = Collections.synchronizedMap
(new HashMap(...));
6. fail-fast iterators

The iterators returned by Hashmap


class are fail-fast: if the map is
structurally modified at any time after
the iterator is created, in any way
except through the iterator's own
remove method, the iterator will throw
a ConcurrentModificationException

Thus, in the face of concurrent


modification, the iterator fails quickly
and cleanly, rather than risking arbitrary,
non-deterministic behavior at an
undetermined time in the future
7. Performance

HashMap provides constant-time


performance O(1) for most operations
like add(), remove() and contains().

The performance may degrade if the


hash codes of the keys collide
frequently, leading to a high number of
entries in the same bucket.
Contact me

Interview Preparation Website


www.myjavainterview.in
Instagram Account

@backend.interview.preparation
Medium Account
@backend.interview.preparation

Youtube Account

@backend.interview.preparation
Linked In Account

@ravirameshbisht
Telegram Account

@backendinterviewpreparation

You might also like