In this article, we are learning about cursors in java and their types and each method of their types with programming examples. This article is very important for interview purposes as well as development purposes.
Cursors in Java:
When we want to get each object one by one from the collection we should go for cursors.
There are three types of cursors available in the Java Collection framework.
- Enumeration
- Iterator
- ListIterator
Enumeration:
- It is introduced in the java 1.0 version.
- Using Enumeration we can get objects One by One from the old Collection Objects (legacy Collection).
Syntex: public Enumeration elements();
Example:
Enumeration e = v.elements(); //v is Vector Object.
In the above example, we are creating an Enumeration object by using the Vector Object.
Enumeration methods:
It defines the following two methods.
- public boolean hasMoreElements();
- public Object nextElement();
Example:
package com.technicalround; import java.util.*; class Demo { public static void main(String[] args) { Vector v = new Vector(); for (int i = 0; i <= 10; i++) { v.addElement(i); //adding element in vector object } System.out.println(v); //[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Enumeration e = v.elements(); while (e.hasMoreElements()) { Integer I = (Integer) e.nextElement(); //getting each element from enumeration if (I % 2 == 0) //finding only even numbers System.out.println(I); } System.out.println(v); //[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] } }
Output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 0 2 4 6 8 10 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
There are some Limitations of Enumeration.
- Enumeration is only Applicable for Legacy Classes and it is Not a Universal Cursor.
- By using enumeration we can perform read operation but we can’t perform remove operation.
To Overcome the above limitations we should go for Iterator.
Iterator:
- Using Iterator we can get objects One by One from the old Collection Objects
- Iterator is Universal Cursor because we can apply Iterator Concept for any Collection Object.
- By using Iterator we can Able to Perform Both Read and Remove Operations.
- We can Create Iterator Object by using the iterator() method of Collection Interface.
Syntex: public Iterator iterator();
Example:
Iterator itr = c.iterator(); //c is any Collection Object.
In the above example, we are creating an Iterator object by using any Collection Object.
Enumeration methods:
It defines the following three methods
- public boolean hasNext();
- public Object next();
- public void remove();
Example:
package com.technicalround; import java.util.*; class Demo { public static void main(String[] args) { ArrayList list = new ArrayList(); for (int i = 0; i <= 10; i++) { list.add(i); //adding number in arrayList object } System.out.println(list); Iterator itr = list.iterator(); //creating iterator object using arrayList object while (itr.hasNext()) { Integer I = (Integer) itr.next(); //get each number from iterator if (I % 2 == 0) System.out.println(I); else itr.remove(); //removing odd numbers } System.out.println(list); } }
Output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 0 2 4 6 8 10 [0, 2, 4, 6, 8, 10]
Limitations of Iterator
There are some Limitations of Iterator
- By using Enumeration and Iterator we can move only towards the forwarding direction and we can’t move backward direction that means it is single direction cursors.
- So it is not supported the BiDirection cursor.
- Iterator only support Read and Remove operations but it can’t perform the addition of new objects and replacing existing objects
To overcome the above limitations we should go for ListIterator.
ListIterator:
- ListIterator is the Child Interface of Iterator.
- By using ListIterator we can Move Either to the Forward Direction OR to the Backward Direction. That is it is a Bi-Directional Cursor.
- By using ListIterator we can able to perform the addition of new objects and replacing existing objects. In addition to Read and Remove operations.
- We can create ListIterator Object by using list iterator().
Syntex: public ListIterator listIterator();
Example:
ListIterator list = l.listIterator(); //l is Any List Object
In the above example, we are creating a ListIterator object by using any List Object.
Note: ListIterator is applicable Only for List Objects that is one limitation.
Enumeration methods:
It defines the following 9 methods.
Forward direction method
- publicbooleanhasNext()
- public Object next()
- publicintnextIndex()
Backward direction method
- publicbooleanhasPrevious()
- public Object previous()
- publicintpreviousIndex()
Extra new method
- public void remove(
- public void set(Object new)
- public void add(Object new)
Example:
package com.technicalround; import java.util.*; class Demo { public static void main(String[] args) { LinkedList list = new LinkedList(); list.add("Niraj"); list.add("Rahul"); list.add("Kunal"); list.add("Sonu"); System.out.println(list); ListIterator listItr = list.listIterator(); while (listItr.hasNext()) { String s = (String) listItr.next(); if (s.equals("Kunal")) listItr.remove(); if (s.equals("Sonu")) listItr.add("Sona"); if (s.equals("Rahul")) listItr.add("Raj"); } System.out.println(list); } }
Output:
[Niraj, Rahul, Kunal, Sonu] [Niraj, Rahul, Raj, Sonu, Sona]
Click for more topic:
- Comparable and Comparator in Java
- Java Set Interface
- Java List Interface
- Java Vector
- Collection Interface
- Collections in java
Hope this was helpful for you. If you have any questions please feel free to leave a comment. Thank you for reading.