WRITTEN BY: supportmymoto.com STAFF
The Iterator and ListIterator are the 2 among the many three cursors of Java. Each Iterator and ListIterator are outlined by Assortment Framework in Java.Util package deal. ListIterator is the kid interface of Iterator interface.
The most important distinction between Iterator and ListIterator is that Iterator can traverse the weather within the assortment solely in ahead path whereas, the ListIterator can traverse the weather in a set in each the ahead in addition to the backwards path.
Allow us to talk about some extra variations between Iterator and ListIterator with the assistance of comparability chart proven beneath.
Content material: Iterator Vs ListIterator
- Comparability Chart
- Key Variations
|Foundation for comparability||Iterator||ListIterator|
|Fundamental||Iterator can traverse the weather in a set solely in ahead path.||ListIterator can traverse the weather in a set in ahead in addition to the backwards path.|
|Add||Iterator is unable so as to add parts to a set.||ListIteror can add parts to a set.|
|Modify||Iterator cannot modify the weather in a set.||ListIterator can modify the weather in a set utilizing set().|
|Traverse||Iterator can traverse Map, Checklist and Set.||ListIterator can traverse Checklist objects solely.|
|Index||Iterator has no methodology to acquire an index of the ingredient in a set.||Utilizing ListIterator, you possibly can get hold of an index of the ingredient in a set.|
Definition of Iterator
Iterator is an interface in a Assortment Framework. It’s used to traverse via the gathering parts. Iterator means that you can iterate every ingredient within the assortment one after the other, get hold of parts from the gathering or take away parts from the gathering. You may discover you cannot modify any ingredient in a set utilizing Iterator.
Iterator has a way iterator() that returns the iterator to the beginning of the gathering. When you get an iterator to the beginning of a set then to traverse the weather within the assortment set up a loop that calls hasNext() every time the loop iterates. The hasNext() if returns true it implies that the following ingredient is there within the assortment, and if it returns false then it means all the weather are traversed. Then contained in the loop, you possibly can get hold of every ingredient in a Assortment utilizing subsequent(). The following() methodology returns the following ingredient of the gathering.
- Utilizing Iterator you possibly can traverse a set within the ahead path solely.
- Utilizing Iterator, you cannot manipulate or modify the weather in a set.
Definition of ListIterator
ListIterator is an interface in a Assortment framework, and it extends the Iterator interface. Utilizing ListIterator, you possibly can traverse the weather of the gathering in each ahead and backwards instructions. You too can add, take away or modify any ingredient within the assortment. Briefly, we are able to say it removes drawbacks of Iterator.
The strategies of ListIterator are as observe:
- hasNext(): If returns true confirms that there are extra parts in a set.
- subsequent(): Returns subsequent parts of the checklist.
- nextIndex(): Returns the index of subsequent parts within the checklist.
- hasPrevious(): Returns true if there are parts within the reverse path in a set.
- earlier(): Returns the earlier ingredient in a set.
- previousIndex(): Returns index of the earlier ingredient in a set.
- take away(): Delete the ingredient from a set.
- set(): Modifies the ingredient in a set.
- add(): Provides the brand new ingredient in a set.
Key Variations Between Iterator and ListIterator
- The fundamental distinction between Iterator and ListIterator is that each being cursor, Iterator can traverse parts in a set solely in ahead path. Then again, the ListIterator can traverse in each ahead and backward instructions.
- Utilizing iterator you cannot add any ingredient to a set. However, by utilizing ListIterator you possibly can add parts to a set.
- Utilizing Iterator, you cannot take away a component in a set the place, as You may take away a component from a set utilizing ListIterator.
- Utilizing Iterator you possibly can traverse all collections like Map, Checklist, Set. However, by ListIteror you possibly can traverse Checklist applied objects solely.
- You may retrieve an index of a component utilizing Iterator. However as Checklist is sequential and index-based you possibly can retrieve an index of a component in utilizing ListIterator.
You need to use ListIterator when you need to significantly traverse a Checklist object in each ahead and reverse path. Else you should utilize Iterator because it helps all sort assortment objects.