Cursors in Java's Collection framework

Java Cursor

A Java Cursor is an Iterator, which is used to iterate or traverse or retrieve a Collection or Stream object’s elements one by one.

There are four type of cursors provided by Java:



  • Enumeration
  • Iterator
  • ListIterator
  • Spliterator(Java  1.8)


Note : Java Enumeration interface is deprecated, it is not recommended to use this in our applications.


Enumeration

This interface has introduced in 1.0 version it contains the following 2 methods.
  1. public boolean hasMoreElements()
  2. public Object nextElement()
Limitations of Enumeration
  1. It is applicable only for legacy classes and it is not a universal cursor.
  2. While iterating the objects by using enumeration we can get only read access and we can’t perform removal or replacement operations.
  3. It supports only Forward Direction iteration. That’s why it is also know as Uni-Directional Cursor.
To overcome these problems we should go for Iterator interface.

Iterator

  1. It is Introduced in 1.2 version in java.util package.
  2. We can get Iterator Object for any collection implemented class hence it is universal cursor.
  3. while iterating objects, we are allowed perform removal operation in addition to read operation.
  4. Compare to Enumeration interface, Iterator method names are simple and easy to use.
This interface contains the following 3 methods.
  1. public boolean hasNext()
  2. public Object next()
  3. public void remove()

Limitation of Iterator

  • Enumeration and Iterator are single directional cursors. They can always move towards forward direction only.
  • By using Iterator we can perform read and remove operations. And we can’t perform any replace or addition of new objects.
  • Compare to Spliterator, it does NOT support iterating elements parallel that means it supports only Sequential iteration.
  • Compare to Spliterator, it does NOT support better performance to iterate large volume of data.
To over come these limitations we should go for ListIterator interface.



ListIterator


  1. It has introduced in 1.2 version and it is child interface of Iterator.
  2. It is useful only for List implemented classes.
  3. It is a bi-directional cursor,i.e we can move either to the forward or backward direction.
  4. While Iterating we can perform read,remove,replace and addition of new objects.
  5. It has no current element; its cursor position always lies between the element that would be returned by a call to previous() and the element that would be returned by a call to next().
This interface defines the following 9 methods.
  1. public boolean hasNext();
  2. public boolean hasPrevious();
  3. public Object next();
  4. public Object previous();
  5. public int nextIndex();
    If there is no next element it returns size of the list.
  6. public int previousIndex();
    If there is no previous element it returns -1 .
  7. public void remove();
  8. public void set(Object new)
  9. public void set(Object new)
  10. public void add(Object new)

Comments

  1. hi nice post easy to read and understand we are the top salesforce marketing cloud services firm offering a wide range of Salesforce services like marketing cloud services, commerce cloud services, and salesforce lighting services for more information visit https://cloudanalogy.com/

    ReplyDelete
  2. Hi nice post easy to read and understand we are the top salesforce marketing cloud services firm offering a wide range of Salesforce services like marketing cloud services, commerce cloud services, salesforce development services and more

    ReplyDelete

Post a Comment