Back to: Java Tutorials For Beginners and Professionals
Cursors of Collection Framework in Java
In this article, I am going to discuss the Cursors of Collection Framework in Java with Examples. Please read our previous article where we discussed List Collections in Java. As part of this article, we are going to discuss the following pointers in detail.
- Cursors of Collection Framework in Java
- Iterator Cursors of Collection Framework in Java
- ListIterator Cursors of Collection Framework
- Enumeration Cursors in Java
- Comparison Between Iterator, ListIterator and Enumeration Cursors in Java
Cursors of Collection Framework in Java:
Cursors are mainly used to access the elements of any collection. We have the following three types of cursors in Collection Framework:
- Iterator
- ListIterator
- Enumeration
Iterator Cursors of Collection Framework in Java
This cursor is used to access the elements in the forward direction only. This cursor can be applied to any Collection Interfaces. While accessing the methods we can also delete the elements. An iterator is an interface and we cannot create an object directly. If we want to create an object for Iterator we have to use the iterator() method.
Creation of Iterator
Syntax : Iterator it = c.iterator();
Here, the iterator() method internally creates and returns an object of a class that implements the Iterator Interface.
Methods of Iterator:
- boolean hasNext(): returns true if there is a next element in the array list.
- Object next(): returns the next element in the array list.
- void remove(): removes an element from an array list.
Sample Program to demonstrate Iterator Cursors in Java
import java.util.*; public class IteratorDemo { public static void main (String[]args) { LinkedList < Integer > ll = new LinkedList < Integer > (); ll.add (10); ll.add (25); ll.add (50); ll.add (20); ll.add (25); ll.add (23); ll.add (60); ll.add (25); ll.add (30); ll.add (40); ll.add (15); ll.add (25); System.out.println (ll); Iterator it = ll.descendingIterator (); while (it.hasNext ()) { System.out.println (it.next ()); } Iterator it1 = ll.descendingIterator (); while (it1.hasNext ()) { Integer e = (Integer) it1.next (); //down casting if (e == 25) { it1.remove (); } } System.out.println (ll); } }
Output:
ListIterator Cursors of Collection Framework in Java
This cursor is used to access the elements of Collection in both forward and backward directions. This cursor can be applied only for List category Collections. While accessing the methods we can also add, set, delete elements. ListIterator is an interface and we cannot create Object directly. If we want to create an object for ListIterator we have to use ListIterator() method.
Creation of ListIterator
Syntax : ListIterator it = l.listIterator();
Here, listIterator() method internally creates and returns an object of a class that implements the ListIterator Interface.
Methods of ListIterator:
- boolean hasNext(): return true if the given list iterator contains more number of elements during traversing the given list in the forward direction.
- Object next(): return the next element in the given list. This method is used to iterate through the list.
- boolean hasPrevious(): is used to retrieve and remove the head of the deque.
- Object previous(): return the previous element from the list and moves the cursor in a backward position. The above method can be used to iterate the list in a backward direction.
- int nextIndex(): return the index of the element which is returned by the next() method.
- int previousIndex(): return the index of the given element which is returned by a call to previous. The method may return -1 if and only if the iterator is placed at the beginning of the list.
- void remove(): remove the last element from the list which is returned by next() or previous() method.
- void set(Object obj): is used to replace the last element which is returned by the next() or previous() along with the given element.
- void add(Object obj): is used to insert the given element into the specified list. The element is inserted automatically before the next element may return by the next() method.
Sample Program to demonstrate ListIterator Cursors of Collection Framework in Java
import java.util.LinkedList; import java.util.ListIterator; public class ListIteratorDemo { public static void main (String[]args) { LinkedList < Integer > ll = new LinkedList < Integer > (); ll.add (10); ll.add (25); ll.add (50); ll.add (20); ll.add (25); ll.add (23); ll.add (40); ll.add (15); ll.add (25); System.out.println (ll); ListIterator < Integer > lit = ll.listIterator (); System.out.println ("Elements in Forward Direction"); while (lit.hasNext ()) { System.out.println (lit.next () + " "); } System.out.println ("\n elements in Backward direction"); while (lit.hasPrevious ()) { System.out.println (lit.previous () + " "); } while (lit.hasNext ()) { Object o = lit.next (); Integer e = (Integer) o; if (e == 23) { lit.add (56); } if (e == 15) { lit.set (15000); } if (e == 25) { lit.remove (); } } System.out.println ("\n New List:" + ll); } }
Output:
Enumeration Cursors of Collection Framework in Java
This cursor is used to access the elements of Collection only in a forward direction. This is a legacy cursor and can be applied only for legacy classes like Vector, Stack, Hashtable. Enumeration is also an interface and we cannot create the object directly. If we want to create an object for Enumeration we have to use a legacy method called the elements() method. The Enumeration interface defines the methods by which you can enumerate (obtain one at a time) the elements in a collection of objects.
Creation of Enumeration
Syntax : Enumeration e = v.elements();
Here, the elements() method internally creates and returns an object of a class that implements the Enumeration Interface.
Methods of Enumeration
- boolean hasMoreElements(): When implemented, it must return true while there are still more elements to extract, and false when all the elements have been enumerated.
- Object nextElement(): This returns the next object in the enumeration as a generic Object reference.
Program to demonstrate Enumeration Cursors of Collection Framework in Java
import java.util.*; public class EnumerationDemo { public static void main(String[] args) { Vector<Integer> v = new Vector<Integer>(); v.add(10); v.add(25); v.add(50); v.add(20); v.add(25); v.add(23); v.add(25); System.out.println(v); Enumeration e = v.elements(); while(e.hasMoreElements()) { System.out.println(e.nextElement()+" "); } } }
Output:
Comparison Between Iterator, ListIterator and Enumeration Cursors in Java
In the next article, I am going to discuss Set Collections in Java with examples. Here, in this article, I try to explain Cursors of Collection Framework in Java and I hope you enjoy this Cursors of Collection Framework in Java article.