Monday, October 22, 2012

Java Collections Important Questions & Concepts

1) What is a Collection Framework ?

A) Collection framework is a class library to handle group of objects. Collection framework is implemented in java.util package.

2) Does a Collection object store copies of other objects or their references ?

A) collection object stores references of other objects.

3) Can you store primitive data type into a collection ?

A) No, Collections store only objects.

4) What is the difference between Iterator and ListIterator ?

A) Both are useful to retrieve elements from a collection. Iterator can retrieve the elements only in forward direction. But ListIterator can retrieve the elements in forward and backward direction also. So ListIterator is preferred to Iterator.

5) What is the difference between Iterator and Enumeration ?

A) Both are useful to retrieve elements from a collection. Iterator has methods whose names are easy to follow and Enumeration methods are difficult to remember. Also Iterator has an option to remove elements from the collection which is not available in Enumeration. So, Iterator is preferred to Enumeration.

6) What is auto boxing ?

A) Converting a primitive data type into an object form automatically is called 'auto boxing'. Auto boxing is done in generic types.

7) What is difference between an ArrayList and a Vector?

A)       There are two important differences between an ArrayList and Vector
           a) Synchronization - ArrayList is not thread-safe whereas Vector is thread-safe. In Vector class each method like add(), get(int i) is surrounded with a synchronized block and thus making Vector class thread-safe.
            b) Data growth - Internally, both the ArrayList and Vector hold onto their contents using an Array. When an element is inserted into an ArrayList or a Vector, the object will need to expand its internal array if it runs out of room. A Vector defaults to doubling the size of its array, while the ArrayList increases its array size by 50 percent.

8) How can Arraylist be synchronized without using Vector?

A)  Arraylist can be synchronized using :-
                
                 Collection.synchronizedList(List list)

      Map can be synchronized using :-
 
                 Collection.synchronizedMap(Map map)

      Other collections can be synchronized using :-

                 Collection.synchronizedCollection(Collection c)

9) What is difference between HashMap and HashTable?

A)  Both Collections implements Map. Both Collections store values as key-value pairs. The key differences between the two are :-
                a. Hashmap is not Synchronized in nature where as HashTable is Synchronized.

                b. Another difference is that iterator in the HashMap is fail-safe, while the enumerator for the Hashtable isn't. Fail-safe - if the Hashtable is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove method, the iterator will throw a ConcurrentModificationException.

               c. HashMap permits null values and only one null key, while Hashtable doesn't allow any of its key or value as null.

  •  If you want to learn more about FailFast and FailSafe please refer to the below link
http://javarevisited.blogspot.in/2012/02/fail-safe-vs-fail-fast-iterator-in-java.html


10) What all classes implement List Interface ?

A) There are three classes that implement the List Interface they are :-
 
            ArrayList : It is a resizable array implementation. The size of the ArrayList can be increased   dynamically also operations like add,remove and get can be formed once the object is created. It also ensures that the data is retrieved in the manner it was stored. The ArrayList is not thread-safe.

            Vector: It is thread-safe implementation of ArrayList. The methods are wrapped around a synchronized block.

            LinkedList: The LinkedList also implements Queue interface and provides FIFO(First In First Out) operation for add operation. It is faster if than ArrayList if it performs insertion and deletion of elements from the middle of a list.


11) Which all classes implement Set interface ?

A) A Set is a collection that contains no duplicate elements. More formally, Sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element. HashSet,SortedSet and TreeSet are the commnly used class which implements Set interface.

            SortedSet - It is an interface which extends Set. A the name suggest , the interface allows the data to be iterated in the ascending order or sorted on the basis of Comparator or Comparable interface. All elements inserted into the interface must implement Comparable or Comparator interface.

             TreeSet - It is the implementation of SortedSet interface.This implementation provides guaranteed log(n) time cost for the basic operations (add, remove and contains). The class is not synchronized.

             HashSet: This class implements the Set interface, backed by a hash table (actually a HashMap instance). It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time. This class permits the null element. This class offers constant time performance for the basic operations (add, remove, contains and size), assuming the hash function disperses the elements properly among the buckets.


 12) What is difference between List and a Set?

A) List can contain duplicate values but Set doesnt allow. Set allows only unique elements. List allows retrieval of data to be in same order in the way it is inserted but Set doesnt ensures the sequence in which data can be retrieved.(except HashSet)

13) What is difference between Arrays and ArrayList ?

A) Arrays are of fixed size whereas ArrayList is is not of fixed size.

Some of the advantages ArrayList has over arrays are :-

  • ArrayList can grow dynamically.
  • It provides more powerful insertion and search mechanisms than arrays.
It means that once array is declared as :

Also the size of array cannot be incremented or decremented. But with arrayList the size is variable.
Once the array is created elements cannot be added or deleted from it.

 But with ArrayList the elements can be added and deleted at runtime.

       List list = new ArrayList();
       list.add(1);
       list.add(3);
       list.remove(0) // will remove the element from the 1st location.

          ArrayList is one dimensional but Array can be multidimensional.

To create an array the size should be known or initalized to some value. If not it should be initialized carefully such that there could me no memory wastage. But arrayList is all about dynamic memory creation and there is no wastage of memory.







No comments:

Post a Comment