Wednesday, December 14, 2016

collections framework -Interfaces Collections of Object

                                                                               Define Interface Collections  in Java


Basically collections represent the group of objects. Collections in java represent the collection framework which is used to store and manipulate the group of objects and simple collection represent the single object or a group. The collection of these single object or a group represents collection which is known as collection framework.Collections group of Object

        Collection framework
Collection framework in java is a centralized and unified template to store and manipulate the group of objects. This framework provides some pre-defined interfaces, implements classes and collection algorithms to handle the group of objects. Collection framework contains following things
  o   Interfaces
  o   Implement Classes
  o   Algorithms

Hierarchy Collection Framework

The java.util package contains all the classes and interfaces for collection framework.
collection framework

The Iterable interface
This is the parent interface which is extends by collection interface.This interface is used to for iterating the elements in forward direction. This interface having three methods which are the following
  o   booleanhasNext(): This method is Boolean method, it return true when the iterator has next element.
  o   object next(): This method return the element object and moves the cursor to the next elements
  o   void remove(): This method is used to remove the last element from iterator.

Collections group of Object

The collection interface

The collection interface is the root element of the hierarchy. This interface is extending by three interfaces which are List, Queue and Set Interfaces. The java does not provide the direct implementation of this interface but java allow us for direct implements of its sub interfaces. All
collection interface are generics. I will discuss generic later.  

Methods of collection interface
o   int size ()
o   void clear ()
o   boolean contains (Object element)
o   booleancontainsAll (Collection c)
o   boolean add (Object element)
o   booleanaddAll (Collection c)
o   boolean remove (Object element)
o   booleanremoveAll (Collection c)
o   Iterator iterator ()
o   Object[] toArray ()
o   booleanisEmpty
o   boolean equals
o   inthashCode ()

    1.       List Interface
In java list represent the sequential or ordered collection of an objects. In list the elements are stored using Integer index, which is start from zero. A list may contain duplicate elements. In java three classes are used to implement the list interface.
o   ArrayList
o   Linkedlist
o   Vector

ArrayList Class
In java ArrayList class is used to provide the dynamic array. Basically arrays are of fixed length, once you define any array after that you cannot change the size of an array. So, it means you need to have to know the size or length of an array in advance but in some cases it is impossible to know the exact length of an array in advance.

ArrayList is an alternate of an array, we can resize the arrayList. ArrayList is more dynamic then the Array because it provides some built-in methods which helps us to store and manipulate the sequential collection of an objects.Collections group of Object

Example Simple ArrayList
Collections group of Object
                          
                                              Example with Collection
Collections group of Object

LinkedList
Basically LinkedList is a data structure. In java LinkedList is a class which is used to store the sequential collection or ordered collection or special collection objects. to store the objects, it uses the doubly linked list which means it implement two interfaces List and Queue.

Elements in LinkedList is knows as node. Every node consists of three parts, first part is containing the reference of previous element, middle part is containing the actual value and the last part is containing the reference of next element. Java LinkedList class can contain the duplicate elements.

Example
Collections group of Object
Collections group of Object
Vector Class
In java Vector Class is work same as like ArrayList Class but Vector class is synchronized which means it will run one thread at a given time. In Vector class all methods are synchronized so that only one thread can be executing them at any given time. Whenever you want thread safety code than vector class is more useful.

As we agree that vector class is useful when we need thread safety but this class reduce the performance of our application because at a time it will execute only one thread so, for other threads have to wait until the current thread will complete its execution.

Vector class has same features as ArrayList. Vector class also extends AbstractList class and implements List interface. It also implements 3 marker Interface RandomAccess, Cloneable and Serializable

Example
Collections group of Object
Collections group of Object

Queue Interface

In Java Queue is an interface which is used to perform FIFO (First in First out). Basically Queue is a data structure which is used to perform the FIFO operation, now the same thing is implement in Java through Queue interface. Queue interface is extending the collection interface, this queue interface is extending by Deque interface and implement by PriorityQueue class.

In Queue it has two ends one end is known as head and second end is known as tail. In head the elements of queue are will be removed and in tail the elements of queue will be added

Methods of Queue Interface
  • public boolean add(object):add the elements in queue. If element is not founding, then return an exception
  • public boolean offer(object): add the elements in queue. If element is not founding, then return null value
  • public remove():remove and retrieve the element from head of the queue. It returns exception if element is not found
  • public poll(): remove and retrieve the element from head of the queue. It returns a null value if element is not found.
  • public element(): retrieve  the element from head of the queue. It returns exception if element is not found
  • public peek(): retrieve the element from head of the queue. It returns exception if element is not found.

PriorityClass
In java priority Class is used to maintain the sequence of an object on FIFO based and also sometime on priority based. If you want a Queue on priority base than you have to provide comparable comparator implementation otherwise it will maintain the Queue in FIFO order.

Example with Priority Base

Collections group of Object
Now if you look at the output the first and second lines says “The Peek Method: 3 3” and “The Element Method:  3 3” as we discuss that the peek and element methods are retrieves the elements from head. So in head the first element is 3 so that’s why both they return the 3.  Now look at the output line number three “Print the Queue: 3 10 5 20 25 5”. Now look at line number four and five “The Poll Method: 3 5 10 15 20 25” and “The Remove Method: Exception in thread main…..” respectively. As we discuss above about poll and remove method that both will retrieve and remove the element from Queue so that’s why the remove method generate the exception because the poll method removes retrieves and also remove the element from so queue. So when the turn of remove method comes and this method not found any element so it generates the exception.

Deque Interface
The Deque interface in java is used for insertion and removals of an element from both sides. The Deque stands for “Double Ended Queue”. This DeQueinterface use both Queue (FIFO: First in and First out) and Stack (LIFO: Last in First out).Collections group of Object

Queue Methods                  Vs            DeQue Methods

Queue Method                                DeQue Method
add ()                                            addLast ()
offer ()                                            OfferLast ()
element ()                                         getFirst ()
peek ()                                             peekFirst ()
remove ()                                 removeFirst ()
poll ()                                          pollFirst ()

Stack Methods            Vs                DeQue Methods
Stack Method                   DeQue Method
push ()                        addFirst ()
pop ()              removeFirst ()
peek ()                     peekFirst ()

Array DeQue Class
In java Array Deque class is used for insertion and removal of an element at both ends. This class implement the DeQue interface. This class used both FIFO and LIFO.

Example
Collections group of Object


Collections group of Object





No comments:

Post a Comment