Wednesday, November 17, 2010

Collection Interview Questions.

Question: Difference between Vector and ArrayList?
Answer : 
              1) Synchronization:
                         vector is synchronized and ArrayList is not.
             2) Data Growth:
              Both holds there contents using the Array.
             Vector double the size of it's array.
             ArrayList increases it's array Size by 50 percent.
3) Default Size :
           vector: Has default size of 10.
         ArrayList : Has no default size.
4) Iteration :
       ArrayList : Use only iterator to traverse the elements.
       Vector : can use Iterator and enumeration interface to traverse the elements.
Question: Difference between Hashtable and HashMap ?
       1) Synchronization:
                Hashtable : Synchronized.
                HashMap : Not Synchronized.
       2) Null Values :
           HashTable: Does not allow null  key - values, it Does not allow duplicate values.
           HashMap : allows null key and null values.
Detailed Explanation :
HashMap : Allows null as key and null as value.
if you duplicate the key value in HashMap it simply overwrites the key value.
that means.


case 1 :
HashMap.put(null,null);
HashMap.put(null,null); is allowed in HashMap.
case 2 :
HashMap.put("sekhar",200);
HashMap.put("sekhar",400);
in the above case it simply overwrites the key with new value : 400.
case 3:
HashMap.put(null,"Java") // is allowed in HashMap.


Case 4 :
HashMap.put("Java",null) // allowed in HashMap.
HashMap.put("Java",null) // allowed in HashMap. overwrites the Key.


HASHTABLE :
case1 :
Hashtable.put(null,null) // Not supports in Hashtable. but allowed in Hashmap.


case2:
Hashtable.put("sekhar",400);
Hashtable.put("sekhar",300);
Same like the HashMap it simply overwrites the Key here also.
case 3:
Hashtable.put(null,"MyName");
 // not allowed in Hashtable same like HashMap.
case 4:
Hashtable.put("Java",null) 
//not allowed in hashtable but allows in Hashmap.


Observation 1 : Simply, if either key or value if it is null it is not allowed in Hashtable.
Observation 2 : No Problem key or value allowed as null in HashMap.


Question : What is the difference between TreeMap and HashMap?


TreeMap                                                             HashMap
1) Cannot insert null as key                              1)cannot inset null as key.
2) keys in TreeMap stored in sortedOrder    2)stored according to hashCode.
Note1 : 
TreeMap is like the Hashtable :where we cannot insert null as key.
Hashtable does not support the null as value,but Tree Map supports that.
Note2: 
2) TreeMap has constructor which takes comparator and sorts the keys according 
to that.
Question : What is the difference between the TreeSet and HashSet ?
Ans: TreeSet                                                                                     HashSet
1) organize the data in a tree through use of comparator  2) organize in HashTable
(natural Ordering)                                                                         through hashcode.
2) Do not need to sort the elements.


Question : What is Collection Framework hierarchy ?
Ans : 
                 Collection[ interface ]                      MAP [ interface ]
|---------------|--------------|                                    |
List[ interface ]     Set[interface]            SorterdMap[ interface ]        
|                                            |                                             |
ArrayList, LinkedList      Hashset, Treeset           HashMap,TreeMap


Note : ArrayList, LinkedList, HashSet,Treeset, HashMap,TreeMap are the 
Concrete Class came from java 1.5. These are not Synchronized.


There are historical classes like Vector,HashTable,Stack,Properties these
 present in Java 1.0 and these are synchronized.
Question : What are class which does not suits really for serialization ?
Ans :  In general, any class that involves native code is not really a good candidate for serialization.
http://oreilly.com/catalog/javarmi/chapter/ch10.html


Note: Iterator in of Behavioural Pattern.
Note : It a best practice to implement the user defined key class as an immutable object.


String class in designed with "FlyWeight" design Pattern.
What are the Final Methods in String Class ?
Ans : wait, notify,notify,getClass.


Question : What is concurrentHashMap?
Ans :Permits any number of concurrentReads and tunable  number of concurrent 
Writes.
Concurrent Package : also have 
CopyOnWriteArrayList, CopyOnWriteArraySet.


Example of CopyOnWriteArrayList ;

CopyOnWriteArrayList mylist = new CopyOnWriteArrayList();
      mylist.add("sekhar");
      
      Iterator myIt = mylist.iterator();
      mylist.add("Ramu");
      
      while(myIt.hasNext()){
 System.out.println(" Value : "+myIt.next());
      }
Above Example Does not through any Exception : like ConcurrentModification 
Exception. but a ArrayList throws that Exception.


NO risk of Concurrent Modification.


Note: A TreeSet is a orderedSet which implements the SortedSet.
Note: Iterator is Behavioural Design Pattern.



Q. What is a list iterator?
The java.util.ListIterator is an iterator for lists that allows the programmer to
traverse the list in either direction (i.e.forward and or backward) and modify the
list during iteration.



No comments:

AWS certification question

AWS AWS Hi! this is for questions related to AWS questions. EC2 instances EC2 storage types cold HDD : 1. Defines performance in terms...