Youtube Channel

Collection-Framework

Collection Framework
Collection framework is one of the distinct facilities available in java programming.
Collection framework is one of the standardized mechanisms of java which allows us to integrate or grouped different type of elements in a single variable.
Difference between collection framework and arrays in java
Array
Collection framework
An array is a collective name given to group of consecutive memory locations which are referred by similar type of elements
Collection framework is one of the standardized mechanisms of java which allows us to integrate or grouped different type of elements in a single variable.

The concept of array allows to store homogeneous elements
Both homogeneous and heterogeneous elements are allowed
The concept of arrays containing fixed size in nature
The concept of collection framework contains dynamic size in nature

Types of collection framework
Collection framework is classified into two types .They are:
1.       New collection framework.
2.       Legacy collection framework.
New collection framework
In the initial days the sun micro system the concept of collection framework was known as data structures. The concept of data structures was fulfilling few demands of the industry and few more demands were not fulfilled. so sun micro system performed reengineering operation on data structures concept and released to the industry as collection framework.
The existing data structure concept was renamed as legacy collection framework
Types of new collection framework
1.       1D/single collection framework
2.       2D/double collection framework
1D/single collection framework
1D collection framework is one in which the data is organized in the form of key value pair but it is organized in the form of row or column.
1D collection framework interfaces
1.       Java.util.Collection
2.       Java.util.List
3.       Java.util.Set
4.       Java.util.SortedSet
Hierarchy chart of collection framework Interfaces

Collection
List
Set
SortedSet
Available at top most of collection framework hierarchy
Sub interface of collection interface
Sub interface of collection interface
Sub interface of Set interface
An object of collection interface allows us to place duplicate value
Duplicate values are allowed
Duplicate values are not allowed
Duplicate values are not allowed
An object of collection framework always display the data in random order
Sequential order
Random order
Sorted order
An object of collection interface allows us to add data only at end
Add the data either at the end or at the specific position
Add the data only at end
Add the data at end only
Data can be retrieved in forward direction only
Data can be retrieved in forward or backward direction  or random order
Data can be retrieved in forward direction only
Data can be retrieved in forward direction only

Note:
The object of Collection, Set, SortedSet interfaces are known as unit direction objects where as the object of List interface is known as multidirectional object.
Collection framework process
Collection framework process is nothing but performing two phases
1.       Assembling or grouping phase
2.       Disassembling or ungrouping phase

The steps 1,2,3 are known as assembling phase and step 4,5,6 are called disassembling phase.
Example
Int a=10;//------------------1
Integer io=new Integer(a);//-----------2
c.add(io);//-----------------3
Object obj=get the value of c;//----------4
Integer io=(Integer)obj;//------5
Int x=io.intValues()//---------6

Methods in java.util.Collection (interface)
Method
Description
Public int size()
This method returns the size of collection framework variable
Public bollean isEmpty()
Returns true provided collection framework variable is empty otherwise false
Public bollean add()
Used to add values to collection framework variable
Public Iterator iterator()
Used to retrieve the data from collection framework variable in forward direction only
Public void removeAll()
Remove all elements from collection framework variable
Public ListIterator listIterator()
Used to retrieve the data from collection framework variable in forward and backward directions

Public Iterator iterator()
Used to retrieve the data from collection framework variable in forward direction only.
Lets ‘c’ be the collection framework variable .then
C
10
20
30
Or  
10
20
30

Iterator it=c.iterator();
System.out.println(it);//10 20 30

Public Object [] toArray()
This method is used to extract the data from any collection framework variable. This method return array of object of java.lang.Object and they will be processed based on zero based indexing concept or array concept.
C
10
20
30



Obj
10
20
30



Object obj[]=c.toArray();//-----------4
For(int i=0;i<obj.length;i++)
{
Integer io=(Integer)obj[i];//------------------5
Int x=io.intValue();//----------------------6
System.out.println(x);
}
Iterator Interface
Iterator is one of the predefined interface in java .util.* package. When we create an object of iterator interface indirectly which is by default pointing just before the first element and it allows us to retrieve the data in forward direction but not in backward direction.
Methods of Iterator Interface
1.       Public Boolean hasnext();
2.       Public Object next();
Method (1) returns true provided Iterator interface object is having next element else return false.
Method (2) used for obtaining next element of any collection framework variable.
Methods in List Interface
The interface List is sub interface of Collection Interface. So that all method of Collection are inherited into List interface.
Methods in List Interface
Method
Description
Public void add(int,object)
This method is used for adding element to any cfw variable either at end or at specific position
Example
c.add(l.size,new Integer(60));
C

10
20
30
60


Public Object get(int)
Used for obtaining a value of any cfw variable in the form of object of java.lang.Object. the parameter int represents void position of the cfw variable.
10
20
30
60
Object obj=l.get(1);
Sop(obj);//20

Object obj=l.get(4);
Sop(obj);/null


