Saturday, September 21, 2019

Collection Interview Questions (With Java 8 Enhancements) - Part 1

1. Collections and its need?

A collection represents a group of Objects. Prior to JDK1.2 there was no common interface/API to work on group of Objects. Only Vectors and HashTable was there to do some operations, but again there was no consistency between them.So the Java Organization decided to make a commonn interface to solve this problem and it came to know as Collection.

2. Collection Hierarchy



The Collection interface has mainly two sections :

  • 2.1 Collection
    1. Set
    2. List
    3. Queue

  • 2.2 Map
    1. SortedMap
    2. ConcurrentMap



2. The Iterator Interface

The Iterator interface contains iterator() method which is used to obtain iterator on collections.Along with iterator(), it also has forEach(Consumer<? super T> action) and spliterator() method.

3. Collection Interface

The Collection interface contains methods which are common for all the collections like :
add(E e), addAll(Collection<? extends E> ), remove(Object o), removeAll(Collection<?> c), stream(), parallelStream(), contains(Object o) , containsAll(Collection<?> c) etc.

These methods are available for all the sub-interfaces of collection interface.

4.  Why Collection interface does not extend Cloneable and Serializable interface?

Cloneable and Serializable are marker interfaces, which implies that implementing them has a specific meaning and functionality, which in turn is not required to be by default available for all the collections.
So simply, there is no need for collection to implement or extend these interfaces. There are specific collection implementations which implement them as per their specifications.

5.  Why Map interface does not extend Collection interface?

Simply because they are incompatible.

6. What is difference between fail-fast and fail-safe?

7. Difference between Iterator and ListIterator?

8. How to make a collection read only?
Use following methods:

Collections.unmodifiableList(list);
Collections.unmodifiableSet(set);
Collections.unmodifiableMap(map);
These methods takes collection parameter and return a new read-only collection with same elements as in original collection.


9. How to make a collection thread safe?
Use below methods:

Collections.synchronizedList(list);
Collections.synchronizedSet(set);
Collections.synchronizedMap(map);

10. What is BlockingQueue?

11. What is Queue and Stack, list down their differences?
A collection designed for holding elements prior to processing. Besides basic Collection operations, queues provide additional insertion, extraction, and inspection operations.
Queues typically, but do not necessarily, order elements in a FIFO (first-in-first-out) manner.

Stack is also a form of Queue but one difference, it is LIFO (last-in-first-out).

Whatever the ordering used, the head of the queue is that element which would be removed by a call to remove() or poll(). Also note that Stack and Vector are both synchronized.

Usage: Use a queue if you want to process a stream of incoming items in the order that they are received.Good for work lists and handling requests.
Use a stack if you want to push and pop from the top of the stack only. Good for recursive algorithms.

12. What is Comparable and Comparator interface?

13. What are Collections and Arrays classes?

14.  Impact of random/fixed hashcode() value for key
The impact of both cases (fixed hashcode or random hashcode for keys) will have same result and that is “unexpected behavior“. The very basic need of hashcode in HashMap is to identify the bucket location where to put the key-value pair, and from where it has to be retrieved.

If the hashcode of key object changes every time, the exact location of key-value pair will be calculated different, every time. This way, one object stored in HashMap will be lost forever and there will be very minimum possibility to get it back from map.

For this same reason, key are suggested to be immutable, so that they return a unique and same hashcode each time requested on same key object.

15. 

No comments:

Post a Comment