package collection.view; import java.util.

*; public class IteratorDemo { public static void main(String[] args) { // CREATING AN ARRAYLIST ArrayList<String> al=new ArrayList<String>(); // STORING ELEMENTS IN ARRAYLIST al.add("A"); al.add("D"); al.add("V"); al.add("C"); al.add("B"); al.add("J"); // PRINTING THE ELEMENTS VIA ITERATOR System.out.print("Original Element:"); Iterator<String> itr=al.iterator(); while(itr.hasNext()) { String element=itr.next(); System.out.print(element+" "); } System.out.println(); // MODIFYING THE ELEMENTS OF THE ARRAYLIST USING LISTITERATOR ListIterator<String> litr=al.listIterator(); System.out.println("Modifying the collection elements:"); while(litr.hasNext()) { String element =litr.next(); litr.set(element+"+"); } // OBTAINING THE ITERATOR AGAIN litr=al.listIterator(); // PRINTING THE MODIFIED ELEMENTS USING ITERATOR System.out.print("Modified elements:"); while(litr.hasNext()) { String element=litr.next(); System.out.print(element); } System.out.println(); // PRINTING THE MODIFIED ELEMENTS USING LISTITERATOR

out. dept.System. public class { public public String Student implements Comparable<Student> int id. while(litr. System. String name.out.String name. } . this. } public String toString() { return "Student [id=" + id + ".previous(). } return res. } } } package collection. name=" + name + ". } else if(this.id) { res=-1.name=name.print("Modified element backward:").id=id.domain. if(this.id>id) { res=1. this.dept=dept.print(element).id<s.String dept) { this. } public int compareTo(Student s) { int res=0. public Student(int id. course=" + dept+ "]\n".hasPrevious()) { String element=litr.

public class StudentStore { ArrayList<Student> al=new ArrayList<Student>().Student.} package collection. public void register(Student s) { al.iterator().add(s).util.out. } public void remove(int id) { Iterator<Student> itr=al.println(al). import collection. while(itr. } public void view() { System.domain.*.hasNext()) .dao. import java.

out.println(al).out.out. System. } } } public void sort() { Collections.ao).remove().println(al).id) { itr.println(al). System.{ Student element= itr. } } . //al.sort(al. if(id==element.remove(element).sort(al).next(). System. } public void sort(Comparator<Student> ao) { Collections.

myList.Iterator.util.next()). while(itr. myList.hasNext()){ System. myList. import java.List. myList.util.add("Perl").add("C++").util.add("Java"). } } } . myList.add("Oracle"). import java.import java. public class MyCollectionIterator { public static void main(String a[]){ List<String> myList = new ArrayList<String>().println(itr.out.iterator().add("Unix"). Iterator<String> itr = myList.ArrayList.