Home > java > Collection Framework in java

Collection Framework in java

Collection Framework classes and interfaces in java

Collection framework is one of the additional service developed by Sun Microsystem developer supply is a part of Java software for enhancing the performance of Java application.

Collection Framework
A Collection Framework is one of the standardized mechanism of Java and its allow us to store a similar type of values or a different type of the value of both the type of value in a single variable with dynamic size in nature. This single variable is called the collection framework variable.


Q. What is the difference between array and collection framework

ArrayCollection framework
1) An array allows storing only homogeneous value but not heterogeneous value.1) the collection framework allows us to store multiple values of the same type or a different type or both the types.
2) An array is a fixed size in nature.2) collection framework is dynamic in nature.

To deal with collection framework programming be required some predefined classes and interfaces which are present in “java.util.*” package.

The advantage of collection framework


1) collection framework provides high performance to Java application.
2) collection framework provides extendability (increasing the size of a collection framework variable depend on the incoming flow of data is called extendability).
3) collection framework provides adaptability (adding the content of one collection framework variable into another collection framework variable either in beginning/ending/middle od the adding is called Adaptability).
4) collection framework is algorithmic oriented. a) it provides inbuilt sorting technique. b) it provides inbuilt searching technique. c) it provides the premilary concept of a data structure (Stack, LinkedList, queue).

Types of Collection Framework


In the earlier days of Sun Microsystem the concept of collection framework in known as data structure.
Data Structure concept fulfills few requirements of industry and unable to fulfill few more requirement of the industry so that Sun Microsystem developer revise the existing Data Structure concept and release to the real industry on the name of new collection framework and existing Data Structure concept is renamed as Legacy collection framework.
collection framework is classified into two types.

1) New collection framework
2) Legacy collection framework

New Collection Framework


Re-engineering or a revised form of existing data structure is known as the new collection framework.

Types of New Collection Framework

1) 1-Dimensional/Single Collection Framework (1-D Collection Framework)
2) 2-Dimensional/Double Collection Framework/Maps (2-D Collection Framework)

1-D/Single Collection Framework


1-D Collection Framework organizes the data either in the form of row or column but not in the form of row and column.

To deal with 1-D Collection Framework programming, we need to learn 1-D Collection Framework interfaces and classes, and they present in java.util.*.

1-D Collection Framework interfaces

1) java.util.Collection
2) java.util.List
3) java.util.Set
4) java.util.SortedSet

One dimensioanl new collection framework

The object of Collection, List, Set, and SortedSet is called Collection Framework variables. As a Java programmer, it is mandatory for us to find which collection variable to select to fulfills the requirement in the industry.

Difference between Collection, List, Set, SortedSet


DIFFERENT FACTOR

Collection

List

Set

SortedSet

HIERARCHY

Collection is one of the topmost pre-defined interface in 1-D Collection FrameworkList is the subinterface of Collection.Set is a subinterface of Collection.Sortedset is a subinterface of Set

FUNCTIONALITY

Object of Collection interface allow duplicate elements.Object of List interface allow duplicate elements.Object of Set interface allows unique elements.Object of SortedSet interface allows unique elements.

DISPLAY

Collection interface object display the data in random order.List interface object display the data in sequential order.Set interface object display the data in random order.Sortedset interface object displays the data in sorted order.

ORGANIZE

Collection interface an object allows us to add the data only at the endpoint.List interface object allows us to add the data either in the beginning or in the middle or at the endpoint.Set interface object allows us to add the data only at the endpoint.Sortedset interface object allows adding the data either in the beginning or in the middle or at the endpoint.

RETRIEVE

Collection interface object allows us to retrieve the data only in forward direction but not in other direction.List interface object allows retrieving the data in forward-backward and random direction.Set interface object allows to retrieve the data only in forward direction but not in other direction.Sortedset interface object allows retrieving the data in forward-backward and random direction.

1-D Collection Framework Process


It contains two phases.

1) Grouping/Assembling
2) Ungrouping/Disassembling

The following diagram use sequence of steps to be performing Grouping and Ungrouping phases


One dimensioanl collection framework process

In the above diagram, 1,2,3 are used for performing Grouping phase and 4,5,6 are used for performing Ungrouping phase.

Methods in java.util.Collection


public int size(): This method is used for finding the number of entries present in any collection Framework.

public boolean isEmpty(): This method will return true if Collection Framework variable is empty else return false.

public boolean add(Object): This method is used for adding the value to any Collection Framework variable and they are internally treated as an object of Java.lang.Object.This method will return true if we call with respect to the object of Collection and List interfaces. By adding item unique and duplicate elements. This method also returns true in case of adding a unique element to the set and SortedSet.This method returns false if we add duplicate elements to the Set and SortedSet.Kindly refer below example for better understanding.

c.add(10); 
c.add(12.5f); 
c.add('A'); 
System.out.println(c);//[10,12.5,A]

public Object[] toArray():This method is used for retrieving all the elements of the 1-D Collection framework variable and holding them in Array of an object of java.lang.Object.Example,

c.add(10); 
c.add(12.5f); 
c.add('A'); 
System.out.println(c);//[10,12.5,A]

public Object[] toArray(): This method is used for retrieving all the elements of the 1-D Collection framework variable and holding them in Array of an object of java.lang.Object.Example,