Public Object remove(int)
Used to remove value from cfw variable
Public void removeAll()
Used to remove all elements of cfw variable
Public Boolean hasPrevious()
Returns true provided ListIterator  interface Object having previous element otherwise false
Public Object previous()
Used to retrieve previous element if hasPrevious() returns true
Public Object first()
Used to retrieve first element of cfw variable
Public Object last()
Used to retrieve last element of cfw variable
Public List headList(Object obj)
Here obj is the target object.
Used for obtaining those values(xi) which are less than or equal (<=) target object mathematically.
Public List tailList(Object obj)
Here obj is the target object.
Used for obtaining those values(xi) which are greater  than  (>) target object mathematically.
Public List subList(Object obj1,Object obj2)
Used to obtain range of values from list or used to retrieve the values which are greater than or equal to target object obj1 and less than target object obj2 matematically.
10
20
30
40
Object obj=c.first();//10
Object obj=c.last();//40
List hl=c.hasList(newInteger(20));//10
List tl=c.tailList(new Integer(20));//40
List sl=c.subList(new Integer(20),new Integer(40));//40 30 20


*cfw=collection framework variable
Public ListIterator listIterator()
This method is used for returning the elements of a cfw variable in both forward and backward directions.
10
 1120
30
60
0      forward direction            Backward Direction
1
2
3      sop(l);//10 1120 30 60
ListIterator li=c.listIterator();
//for forward direction
While(li.hasNext())
{
Object obj=li.next();
Sop(obj);//10 1120 30 40
}
//for backward direction
While(li.hasPrevious())
{
Object obj=li.previous();
Integer io=(Integer)obj;
Int x=io.intValue();
Sop(x);// 40 30 1120 10
}
Difference between Iterator and ListIterator Interface
                                         Iterator            
listIterator
Iterator is one of the super interface for ListIterator
Sub interface of Iterator
An object of iterator interface allows to retrieve the data only in forward direction
An object of ListIterator allows s to retrieve the data in both forward and backward directions
Public Boolean hasNext()
Public Object next()
Public Boolean hasNext()
Public Object next()
Public Boolean hasPrevious()
Public Object previous()

Methods in java.util.Set(Interface)
SetInterface is one of the sub interfaces of collection Interface so that all the methods of collection are inherited into Set interface.
The Set interface does not contain any special methods except Collection Interface methods.
Note:
Even though methods of collection and Set interface are same, the methods of collection interface are defined in some predefined in such a way that duplicate values are allowed. Where as in Set interface unique values or elements are allowed.
Methods of java.util.SortedSet
Methods
Description
Public Object first()
Used to retrieve first element
Public Objet last()
Used to retrieve last element
Public SortedSet hasSet(Object obj)
Here obj is the target object.
Used for obtaining those values (xi) which are less than or equal (<=) target object mathematically.
Public SortedSet tailSet(Object obj)
Here obj is the target object.
Used for obtaining those values (xi) which are greater than (>) target object mathematically.
Public SortedSet subset(Object obj1,Object obj2)
Used to obtain range of values from list or used to retrieve the values which are greater than or equal to target object obj1 and less than target object obj2 mathematically.
10
20
30
40
Object obj=c.first();//10
Object obj=c.last();//40
List hl=c.hasList(newInteger(20));//10
List tl=c.tailList(new Integer(20));//40
List sl=c.subList(new Integer(20),new Integer(40));//40 30 20


1D collection framework classes
1D collection framework classes are those which are containing definitions or implementations for those abstract methods which are inherited from 1D collection framework interfaces.
All 1D collection framework classes are present in java.util.* package. The following table gives the Collection framework interface its corresponding class name and its hierarchy.
Interface
Class
Hierarchy
Collection
1.       AbstractCollection
Implemens Collection
List
2.       AbstractList
Extends AbstractCollection implements List
Set
3.       AbstractSet
Exends AbstractCollection implements Set
SortedSet
4.       AbstractSequentialList
5.       LinkedList
6.       ArrayList
7.       HashSet
8.       TreeSet
Extends AbstractList
Extends AbstractSequentialList
Extends AbstractSequentialList
Extends  AbstractSet
Extends  AbstractSet implements SortedSet

The classes 1,2,3,4 are abstract predefined classes and their object cannot be created directly. So that we may not be using them directly in real time applications but these abstract classes are playing an important role in defining the abstract methods of 1D collection framework interfaces and supplying them to concrete sub classes provides a middleman role between predefined sub classes of 1D collection framework.
The classes 5,6,7,8 are treated as bottom most concrete subclasses of 1D collection framework. So that their object can be created directly hence we use them in real time applications.
LinkedList
LinkedList is one of the bottom most concrete subclass of collection framework classes .
Creating a LinkedList is nothing but creating the object of LinkedList.
LinkedList ll=new LinkedList();
Ll
Data
Address





                                                                                                                             

0 comments: