Welcome to Java4u

A Single Place for all Java Resources

Looking for something?

Subscribe to this blog!

Receive the latest posts by email.

.Just enter your email below if you want to subscribe!

Email

Tuesday, August 31, 2010

Collections FAQs

Q21. What is difference between ArrayList and Vector?
   


ArrayList
Vector
1. No method is synchronized in the ArrayList class 1. All methods in Vector are synchronized.
2. ArrayList object is not thread safe. 2.  Vector is thread safe.
3. Relatively performance is high 3. Relatively performance is low
4. Introduced in 1.2 version and it is non legacy 4. Introduced in 1.0 version and it is legacy

Q22. How we can get synchronized version of ArrayList?

Collections class contains synchronizedList() method for this
                Public static List synchronizedList(List l)
               
                EX                 ArrayList l= new  ArrayList();
                                  List l2=Collections.synchronizedList(l);

  Similarly we can get synchronized versions of Set and Map objects by the following methods.
Public static List synchronizedSet(Set s)
Public static List synchronizedMap(Map m)

Q23. What is difference between size and capacity of a Collection Object?

size means number  of objects present  where as capacity means no of objects it can accommodate.

Q24. What is difference between ArrayList and Linked List?

ArrayList
LinkedList
1. The underlying data structure is resizable or growable array. 1. The underlying data structure is Double Linked List.
2.  This is Best choice if frequent operation is retrieval and worst choice if frequent operation is insertion or deletion in the middle. 2.  This is Best choice  if frequent operation is insertion or deletion in the middle and worst choice if frequent operation is retrieval .
3. This class implements Serializable , Cloneable and RandomAccess interfaces. 3. This class implements Serializable , Cloneable but not  RandomAccess interface.

Q25. What are legacy classes and interfaces present  in Collections framework ?
  • Enumeration ---Interface
  • Dictonary ------Abstract class
  • Hashtable -----Concrete class
  • Properties -----Concrete class
  • Vector -----Concrete class
  • Stack  -----Concrete class
 Q26. what is difference Enumeration and Iterator?

Enumeration
Iterator
1. It is legacy interface and introduced in 1.0 version 1 It is non-legacy and introduced in 1.2 version
2.Applicable only for legacy classes and it is not universal cursor 2.Applicable for any Collection implemented class object.
3.While iterating the elements we are not allowed to remove the objects just we can perform only read operation 3.While iterating we can perform removal also in addition to read operation.
4.By using elements() method we can get Enumeration object 4.By using iterator() method we can get Iterator
    object

Q27. What are limitations of Enumeration?
  • While iterating the elements we are not allowed to perform removal operation
  • It is applicable only for legacy classes and it is not a universal cursor.
  • It can retrieve the elements only in forward direction
  Q28. What is difference between enum and Enumeration?

An enum can be used to define a group of named constants .It has  introduced in 1.5 version

Ex                  Class Beer
                                 {
                                  KO,KF,RC,FO
                                    }

Enumeration is cursor to retrieve Objects one by one from Collection objects.

Q29. What is difference between Iterator and ListIterator?
  • ListIterator is the child interface of the Iterator
  • Iterator is the single direction cursor where as ListIterator is bidirectional cursor.
  • While iterating the elements by Iterator we can perform only read and remove operations. But by using ListIterator we can perform read,removal, replace and addition of new objects also.
  • Iterator is applicable for every Collecton implemented class object but ListIterator  is applicable only for List implemented class objects.
  • Iterator can be get by using iterator() of Collection interface where as ListIterator can be get by using listIterator() method of List interface
  • both are introduced in 1.2 version
Q30. What is relation between ListIterator and Iterator?
   
      ListIterator is child interface of Iterator 

<Pre 1 2 3 4 5 6 Next>

0 comments: