24/03/25 ----------- Cursors in Java Collection Framework ----------------------------------------- Q)What is a cursor? =>A cursor is an iterator used to traverse a collection. Note:- Traversing referes to the process of visiting and accessing each element of a data structure. Q)What are the different types of cursors in Java collection framework? =>There are 4 types of cursors. 1)Enumeration 2)Iterator 3)ListIterator 4)Spliterator Q)Explain about Enumeration. =>java.util.Enumeration interface provides methods using which we can enumerate(obtain one by one) the elements of the collection. =>This legacy cursor was used in legacy collections for eg. Vector, Stack etc. =>This interface has 3 methods. 1)hasMoreElements() 2)nextElement() 3)default Iterator asIterator() (used to convert Enumeration into Iterator) Q)What are the limitations of Enumeration? 1)can be used for read-only operations 2)unidirectional traversing 3)performance problem for read operation also Q)Explain about Iterator. =>java.util.Iterator is an interface. =>It is known as a universal cursor.I.e. it can be used to iterate any collection(list based,set based or map based). =>It is a unidirectional cursor. I.e. it can traverse the elements of collection in forward direction only. =>We can perform two kinds of operations only on the collection using Iterator. 1)reading of elements of the collection 2)removing the elements of the collection =>Iterator has the following methods. 1)boolean hasNext() 2)E next() 3)void remove() 4)void forEachRemaining(lambda expression) Q)Example program on Iterator. Q)What are the limitations of Iterator? import java.util.*; class IteratorExample{ public static void main(String[] args){ ArrayList list=new ArrayList<>(); Collections.addAll(list,10,20,25,30,35); Iterator iterator1=list.iterator(); Iterator iterator2=list.iterator();//to traverse again System.out.print("Numbers in the list:"); iterator1.forEachRemaining(n->System.out.print(n+" ")); while(iterator2.hasNext()){ if(iterator2.next() % 2==0){ iterator2.remove(); } } System.out.println(); System.out.print("Numbers in the list after removal:"); for(int n:list) System.out.print(n+" "); } } Q)What are the limitations of Iterator? 1)traverses the collection only in forward direction 2)All CRUD operations can't be perfroming on collection. I.e. read and remove only allowed Note:- ListIterator overcomes the limitations of Iterator. Q)Explain about ListIterator. =>java.util.ListIterator is the sub interface of Iterator. =>by calling listIterator() method on the list based collection object, ListIterator object can be created. =>It is a bidirectional cursor. I.e. using this cursor, we can traverse the list based collection in forward and backward direction. =>It can be used only with list based collections. =>Using ListIterator, all CRUD oeprations can be performed on the collection Q)Example program on ListIterator. import java.util.*; class ListIteratorExample{ public static void main(String[] args){ ArrayList list=new ArrayList<>(); Collections.addAll(list,10,20,30,40); ListIterator li=list.listIterator(); System.out.print("Elements of the list:"); while(li.hasNext()){ System.out.print(li.next()+" "); } System.out.println();//blank line System.out.print("Elements of the list in backward direction:"); while(li.hasPrevious()){ System.out.print(li.previous()+" "); } while(li.hasNext()){ if(li.nextIndex()==0){ li.set(18);//Updation } if(li.nextIndex()==2){ li.add(25);//storing data into the collection } li.next(); }//while System.out.println(); System.out.println(list); } }