Friday, June 2, 2017

9-key-interfaces-of-Collection-framework

9 Key Interfaces of Collection framework
  • Collection
  • List
  • Set
  • SortedSet
  • NavigableSet
  • Queue
  • Map
  • SortedMap
  • NavigableMap



Set Interface
.Duplicates are not allowed
Insertion order is not preserved.
Child interface of the collection.

1. Set is the child interface of Collection.
2. If we want to represent a group of individual objects as a single entity, where duplicates are not allowed and insertion order is not preserved then we should go for Set.
3. Set interface doesn’t contain any new methods. So we have to use only Collection interface methods.


Implementation classes: HashSet (1.2 v) and LinkedHashSet(1.4 v)



HashSet:

1. The underlying data structure is Hashtable.
2. Duplicates are not allowed. If we are trying to insert duplicates, we won’t get any compiletime or runtime errors. add() method simply returns false.
3. Insertion order is not preserved and all objects will be inserted based n hash-code of objects.
4. Heterogeneous objects are allowed.
5. ‘null’ insertion is possible.
6. implements Serializable and Clonable interfaces but not RandomAccess.
7. HashSet is the best choice, if our frequent operation is Search operation.


SortedSet

1. It is the child interface of set.
2. If we want to represent a group of individual objects according to some sorting order and duplicates are not allowed then we should go for SortedSet



Difference between List and Set?
ListSet
Duplicates are allowedDuplicates are not allowed
Insertion order preservedInsertion order not preserved

 SortedSet v Navigable Set
Child interface of Set.
Duplicates are not allowed.
Insertion order is preserved.










Navigable Set Interface:(1.6 v)
Child interface of SortedSet.
Implementation class: TreeSet (1.6 v).













Queue Interface:(1.5 v)
Child Interface of Collection.
A group of objects prior to process this can be used.
Implementation class: PriorityQueue, BlockingQueue, LinkedBlockingQueue, PriorityBlockingQueue.

MAP










implementation class for the Map class




Sorting:
1. Comparable(I) - default sorting
2. Comparator (I)  - Customized Sorting order
Cursors:(To retrive the items one by one)
1.Enumeration(I)
2. Iterator(I)
3. ListIterator(I)

Utility Classes: 
1. Collections
2. Arrays
Collection Interface:
1. If we want to represent a group of individual objects as a single entity then we should go for Collection
2. In general collection interface is considered as root interface of Collection Framework. 
3. Collection interface defines the most common methods which are applicable for any collection object
Methods of collection 




Note: Collection interface doesn’t contain any method to retrieve objects there is no concrete class which implements collection class direct
List Interface:
1. It is the child interface of Collection.
2. If we want to represent a group of individual objects as a single entity where duplicates are allowed and insertion order must be preserved then we should go for List.
3. We can differentiate duplicates by using index.
4. We can preserve insertion order by using index, hence index play very important role in list interface







ArrayList-details

The underlined data structure Resizable Array or Growable Array.
Duplicates are allowed.
Insertion order is preserved.
Heterogeneous objects are allowed ( Except TreeSet & TreeMap everywhere heterogeneous objects are allowed ).
Null Insertion is possible.

ArrayList Constructors:
1. ArralyList al= new ArrayList()
Creates an empty Array list object with default initial capacity 10.
Once Array List reaches its map capacity a new Array List will be created with new capacity=(currentcapacity*3/2)+1

ArrayList Constructors:
2. ArrayList al=new ArrayList(int initialCapacity);
3. ArrayList al=new ArrayList(Collection c);











Usually we can use collections to hold and transfer Objecs from one place to another place, to provide support for this requirement every Collection already implements Serializable and Cloneable interfaces. ArrayList and Vector classes implements RandomAccess interface so that we can access any Random element with the same speed. Hence if our frequent operation is retrieval operation then ArrayList is the best choice.
ArrayList:

