Sunday, June 28, 2015

Important Java collection Interview Questions



1) What is collection in Java?
collection (lowercase c): which represents any of the data structures in which objects are stored and iterated over. More for details see this "Java Collection "

2) Basic Difference between List, Set and Map
List: can contain duplicate elements. A List cares about the index. List implementations are ordered by index position—a position that you determine either by setting an object at a specific index or by adding it without specifying position, in which case the object is added to the end.
Set: it doesn't allow duplicate elements. The equals() method determines whether two objects are identical (in which case only one can be in the set).
Map: is key value pairs.Unique key to a specific value, where both key and value are, of course, objects.Like Sets, Maps rely on the equals() method to determine whether two keys are the same or different.

3) Difference between ArrayList and Vector
  • Thread safe:
    Vector methods are synchronized for thread safety.
    ArrayList methods are not thready safety, so for performance we can use ArrayList (based on the requirement).
  • Vector and ArrayList both implements RandomAccess interface.
  • Other than Hashtable ,Vector is the only other class which uses both Enumeration and Iterator. While ArrayList can only use Iterator for traversing an ArrayList . 
  • Automatic Increase in Capacity:
    A Vector defaults to doubling size of its array . While when you insert an element into the ArrayList, it increases its Array size by 50%  .
    By default ArrayList size is 10 . It checks whether it reaches the       last  element then it will create the new array ,copy the new data of last array to new array ,then old array     is garbage collected by the Java Virtual Machine (JVM) .
  • Introduction in Java :
    Vector is legacy Collection (i.e. it is from the earliest days of Java).
    ArrayList is available from Java 1.2


4) How to use/make ArrayList in Multithread environment?
Below 2 ways we can use ArrayList in Multithread environment.
i)  Collections.sycnhronizedList(...)
    <T> List<T> java.util.Collections.synchronizedList(List<T> list)

ii) CopyOnWriteArrayList

     java.util.concurrent.CopyOnWriteArrayList

5)  Differences between ArrayList and LinkedList?
ArrayList: This as a grow-able(Dynamic) array. It gives you fast iteration and fast random access. To state the obvious: it is an ordered collection (by index), but not sorted.ArrayList is better to store and fetch data. Choose this over a LinkedList when you need fast iteration but aren't as likely to be doing a lot of insertion and deletion.
LinkedList: A LinkedList is ordered by index position and the elements are doubly-linked to one another. If you modify the list (insertion or deletion of the elements from the list) then go for LinkedList. It is more efficient for manipulation because a lot of shifting is required.LinkedList may iterate more slowly than an ArrayList, but it's a good choice when you need fast insertion and deletion

6) Differences between HashSet and LinkedHAshSet?
HashSet : It is an unsorted, unordered Set. Use this class when you want a collection with no duplicates and you don't care about order when you iterate through it. It uses the hashcode of the object being inserted, so the more efficient your hashCode() implementation the better access performance we'll get.
LinkedHashSet A LinkedHashSet is an ordered version of HashSet that maintains a doubly-linked List across all elements. Use this class instead of HashSet when you care about the iteration order. When you iterate through a HashSet the order is unpredictable, while a LinkedHashSet lets you iterate through the elements in the order in which they were inserted.

7) What is Treeset?
 The TreeSet is one of two sorted collections (the other being TreeMap). It uses a Red-Black tree structure, and guarantees that the elements will be in ascending order, according to natural order. Optionally, we can construct a TreeSet with a constructor that lets we give the collection our own rules for what the order should be (rather than relying on the ordering defined by the elements' class) by using a Comparable or Comparator.

8) What is the difference between HashSet and TreeSet?
HashSet maintains no order whereas TreeSet maintains ascending order (or we can construct the TreeSet with a constructor that lets we give the collection our own rules for what the order should be (rather than relying on the ordering defined by the elements' class) by using a Comparable or Comparator.


9) What is the difference between HashMap and Hashtable?
HashMap and Hashtable both implements Map interface Both looks similar, but they have few differences.
HashMap is not synchronized, so HashMap is better for single threaded environment.   
Hashtable is synchronized, so Hashtable is suitable for multi-threaded environment.
HashMap allows one null key and multiple null values. whereas Hashtable doesn't allow null key and null values.