Collection Framework

Collection Framework

 

Collections are used in almost every programming language. Most of the programming languages support various type of collections such as List, Set, Queue, Stack, etc.

A Collection is a group of objects.

A Framework is a predefine architecture where we can manipulate group of objects.

Java 1.2 provided a Collection Framework that is used to provide sorting, searching, manipulate the group of objects.

Collection Framework provide group of interfaces and classes and also provide implementation of Algorithms.

Collection Framework's all classes and interfaces exist in java.util package.

Why Collections Framework

Java Collections framework have following advantage :

·  Reduced development effort by using core collection classes rather than implementing our own collection classes.

·  Code quality is enhanced with the use of well tested collections framework classes.

·  Reduced effort for code maintenance by using collection classes shipped with JDK.

·  Re-usability and Interoperability

There are two form of a collection :

·  Generic Collection

·  Non Generic Collection


Generic Collection :

A group of similar or homogeneous type of objects is called Generic Collection.

For Example : List <String> list=new ArrayList<String>();

In above collection we can have String type of objects only. 

 

Non Generic Collection :

A group of dissimilar or heterogeneous type of objects is called Generic Collection.

For Example : List list=new ArrayList();

In above collection we can have any type of objects.



Difference between Array and Collection

Array

Collection

Array size is fixed.

Collection is a re-sizable.

Array can holds homogeneous data type.

Collection can be homogeneous or heterogeneous.

Array can holds primitive and non primitive data type.

Collection can holds non primitive data type (Objects).

Array is faster than collection.

Collection slower than an array.

If you know the size of elements then go for array.

If you dont know the size of elements then go for collection.

In case of array there is no ready made methods are availables.

In case of collection there are ready made methods are availables.

In case of memory array is not recommended.

In case of memory collection is recommended.

 

Hierarchy of Collection Framework


Collection Hierarchy


Iterable Interface

It is a root class of Collection Framework.

This interface exist on java.lang package.

It has an iterator() method which used to iterate a collection.

This iterator() method return Iterator interface object which exist on java.util package.

 

Iterator Interface

Iterator interface provides methods to iterate over any Collection. We can get iterator instance from a Collection using iterator method.

Iterators allow the caller to remove elements from the underlying collection during the iteration.

Iterators in collection classes implement Iterator Design Pattern.

Method

Description

hasNext()  

Test next element is available in collection or not during iteration.

next()    

Return an element from a collection during iteration.

remove()   

It is used to remove element from collection during iteration.

 

Collection Interface

Collection is a sub class of Iterable interface which having following methods to perform different operation.

Method

Description

size()  

It is used to get length of a collection.

isEmpty()   

Test whether collection is empty or not.

contains()

Find an element from a collection.

toArray()

It is used to convert a collection into an array.

add()

It is used to add an element into collection.

remove()

It is used to remove an element from a collection.

clear()

It is used to remove all the elements from a collection.

addAll()

It is used to add a collection in to another collection.

removeAll()    

It is used to remove a collection from another collection.

retainAll()    

It is used to remove all collection elements except of retain collection.

containsAll()   

Find a collection from another collection.

 

List Interface

List is an sub interface of Collection interface.

List interface allows duplicate elements.

List interface is an ordered collection (sometimes called a sequence).

List interface implemented by ArrayList, LinkedList and Vector class.

List interface has following methods:

Method

Description

get()

It is used to get an element based on index from a collection.

set()

It is used to replace an element based on index in a collection.

indexOf() 

It is used to get an index based on element.

lastIndexOf() 

It is used to get an index based on last element.

subList() 

It is used to get sublist based on from index and to index.

listIterator() 

It is used to iterate an element in both direction forward and backward.

 

Queue Interface

Queue is sub interface of a Collection interface.

Queue interface holds elements in FIFO (First In First Out) order.

Queue interface implemented by PriorityQueue class.

Queue interface provide following methods :

Method

Description

add()  

It is used to add an element in Queue.

offer()  

It is used to add an element in Queue.

remove()  

It is used to get and remove an element from top in a Queue.

poll()

It is used to get and remove an element from top in a Queue and returns null if Queue is empty.

element()  

It is used to get an element from top in a Queue.

peek()   

It is used to get an element from top in a Queue and return null if Queue is empty.

 

Deque interface

Deque interface is sub interface of Queue interface.

In Deque we can insert and delete element from both ends.

Methods are provided to insert, remove, and examine the element.

Each of these methods exists in two forms:

·  One throws an exception if the operation fails,

·  The other returns a special value (either null or false, depending on the operation).

Deque interfaced implemented by ArrayDeque class.

Deque provides following methods :

Method

Description

addFirst()

It is used to add an element at the beginning.

addLast()

It is used to add an element at the end.

offerFirst()

It is used to add an element at the beginning and it return true or false.

offerLast()

It is used to add an element at the end and it return true or false.

removeFirst()

It is used to remove the first element at beginning but if deque is empty it will throw NoSuchElementException.

removeLast()

It is used to remove the last element but if deque is empty it will throw NoSuchElementException.

pollFirst()

It is used to remove the first element at beginning but if deque is empty it will return null.

pollLast()

It is used to remove the last element but if deque is empty it will return null.

getFirst()

It is used to get an element from top but if deque is empty it will throw NoSuchElementException.

getLast()

It is used to get an element from last but if deque is empty it will throw NoSuchElementException.

peekFirst()

It is used to get an element from top but if deque is empty it will return null.

peekLast()

It is used to get an element from last but if deque is empty it will return null.

removeFirstOccurrence()

It is used to remove an element first occure.

removeLastOccurrence()

It is used to remove an element last occure.

push()

It is used to add an element at top in a Deque.

pop()

It is used to remove an element from top.

decendingIterator()

It is used to iterate in a decending order.

 

Set Interface

Set interface is sub interface of Collection Interface.

Set interface can never contains duplicate elements.

Set interface implemented by HashSet and TreeSet class.

Set interface has following methods :

Method

Description

size()

It is used to get length of a collection.

isEmpty()

Test whether collection is empty or not.

contains()

Find an element from a collection.

toArray()

It is used to convert a collection into an array.

add()

It is used to add an element into collection.

remove()

It is used to remove an element from a collection.

clear()

It is used to remove all the elements from a collection.

addAll()

It is used to add a collection in to another collection.

removeAll()

It is used to remove a collection from another collection.

retainAll()

It is used to remove all collection elements except of retain collection.

containsAll()

Find a collection from another collection.

 

Collections class

Java Collections class consists exclusively of static methods that operate on or return collections.

This Collections class provide following methods:

Method

Description

addAll()

It is used to add elements in a specific collection.

binarySearch()

It is used to search position of an element in sorted list.

copy()

It is used to copy all elements from source list to destination list.

frequency()

It is used to return number of a repeated object in a collection.

max()

It is used to get max element or value from a collection.

min()

It is used to get min element or value from a collection.

replaceAll()

It is used to replace all occurrences of one specified value in a list with the other specified value.

reverse()

It is used to reverse the order of a list elements.

rotate()

It is used to rotate the elements in the specified list by a given distance.

shuffle()

It is used to randomly reorders the specified list elements.

sort()

It is used to sort list of elements.

swap()

It is used to swap an element from another based on position in list.

synchronized...() 

It is used to to make Thread safe a collection or map.

unmodifiable...() 

It is used to make immutable a collection or map.



No comments: