Week – 17
Lecture Class
CONCEPTS COVERED THIS WEEK
• [Link] package
• Collection Framework
• ArrayList class
• Iterator interface
[Link] Package
• [Link] is the built-in package.
• This package contains the collections framework, legacy
collection classes, even model, date and time facilities,
internationalization(I18N) and other utility classes.
Collection Framework
• Java Vendor has provided some new API (known as Collection
Framework API) from java 2. (before java 2, stack, vector,
Dictionary, Hashtable, Properties was already introduced to
manage the group of elements)
• Collections Framework provides mechanism to store and
manipulate the group of elements.
• Operations like searching, sorting, insertion, deletion etc. can be
performed easily on the group of elements using Collection API.
Collection Framework (contd…)
• Following are the top level interfaces available in Collection Framework
API:
1. Collection
Collection inteface has following 3 sub interfaces:
a. List
List interface has following concrete sub classes:
• ArrayList ( this is the topic for this week)
• LinkedList
• Vector
o Stack
b. Set
c. Queue (from Java 5)
2. Map
3. Iterator
import [Link].*;
class TestCollection1{
public static void main(String args[]){
ArrayList<String> list=new ArrayList<String>();//Creating arraylist
[Link]("Ravi");//Adding object in arraylist
[Link]("Vijay");
[Link]("Ravi");
[Link]("Ajay");
//Traversing list through Iterator
Iterator itr=[Link]();
while([Link]()){
[Link]([Link]());
}
}
}
There are two ways to traverse collection elements:
• By Iterator interface. (shown in above slide)
• By for-each loop.
Iterating Collection through for-each loop
import [Link].*;
class TestCollection2{
public static void main(String args[]){
ArrayList<String> al=new ArrayList<String>();
[Link]("Ravi");
[Link]("Vijay");
[Link]("Ravi");
[Link]("Ajay");
for(String obj:al)
[Link](obj);
}
}
Iterator Interface
• Iterator Interface is an interface available in [Link].
• It is a cursor to access the elements from Collection subclasses
one by one.
• Invoke the iterator() method on collection objects (you can say
ArrayList object) to get the instance of Iterator.
Ex: Iterator it=[Link]();
Iterator Interface (contd…)
• Iterator interface contains following methods.
1. boolean hasNext():
checks whether next element is available or not.
2. Object next():
Moves the pointer and returns elements from collection.
3. Void remove():
Removes the current elements from Collections( or list).
User-defined class objects in Java ArrayList
class Student{
int rollno;
String name;
int age;
Student(int rollno,String name,int age){
[Link]=rollno;
[Link]=name;
[Link]=age;
}
}
import [Link].*;
public class TestCollection3{
public static void main(String args[]){
//Creating user-defined class objects
Student s1=new Student(101,"Sonoo",23);
Student s2=new Student(102,"Ravi",21);
Student s3=new Student(103,"Hanumat",25);
//creating arraylist
ArrayList<Student> al=new ArrayList<Student>();
[Link](s1);//adding Student class object
[Link](s2);
[Link](s3);
//Getting Iterator
Iterator itr=[Link]();
//traversing elements of ArrayList object
while([Link]()){
Student st=(Student)[Link]();
[Link]([Link]+" "+[Link]+" "+[Link]);
}
}
}
Output:
101 Sonoo 23
102 Ravi 21
103 Hanumat 25
Thank You!