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.
- public boolean hasMoreElements()
- public Object nextElement()
Limitations of Enumeration
- It is applicable only for legacy classes and it is not a universal cursor.
- While iterating the objects by using enumeration we can get only read access and we can’t perform removal or replacement operations.
- 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
- It is Introduced in 1.2 version in java.util package.
- We can get Iterator Object for any collection implemented class hence it is universal cursor.
- while iterating objects, we are allowed perform removal operation in addition to read operation.
- Compare to Enumeration interface, Iterator method names are simple and easy to use.
This interface contains the following 3 methods.
- public boolean hasNext()
- public Object next()
- 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
- It has introduced in 1.2 version and it is child interface of Iterator.
- It is useful only for List implemented classes.
- It is a bi-directional cursor,i.e we can move either to the forward or backward direction.
- While Iterating we can perform read,remove,replace and addition of new objects.
- 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.
- public boolean hasNext();
- public boolean hasPrevious();
- public Object next();
- public Object previous();
- public int nextIndex();
If there is no next element it returns size of the list. - public int previousIndex();
If there is no previous element it returns -1 . - public void remove();
- public void set(Object new)
- public void set(Object new)
- public void add(Object new)
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/
ReplyDeleteHi 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