Tuesday 23 April 2013

Collection framework in JAVA

  • Collection Framework provides an architecture to store and manipulate the group of objects. All the operations that you perform on a data such as searching, sorting, insertion, deletion etc. can be performed by Java Collection Framework. 
  • Collection simply means a single unit of objects. Collection framework provides many interfaces (Set, List, Queue, Deque etc.) and classes (ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet etc).
  • Collection represents a single unit of objects. 
  • Collection Framework provides readymade architecture which represents set of classes and interface.

 

 

 

Implemented classes 

I. List interface

 ArrayList (Implements List interface)

  1. Introduce in Java 1.2 version
  2. Insertion preserved
  3. Duplicates allowed 
  4. Null possible
  5. Implements RandomAccess, Serializable and Cloneable Interface.
  6. Best for retrieval and worst if our frequent operation is insertion & deletion in middle.

LinkedList (Implements List interface)

  1. Introduce in Java 1.2 version.
  2. Insertion preserved.
  3. Null possible.
  4. Implements Serializable & Cloneable interface, but not RandomAccess.
  5. Best for frequent insertion & delection and Worst in retrieval.

Vector (Implements List interface)

  1. Introduce in Java 1.0 version.
  2. Same as ArrayList 2 to 6 points.
  3.  All methods are synchronized, hence vector class objects are thread safe.

Different between ArrayList & LinkedList

 

 ArrayList
 LinkedList
1. Underlying data structure is re-sizable or grow-able array.
 1. Underlying data structure is DoubleLinkedList.
2. Best choice if frequent operation is retrieval and worst choice if frequent operation is insertion & deletion in middle.
 2. Best choice if frequent operation is insertion & deletion in middle and worst in retrival.
 3. Implements Serializable, Cloneable & RandomAccess interface.
 3. Implements Serializable & Cloneable, but not RandomAccess interface
 

Different between ArrayList & Vector

 

 ArrayList

 Vector

 1. Introduced in Java 1.2
 1. Introduced in Java 1.0
 2. Methods are not Synchronized.
 2. Methods are Synchronized.
 3. Objects are not thread safe.
 3. Objects are thread safe.
 4. High performance.
 4. Low performance.
 5. Non Legacy.
 5. Legacy
 

 

II. Set interface

  HashSet (Implements Set interface)

  1. Introduced in Java 1.2 version.
  2. null values are allowed.
  3. Duplicates are not allowed.
  4. Insertion order will be based on Hash code of object, hence insertion order is not preserved.
  5. It is best suitable if frequent operation is searching.
  6. Implements Serializable & Cloneable interface.
  7. If we are trying to insert duplicate objects it will not give any compile time or runtime error just it will returns false and doesn't add that object.

 

LinkedHashSet (Implements Set interface)

  1. Introduced in Java 1.4 version.
  2. It's child class of HashSet, only difference is insertion order is preserved.
  3. Underlying data structure is combination of HashTable & LinkedList.

 

TreeSet (Implements Set interface)

  1.  Introduced in Java 1.2 version.
  2. Duplicates are not allowed.
  3. All objects will store according to some sorting order, hence insertion is not preserved.
  4. Heterogeneous objects are not allowed violation leads to ClassCastException.
  5. In empty TreeSet null value can be inserted only once, after that it will give NullPointerException.
  6. Non-empty TreeSet if we are trying to insert null value it will give NullPointerException.

Difference between HashSet and TreeSet.


 HashSet
 TreeSet
1. Underlying data structure is HashTable.
 1. Underlying data structure is Balanced Tree.
 2. Heterogeneous objects are allowed.
 2. Heterogeneous objects are not allowed.
 3. null values are not allowed.
 3. null values is possible only for 1st element.
 4. Insertion order is not preserved, it's based upon HashCode of object.
 4. Insertion order is not preserved, it's based upon some sorting order.

 

III. Map interface (Key,Value pair)

 HashMap (Implements Map interface)

  1. Introduced in Java 1.2 version.
  2. Underlying data structure is HashTable.
  3. Key's should be unic and value's can be duplicated.
  4. Insertion order is not preserved it will be based on key HashCode.
  5. Heterogeneous object are allowed both key & value.
  6. null in key we can insert only once, but null in value can be inserted any number of times.

 

