1.What is the List interface?
2.What are the main implementations of the List interface ?
3.What are the advantages of ArrayList over arrays ?
4.Difference between ArrayList and Vector ?
5.How to obtain Array from an ArrayList ?
6.Why insertion and deletion in ArrayList is slow compared to LinkedList ?
7.Why are Iterators returned by ArrayList called Fail Fast ?
8.How do you decide when to use ArrayList and When to use LinkedList?
JAVA INTERVIEW QUESTIONS - Set interface
- The List interface provides support for ordered collections of objects.
- Lists may contain duplicate elements.
2.What are the main implementations of the List interface ?
The main implementations of the List interface are as follows :
- ArrayList : Resizable-array implementation of the List interface. The best all-around implementation of the List interface.
- Vector : Synchronized resizable-array implementation of the List interface with additional "legacy methods."
- LinkedList : Doubly-linked list implementation of the List interface. May provide better performance than the ArrayList implementation if elements are frequently inserted or deleted within the list. Useful for queues and double-ended queues (deques).
3.What are the advantages of ArrayList over arrays ?
Some of the advantages ArrayList has over arrays are:
- It can grow dynamically
- It provides more powerful insertion and search mechanisms than arrays.
4.Difference between ArrayList and Vector ?
ArrayList | Vector |
---|---|
ArrayList is NOT synchronized by default. | Vector List is synchronized by default. |
ArrayList can use only Iterator to access the elements. | Vector list can use Iterator and Enumeration Interface to access the elements. |
The ArrayList increases its array size by 50 percent if it runs out of room. | A Vector defaults to doubling the size of its array if it runs out of room |
ArrayList has no default size. | While vector has a default size of 10. |
5.How to obtain Array from an ArrayList ?
Array can be obtained from an ArrayList using toArray() method on ArrayList.
List arrayList = new ArrayList(); arrayList.add(… Object a[] = arrayList.toArray();
- ArrayList internally uses and array to store the elements, when that array gets filled by inserting elements a new array of roughly 1.5 times the size of the original array is created and all the data of old array is copied to new array.
- During deletion, all elements present in the array after the deleted elements have to be moved one step back to fill the space created by deletion. In linked list data is stored in nodes that have reference to the previous node and the next node so adding element is simple as creating the node an updating the next pointer on the last node and the previous pointer on the new node. Deletion in linked list is fast because it involves only updating the next pointer in the node before the deleted node and updating the previous pointer in the node after the deleted node.
7.Why are Iterators returned by ArrayList called Fail Fast ?
Because, if list is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, 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.
8.How do you decide when to use ArrayList and When to use LinkedList?
If you need to support random access, without inserting or removing elements from any place other than the end, then ArrayList offers the optimal collection. If, however, you need to frequently add and remove elements from the middle of the list and only access the list elements sequentially, then LinkedList offers the better implementation.
EmoticonEmoticon