Training On Java
Java Collection Framework
Collection Framework
Collection framework contains group of classes and interfaces by using these classes & interfaces we are
representing group of objects as a single entity.
Collection is sometimes called a container. And it is object that groups multiple elements into a single unit.
Collections are used to store, retrieve , manipulate data.
Note :- The root interface of Collection framework is Collection.
Hierarchy Of Collection Framework
ArrayList Collection
ArrayList:- ArrayList is extending AbstractList class and implementing List interface. It is widely used class
in Projects because it is providing functionality and flexibility.
ArrayList Characteristics:-
1. ArrayList is introduces in Java 1.2 Version.
2. ArrayList stores Heterogeneous objects.
3. Inside ArrayList we can store null objects.
4. ArrayList preserved iteration order it means whatever the order we inserted the data in same way output
is printed.
5. Duplicate Objects are allowed.
6. By using cursor we are able to retrieve the data from ArrayList: Iterator, ListIterator.
ArrayList Collection (cont..)
Declaration of ArrayList:-
First of all import [Link] package. Then declare ArrayList in following manner:-
ArrayList <TypeOfArrayList> VariableName=new ArrayList<TypeOfArrayList>();
E.g.
ArrayList<String> al=new ArrayList<String>();
How to add elements in ArrayList:- To add elements in ArrayList we use add() method of ArrayList class.
[Link](“Ravi”);
[Link](“Brijesh”);
Example Application - 1
//Traversal of ArrayList using for – each loop
import [Link].*;
class Example1 {
public static void main(String [] args) {
ArrayList<String> al=new ArrayList<String>();
[Link]("Ajay");
[Link]("Yashi");
[Link]("Brijesh");
[Link]("Rohit");
[Link]("Akhil");
[Link]("List of My Friends");
for (String name:al)
{
[Link](name);
}
}
}
Example Application - 2
//Traversal of ArrayList elements by using iterator.
import [Link].*;
class Example2 {
public static void main(String [] args) {
ArrayList<String> al=new ArrayList<String>();
[Link]("Ajay");
[Link]("Yashi");
[Link]("Brijesh");
[Link]("Rohit");
[Link]("Akhil");
Iterator itr=[Link]();
[Link]("List of My Friends");
while ([Link]())
{
[Link]([Link]());
}
}
}
LinkedList Collection
LinkedList:- Java LinkedList class uses doubly Linked list to store the elements. It provides a linked list
Data Structure. It Inherits the AbstractList class and implements List and Dequeue interfaces.
Java LinkedList class can contain duplicate elements.
Java LinkedList class maintains iteration order.
Java LinkedList class used as List, Stack or Queue.
LinkedList Collection (cont..)
Declaration of LinkedList:-
First of all import [Link] package. Then declare LinkedList in following manner:-
LinkedList <TypeOfLinkedList> VariableName=new LinkedList<TypeOfLenkedList>();
E.g.
LinkedList<String> al=new LinkedList<String>();
How to add elements in LinkedList:- To add elements in LinkedList we use add() method of LinkedList
class.
[Link](“Ravi”);
[Link](“Brijesh”);
Example Application - 3
/*Develop a program in java to create a LinkedList of objects of user defined class Student. The Student class
have rollno, name and fee. Store Student information in LinkedList and display Student information.*/
import [Link].*;
class Student
{
int rollno;
String name;
int fee;
Student(int rollno,String name,int fee)
{
[Link]=rollno;
[Link]=name;
[Link]=fee;
}
}
Example Application – 3 (cont..)
class Test
{
public static void main(String [] args)
{
LinkedList<Student> al=new LinkedList<Student>();
[Link](new Student(1001,"Ravi",45000));
[Link](new Student(1002,"Priya",50000));
[Link](new Student(1003,"Riya",35000));
[Link](new Student(1004,"Rajat",40000));
[Link]("List of students");
for(Student s:al)
{
[Link]([Link]+"\t"+[Link]+"\t"+[Link]);
}
}
}