LinkedHashMap (Implements Map interface)

  1. It's child of HashMap, only difference is insertion order os preserved.
  2. underlying data structure is both HashTable & LinkedList.

Difference between HashMap and HashTable.

 

 HashMap
 HashTable
 1. Introduced in Java 1.2 & Non-Legacy.
 1. Introduced in Java 1.0 & Legacy.
 2. No methods are Synchronized, hence HashMap objects are not thread safe.
 2. All methods are Synchronized & it is thread safe.
 3. Performance is High.
 3. Performance is Low.
 4. null is possible for both key & value.
 4. null value is not allowed.

 

IdentityHashMap (Implements Map interface).

  1.    It's child class of HashMap, it's similar to HashMap. 
  2.    Only difference is in IdentityHashMAp JVM uses == operator to  operator to identify   duplicate key's and in case of HashMap JVM uses  equals() to identify duplicate key.
 

 WeakHashMap (Implements Map interface)

  1.  It's child class of HashMap.
  2. Only one difference in HashMap object is not eligible for garbage collection even though if doesnot have any external reference, in case of WeakHashMap if it is not associated with external reference it is eligible for garbage collection.

TreeMap (Implements Map interface)

  1. Underlying data structure is Reb-block tree.
  2. Duplicate value are allowed, but not key's.
  3. Insertion order is not preserved because it is based on some sorting order.
  4. Natural sorting Key's should be homogeneous, but value's need not be homomgeneous. In case of customized sorting we can insert heterogeneous key's & value's.
  5. For empty TreeMap 1st key can be null then after it will give NullPointerException and for Non-empty TreeMap null is not possible, values can be null.

 

HashTable

  1. Introduced in Java 1.0.
  2. Values can be duplicated.
  3. null is not allowed for key & value.
  4. All methods are synchronized.
  5. Insertion is not preserved.
  6. Heterogeneous are allowed for key & value.

 

PriorityQueue (Implements Queue interface)

  1. Insertion order is not preserved based on sorting order.
  2. null values are not allowed.
  3. duplicates are not allowed.
  4. For natural sorting objects should be homogeneous and for customized sorting objects can be heterogeneous. 

 

Difference between Comparable & Comparator.

 

 Comparable

 Comparator

 1. Used to implement Natural sorting order.
 1. Used to implement Customized sorting 
 2. This interface present in Java.Lang package.
 2. This interface present in Java.Util package.
 3. It contain only one method
public int compareTo (Object obj)
 3. It contains two methods
public int compare (Object,Object)
public Boolean equals (Object)
 4. It's a marker interface.
 4. Not a marker interface.

 

Difference between Enumeration, Iterator & ListIterator cursor's.

 

 Enumeration

 Iterator

 ListIterator

1.  It's a legacy interface.
 1. It's a non legacy interface.
 1. It's a child of Iterator.
 2. It's applicable for only legacy classes.
 2. It's applicable for all collection implemented classes
 2. It's applicable for List implemented classes
 3. It's useful for read only.
 3. It's useful for read & delete.
 3. It's useful for add, read, replace & delete.
 4. we can travel only forward direction.
 4. we can travel only forward direction.
 4. we travel in bidirectional.
 5. Using elements() we can get enumeration object.
 5. Using iterator() of collection interface we can get Iterator object.
 5. Using listIterator() of List interface we can get ListIterator object.
 6.  Introduce in Java 1.0
 6. Introduce in Java 1.2
 6. Introduce in Java 1.2

 

Synchronized version of  ArrayList, Set & Map interface.

public static List synchronized (List l );
public static Set synchronized (Set s);
public static Map synchronized (Map m);

eg:-  ArrayList l = new ArrayList();
        List l2 = Collection.synchronizedList(l);

No comments:

Post a Comment

Java Packages Interview Question & Answers

All Packages Q) Thread Class Methods: - getName() run() getPriority() Sleep() isA...