Map
- Map is a collection of key value pair.
- By default, a map has 16 buckets
- After threshold value default of which is 12, the size of map automatically increases
- Using hash function We can get the corresponding bucket hash where the data is to be stored.
- If we add null key, then data will go in the first bucket.
- In Java 8 hash map replaces linked list with a binary tree When the number of elements in a bucket, reach certain threshold.
- While converting the list to binary tree, hash code is used as a branching variable. If there are two different hash codes In the same bucket, one is considered bigger and goes to left of tree and Other goes to right of tree.
- Maps use an array of buckets.
- Each bucket has a linked list or binary tree.
- There are four classes which implement map interface
- Hash map
- Linked hash map
- Hash table
- Tree HashMap
- Entry is the interface inside map Which refers to each of the entries in map
- Entryset method in map Interface gives a set of entries.
Types of Maps
- Concurrent hash map
- Introduced in Java 1.5.
- Thread, safe, multiple threads are allowed to operate.
- One thread can lock a bucket or segment at a time.
- Fast performance
- Null key and values are not allowed.
- Locking is done at segment level instead of locking entire object.
- Iterator is fail safe, If we try to read while changing the structure of object, it does not throw any exception.
- It is used in multi threaded environment.
- We will get thread safety without locking the total map object. Just do a bucket level lock.
- At a time, multiple threads are allowed to operate on map objects safely.
- Read operation can be performed without lock, but write operation can be performed with bucket level lock.
- While one thread is reading map objects, the other thread is allowed to modify the map and we won’t get concurrent modification exception.
- Iterator of concurrent hash map is fail safe And won’t raise Concurrent modification exception.
- Hash map
- Hash map is not thread safe.
- Multiple threads can operate simultaneously.
- Contains one null key and multiple null values.
- Hash map is used in single thread environment.
- Hash map was introduced in 1.2 version
- It is not thread safe and unsynchronised.
- It is fast.
- It works with single thread.
- Insertion order is not maintained
- Hash map contains 16 buckets by default, that is 0-15
- Each bucket is a link list of nodes Which contains a key, value, hash And next variable.
- If key is null, entry is placed in the zero bucket.
- We calculate the hash key using mod of Number of buckets and ID. Based on which we assign the value to a hash bucket.
- If there is collision that is hash key of two entries Is same, we add the new entry as next node To current node In hash key or bucket.
- In Java 8 Under certain conditions, link list is converted to balanced tree
- For example, a bucket contains five nodes And then there is a hashing collision on the linked list of that bucket, then it will turn in Balance tree.
- Hash table
- Thread safe Only one thread is allowed at a time.
- Slow performance
- Null key and values are not allowed.
- Iterator is fail first If we modify while it throws concurrent modification exception.
- We will get thread safety by locking the whole map object.
- At a time, one thread is allowed to operate on a map object.
- Every read and write operations required, total map object
- While one thread is iterating map object, the other threads are not allowed to modify the map. Otherwise, we will get concurrent modification exception.
- Iterator of hash table is fail fast, and it will raise concurrent modification exception.
- Null is not allowed for both keys and values.
- Introduced in Java 1.0 version.
- Insertion order is not maintained.
- Synchronised map
- Thread safe and synchronised I.e. Only one thread can operate at a time.
- Slow in performance
- Null key And multiple null values are allowed.
- We use Collections. SynchroniseMap() to synchronise.
- We will get thread safety by locking the whole map object.
- Works with multiple threads but At a time, only one thread is allowed to perform any operation on map object.
- Every we and wide operation operations required total map object
- While one thread is it map object, the other threads are not allowed to modify the map. Otherwise, we will get concurrent modification exception.
- Iterator of synchronised map is fail fast and it will raise concurrent modification exception.
- Null is allowed for both keys and values
- Introduced in Java 1.2 version
- Linked hash map
- Insertion order is fixed and maintained.
- Tree Hash map
- Elements are pre sorted.
No comments:
Post a Comment