System.out.println(c);//[10,20,30,40] 
Object obj[]=c.toArray();//4 
int sum=0; 
for(int i=10;i<obj.length;i++) 
{ 
Integer io=(Integer)obj[i]; 
int x=io.intValue(); 
System.out.println(x); //10 20 30 40
sum=sum+x;
}
System.out.println(sum); // 100

public Iterator iterator(): This method is used for retrieving all the elements of many collection framework variables and forms the forward direction chain. This method returns an object of Iterator interface and it is by default pointing the first element of forward direction chain.

Iterator interface


1) It is one of the predefined interface present in “java.util.*” package.
2) The purpose of the Iterator interface object is that to retrieve the data from any collection framework variable in the forward direction only but not in other directions.
3) by default an object of iterator interface is pointing just before the first element of any collection framework variable.

Methods of Iterator interface


1) public boolean hasNext(): Its return true, if iterator object having next element else return false.

2) public Object next(): it used to retrieve the next element of any collection framework variable with respect to the Iterator interface object if the method hasNext() return true.

3) public void remove(): it used to remove an element of any collection framework variable with respect to the Iterator interface object if the method hasNext() return true.

Methods of java.util.List


You know that List is the sub-interface of the Collection so that all methods of collection are inherited into the List. It also contains some special methods.
1) public void add(int,Object)
This method is used for adding an element to any collection framework variable either in beginning or at the end or at the middle.

How do you add an element to the list object by using add() method?
l.add(l.size(),”T4Tution”); //added at last

2) public Object removeElementAt(int)
3) public void remove(Object)

These methods are used for removing the elements of List either based on the position or based on content.

public void removeAll(): This method is used for remove all the elements of a List object.
public Object get(int): This method is used for obtaining the value of any collection framework variable by passing valid existing position. If the position is invalid then we get java.lang.indexOutOfBoundException. Example

Object obj=l.get(1); 
//List of object l contain[10,'A',12.5f]
//Valid and store 'A' as an element
System.out.println(obj); //A 
Object obj=l.get(6); 
//Invalid throw java.lang.indexOutOfBoundException

public ListIterator listIterator(): This method is used for retrieving all the elements of any collection framework variable and form a bi-directional chain. This method returns an object of ListIterator interface which is by default Pointing just before the first element of any bi-directional chain. Example

System.out.println(l);//[10,20,30,40] 
ListIterator litr=l.listIterator(); 
while(litr.hasNext()) 
{ 
Object obj=litr.next(); 
System.out.println(obj);//10 20 30 40
} 
while(litr.hasPrevious()) 
{ 
Object obj=litr.previous();//4
Integer io=(Integer)obj; //5
int i=io.getValue();//6
System.out.println(i);
}

ListIterator interface


a) It is one of the predefined subinterfaces of Iterator interface and it present on “java.util.*” package.

b) The purpose of list iterator interface object is to retrieve data from any collection framework variable in both directions forward and backward.

b)By default like an Iterator interface object list ListIterator interface object also pointing just before the first element of any collection framework variable.

Methods of ListIterator


1) public boolean hasNext()
2) public Object next()
3) public int nextIndex()
4) public boolean hasPrevious()
5) public Object previous()
6) public int previousIndex()

a) Method 1,2,3 are inherited from the Iterator interface.

b) Method 4 return true if ListIterator object is having the previous element otherwise it returns false.

c) Method 5 used for obtaining the previous element of any collection framework variable with respect to ListIterator interface object if method 4 return true.

 

What is the difference between Iterator and ListIterator?


Iterator interface object we can retrieve the data from any collection framework variable in the forward direction only but not in backward direction whereas list iterator interface object can retrieve the element or data of any collection framework variable in both forward and backward direction.

Methods of java.util.Set


You know that Set is a sub-interface of the collection so that all the methods of Collection are inherited into Set. The Set interface does not contain any special method except collection interface methods.

Even though collection and set interfaces are same, the Collection method is defining in some predefined class in such a way that duplicate are allowed and displaying the data in random order. Whereas Set method of defining in some other predefined class in such a way that allows unique element and displaying in random order.

In general, even though the methods are the same in both interfaces the but implementation is different predefined classes.

Methods of java.util.SortedSet


You know that SortedSetthe subinterface of Set so that all the methods of the Set are inherited into the SortedSet.Sortedset containing the following special method.

1) public Object first()
2) public Object last()
3) public SortedSet headSet(Object obj)
4) public SortedSet subSet(Object obj1,Object obj2)
5) public SortedSet tailSet(Object obj)

Method 1 and Method 2 are used for the first and last element of the SortedSet object.

Method 4 is used to obtain those value which is less than the target object.

Method 4 is used for obtaining those value which is greater than the target obj1 and less than the target obj2.

Method 5 is used for obtaining those value which is greater than the target value.

Collection Framework classes in java


These classes contain the definition of those abstract methods which are inherited from one-dimensional collection framework interfaces.

One dimensioanl new collection framework classes

One dimensioanl new collection framework class in java hirarchy

The classes 1, 2, 3, and 4 are the predefined one-dimensional collection framework abstract classes and those classes are reserved by the Sun Microsystem developer for the future innovation in one-dimensional collection framework programming. So these classes may not be used directly in the industry.

The classes 5, 6, 7, and 8 are the predefined one-dimensional collection Framework concrete classes and we used directly in the industry based on application.Note: In one-dimensional collection framework programming, we find interface layer abstract classes layer and concrete classes layer.

LinkedList in Java


Admin
We are group of application developer and admin.We are providing good content of information about IT technology and services .

Leave a Reply

Your email address will not be published. Required fields are marked *