Lecture Week17
Lecture Week17
Lecture Class
CONCEPTS COVERED THIS WEEK
• Java.util package
• Collection Framework
• ArrayList class
• Iterator interface
Java.util Package
• Java.util 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 java.util.*;
class TestCollection1{
public static void main(String args[]){
ArrayList<String> list=new ArrayList<String>();//Creating arraylist
list.add("Ravi");//Adding object in arraylist
list.add("Vijay");
list.add("Ravi");
list.add("Ajay");
//Traversing list through Iterator
Iterator itr=list.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}
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 java.util.*;
class TestCollection2{
public static void main(String args[]){
ArrayList<String> al=new ArrayList<String>();
al.add("Ravi");
al.add("Vijay");
al.add("Ravi");
al.add("Ajay");
for(String obj:al)
System.out.println(obj);
}
}
Iterator Interface
• Iterator Interface is an interface available in java.util.package.
• 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=col.iterator();
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){
this.rollno=rollno;
this.name=name;
this.age=age;
}
}
import java.util.*;
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>();
al.add(s1);//adding Student class object
al.add(s2);
al.add(s3);
//Getting Iterator
Iterator itr=al.iterator();
//traversing elements of ArrayList object
while(itr.hasNext()){
Student st=(Student)itr.next();
System.out.println(st.rollno+" "+st.name+" "+st.age);
}
}
}
Output:
101 Sonoo 23
102 Ravi 21
103 Hanumat 25
Thank You!