Java Interview questions on Collections, Concurrency and Threads
Categories: EDUCATION
Let's look at a few Questions from Collections and Stream now.
Q.1. Difference between List, Set, and Map in Java?
Lists maintain elements in sequential order and allow duplicates (e.g., ArrayList, LinkedList). Sets do not allow duplicates and do not guarantee order (e.g., HashSet, TreeSet). Maps store key-value pairs and do not allow duplicate keys (e.g., HashMap, TreeMap).
Q.2. Difference between synchronized and concurrent collection in Java?
Synchronized collections use explicit locking to achieve thread-safety, allowing only one thread to modify the collection at a time. Concurrent collections use non-blocking algorithms and are designed for high concurrency, allowing multiple threads to modify the collection concurrently without explicit locking.
Q.3. How does the get method of HashMap work in Java?
The get method of HashMap calculates the hash code of the provided key, determines the index in the underlying array based on the hash code, and then searches for the key at that index. If found, it returns the corresponding value; otherwise, it returns null.
Q.4. How is ConcurrentHashMap different from Hashtable? How does it achieve thread-safety?
ConcurrentHashMap allows concurrent access to the map without blocking, while Hashtable uses synchronized methods to achieve thread-safety, resulting in potential performance bottlenecks. ConcurrentHashMap achieves thread-safety by dividing the map into segments, each with its lock, allowing multiple threads to modify different segments concurrently.
Q.5. When to use LinkedList over ArrayList in Java?
Use LinkedList when frequent insertion and deletion operations are required, as LinkedList provides constant-time insertion and deletion at any position. Use ArrayList when random access and iteration are frequent, as ArrayList provides constant-time access by index.
Let's look at a few Questions from Java multithreading and concurrency concepts now
Q.1. How do notify and notifyAll work, and what's the difference between them? Why prefer notifyAll to notify?
Both notify and notifyAll are methods in Java used to wake up threads waiting on a monitor (i.e., waiting to acquire an object's lock). notify wakes up one randomly selected thread, while notifyAll wakes up all waiting threads. notifyAll is preferred because it ensures that all waiting threads are notified, preventing potential indefinite waiting and improving system responsiveness.
Q.2. What is a race condition and how do you avoid it?
A race condition occurs when the outcome of a program depends on the timing or interleaving of multiple threads. To avoid race conditions, you can use synchronization mechanisms like locks, semaphores, or atomic operations to ensure that critical sections of code are executed atomically or only by one thread at a time.
Q.3. What is a deadlock and how do you avoid it?
Deadlock occurs when two or more threads are stuck waiting for each other to release resources that they need to proceed. To avoid deadlock, you can use techniques such as resource ordering, avoiding nested locks, or using timeouts for acquiring locks. Additionally, designing code with a clear and consistent locking order can help prevent deadlocks.