1. ArrayList is best choice if our frequent operation is retrieval operation (Because ArrayList implements RandomAccess Interfaces)
2. ArrayList is the worst Choice if our frequent operation is insertion or deletion in the middle (Because several shift operation are require.

Differences-between-ArrayList-and-Vector-


-


















Synchronized-version-of-ArrayList




































LinkedList: 

1. The underlying data structure is Double LinkedList.
2. Insertion order is preserved.
3. Duplicates are allowed.
4. Heterogeneous Objects are allowed.
5. Null insertion is possible.
6. LinkedList implements Serializable and Clonable interfaces but not RandomAccess interface.
7. LinkedList is the best choice if our frequent operation is insertion or deletion in the middle.
8. LinkedList is the worst choice if our frequent operation is retrieval operation.
9. Usually we can use LinkedList to implement stacks and queues to provide support for this requirement LinkedList class defines following specific methods.


void addFirst();
void addLast();
Object getFirst();
Object getLast();
Object removeFirst();
Object removeLast();
 

LinkedList Constructors:

LinkedList l1=new LinkedList();
1. creates an empty LinkedList Object. LinkedList l1=new LinkedList(Collection c);
2. Creates an equivalent LinkedList Object for the given Collection.

Differences between ArrayList and LinkedList

Vector class details?

1. The underlying Data structure for the vector is resizable array or growable array.
2. Duplicate objects are allowed.
3. Insertion order is preserved.
4. 'null' insertion is possible.
5. Heterogeneous objects are allowed.
6. Vector class implemented Serializable, Cloneable and RandomAccess interfaces.
7. Most of the methods present in Vector are synchronized. Hence Vector object is Thread-safe.
8. Best choice if the frequent operation is retrieval. 

Vector Specific methods:

For adding objects:

1. add(Object o) [from collection – List(I)]
2. add(int index, Object o) [from List]
3. addElement(Object o) [from Vector]
4. For removing Objects:
5. Remove(Object o) [from collection]
6. removeElement(Object o) [from Vector]
7. remove(int index) [from List)
8. RemoveElementAt(int index) [from collection]
9. removeAllElements() [from Vector]
For Accessing Elements:
Object get(int index) [from Collection]
Object elementAt(int index) [from Vector]
Object firstElement() [from Vector]
Object lastElement() [from Vector]
Other Methods:
int size();
int capacity();
Constructors of vector class:

1. Vector v=new Vector();

Creates an empty vector object with default initial capacity 10, once vector reaches it’s max capacity a new vector object will be created with new capacity =2*current capacity.

2. Vector v=new Vector(int initialCapacity);

creates an empty Vector Object with specified initial capacity.

3. Vector v=new Vector(int initialCapacity, int incrementalCapacity);

4. Vector v=new Vector(Collection c);

Creates an equivalent Vector object for the given collection.

Stack class details (Collection Framework)?

1. It is a child class of Vector.
2. It is Specially designed class for Last in First Out order(LIFO)


Constructor of Stack:

Stack s=new Stack();

Methods in Stack:


1. Object push(Object obj);

For inserting an object to the stack.

2. Object pop():

TO removes and returns top of the stack.

3. Object peak();

To Returns the top of the stack without removal of object.

4. int search(Object obj);
if the specified object is available it returns its offset from top of the statck.
If the object is not available then it returns -1
.

5. Object pop();

For inserting an object to the stack

Three Cursors of Java - Enumeration

If we want to retrieve Objects one by one from the Collection, then we should go for Cursors.
There are three types of cursors are available in Java.

Enumeration
Iterator
ListIterator Enumeration:


Introduced in 1.0 version (for legacy) 

We can use Enumeration to get Objects one by one from the old Collection Objects(Legacy Collection).
We can create Enumeration Object by using elements() method of Vector Class. Public Enumeration elements(); 

Example: 

Enumeration e=v.elements(); 

Method of Enumeration 

Enumeration defines the following two methods

1. public Boolean hasMoreElements();
2. public Object nextElement();


















Iterator:

1. We can apply iterator concept for any Collection object hence it is universal cursor.
2. By using iterator we can perform both read and remove operations.We can create iterator object by using iterator() method of Collection Interface
public iterator iterator();


Example:


Iterator itr=C.Iterator();
Where C is any Collection Object.


Methods In Iterator

Iterator Interface defines the following three methods
1. public Boolean hasNext()
2. public Object next()
3. public void remove()
















Limitations of Iterator:

1. By using Enumeration and Iterator we can move only towards forward direction and we can’t move to the backward direction, and hence these are single direction cursors.
2. By using Iterator we can perform only read and remove operatiions and we can’t perform replacement of new Objects.


Note: 
To overcome above limitations of Iterator we should go for ListIterator



ListIterator: 

1. By using ListIterator we can move either to the forward direction or to the backward direction, and hence ListIterator is bidirectional cursor
2. By using ListIterator we can perform replacement and addition of new Objects in addition to read and remove operations.
3. We can create ListIterator Object by using listIterator() method of List Interface. public ListIterator listIterator() 


Example:

ListIterator itr=l.listIterator();

Where l is any List Object.

Note: ListIterator is the most powerful cursor but its limitation is, it is applicable only for List implemented class objects and it is not a universal cursor.




































Note: LinkedHashSet is the best choice to develop cache based applications, where duplicates are not allowed and insertion order must be preserved.


TreeSet 

1. The underlying data structure for TreeSet Is Balanced Tree
2. Duplicate objects are allowed.
3. Insertion order not preserved, but all objects will be inserted according to some sorting order.
4. Heterogeneous objects are not allowed. If we are trying to insert heterogeneous objects then we will get runtime exception saying ClassCastException.
5. Null Insertion is allowed, but only once.





















Null Acceptance:

1. For Empty TreeSet as the first element null insertion is possible. But After inserting that null if we are trying to insert any another element we will get NullPointerException.
2. For Non empty TreeSet if we are trying to insert Null then we will get NullPointerException.





Difference between Collection and Collections?

1. Collection is an interface which can be used to represent a group of individual objects as a single entity.
2. Collections is an utility class present in java.util.package to define several utility methods (like Sorting, Searching...) for Collection objects.

Difference between Arrays and Collections?

Difference between Arrays and Collections?