Collection interview QA
Collection interview QA
- List:
A List is an ordered collection that allows duplicate elements.
It maintains the insertion order, meaning that the elements are stored and retrieved in the same order
they were added.
- Set:
A Set is an unordered collection that does not allow duplicate elements.
It does not maintain any specific order, and elements are stored and retrieved without any particular
sequence.
Use List when you need to maintain a collection of elements in a specific order, allow duplicate
elements, or access elements by their index.
Use Set when you need to store a collection of unique elements without duplicates, don't care about the
order of elements, or quickly check if an element exists in your collection.
Key differences
- Insertion and deletion: LinkedList is generally faster for insertion and deletion operations, especially
when inserting or deleting elements at arbitrary positions, since only the affected nodes need to be
updated. ArrayList may require shifting all elements after the insertion or deletion point.
- Random access: ArrayList provides faster random access to elements, since it can directly access any
element using its index. LinkedList requires traversing the list from the beginning to access an element at
a specific position.
- Cache performance: ArrayList tends to have better cache performance, since its elements are stored
contiguously in memory.
3. What happen when we declare the List field with the final keyword?
Best practices
- Use final for fields that should not be reassigned, but may still need to be modified (e.g., a List that is
initialized once and then modified).
4. How can i write own Custom ArrayList where I don't want to allow Duplicates ?
Just by overriding ArrayList's add method and provide own impl. as shown below
Any implementation of Set interface uses put() method of Map to store the element as a key in the Map
and key is always unique in Map.
- HashSet implementation:
The HashSet implementation, which is a common implementation of the Set interface, uses a hash table
to store its elements. When you try to add an element that is already present, the hash table will simply
ignore it.
Comparable Interface
Purpose:
The Comparable interface is used to define the natural ordering of objects of a class. A class that
implements Comparable can compare its instances with one another using the compareTo() method.
Purpose:
The Comparator interface is used to define a custom ordering of objects. You can use it when you want
to sort objects based on multiple or different attributes.
Where it's used:
It is used when you want to define multiple ways of sorting objects, or when you don't want to modify
the class itself (i.e., when you can't modify the class to implement Comparable).
Use Comparable when:
- The class has a natural ordering (like age, or alphabetical order).
- You only need to define one way to compare objects.
6. What is the difference between fail fast and fail safe interator?
Example :
in case of CopyOnWriteArrayList a clone copy of collection and any update happen to that clone copy
instead of actual collection.
7. what is the need of ConcurrentHashMap and it is different from HashMap?
ConcurrentHashMap is a thread-safe, highly efficient implementation of the Map interface that allows
concurrent access and modification by multiple threads, and specifically designed for scenarios where
multiple threads need to read and write to the map concurrently without causing issues like data
inconsistency or thread contention.
Locking: HashMap does not have locking and relies on external synchronization. On the other hand,
ConcurrentHashMap uses a segment-based locking mechanism, allowing high concurrency by locking
only a portion of the map, reducing contention.
Null Values: HashMap allows null keys and values, while ConcurrentHashMap does not allow null for
either keys or values. This restriction ensures that null values cannot cause ambiguity in concurrent
environments.
Example:
Globally declaring a map and on a seperate thread I am adding an element to this map,
and in main thread I am add few for elements to map and iterating the map
will get ConcurrentModificationException in case of HashMap
but with ConcurrentHashMap will allow us to add and iterate the map at same time
8. If we have HashTable which is already synchronized then why we need
ConcurrentHashMap?
Locking Mechanism:
Hashtable:
In Hashtable, the entire map is locked during each read and write operation. This means that when one
thread is accessing or modifying the map, no other thread can access or modify it, even if they are
working on different keys or values.
This is a global lock on the entire map, which results in a significant performance bottleneck under high
concurrency. Only one thread can access the map at a time, even for operations that are independent
(e.g., reading or writing different keys).
ConcurrentHashMap:
ConcurrentHashMap uses a segment-based locking mechanism (in earlier versions, it divided the map
into segments, and each segment could be locked independently). In modern versions of
ConcurrentHashMap, it uses bucket-level locking or lock striping (using finer-grained locks).
This means that multiple threads can concurrently access and modify different parts of the map
simultaneously without blocking each other. For example, one thread could be reading from one bucket,
while another thread is modifying a different bucket.
9. We can Synchronize a HashMap using Collections then why can not we use that instead
using ConcurrenHashMap?
ConcurrentHashMap
ConcurrentHashMap, on the other hand, is a thread-safe implementation of a map that allows multiple
threads to access and modify the map concurrently.
Key differences
The key differences between synchronizing a HashMap using Collections and using a
ConcurrentHashMap are:
- Locking mechanism: Synchronizing a HashMap using Collections uses a single lock for the entire map,
whereas ConcurrentHashMap uses a segmented locking mechanism, which allows multiple threads to
access different segments of the map concurrently.
- Performance: ConcurrentHashMap is generally faster and more efficient than synchronizing a HashMap
using Collections, especially in multi-threaded environments.
10. How HashMap internally works?
A HashMap uses an array of buckets, where each bucket is essentially a linked list.
When you put a key-value pair in a HashMap, it calculates a hash code of the key using Object's class
hashCode method, then maps this hash code to an index in the array (bucket) using the formula:
index = (n - 1) & hash
Example:
creating a HashMap of Employee and putting e1 object to map then HashCode calculated to 6 so this
entry put into 6th index in map
Collision Handling:
Collisions occur when two keys have the same index. HashMap handles collisions using:
Note: can we have null key in HashMap and if yes then what will be the hashcode for that ?
yes we can store single null as a key and hashcode for that will be 0.
Resizing (Rehashing):
When the number of entries exceeds the load factor (default 0.75), the HashMap doubles its
capacity and rehashes all entries to new buckets. This is an expensive operation.
get() Operation:
HashMap calculates the hash of the key.
Maps it to the bucket index.
Traverses the bucket (LinkedList or Tree) to find the correct key.
Returns the associated value.
3. What will happen if you try to store a value with a key that is already present in
HashMap?
If you store an existing key in HashMap, it will overwrite the old value with the new
value, and the put method will return the old value.
10. What will happen if you use HashMap in a multi-threaded Java application?
If you use HashMap in a multi-threaded environment, the internal data structure of
HashMap may get corrupted, and the data might be inconsistent. You can either use
ConcurrentHashMap and Collections.synchronizedMap()
11. What are the different ways to iterate over HashMap?
You can iterate over HashMap using keySet(), entrySet(), and forEach() methods.
2. Using keySet()
12. How do you remove a mapping while iterating over HashMap in Java?
You can use the remove() method to remove a key-value pair while iterating over
HashMap.
18. What is the difference between the capacity and size of HashMap?
Capacity refers to the number of buckets in the HashMap, while size refers to the
number of key-value pairs currently stored.
19. What will happen if two different keys of HashMap return the same hash code?
If two different keys return the same hash code, a collision will occur, and the colliding
elements will be stored in a linked list or a balanced binary tree.
TreeMap is implemented using a Red-Black Tree, which ensures O(log n) time complexity for all major
operations like get(), put(), remove().