Home  >  Article  >  Java  >  What is the difference between Iterator and ListIterator in Java?

What is the difference between Iterator and ListIterator in Java?

青灯夜游
青灯夜游Original
2019-03-16 15:10:575061browse

Iterator and ListIterator are two of the three cursors in Java, both defined by the collection framework in the Java.UTIL package. So what's the difference between them? The following article will introduce to you the difference between Iterator and ListIterator. I hope it will be helpful to you.

What is the difference between Iterator and ListIterator in Java?

What is Iterator

Iterator represents an iterator and is an interface in the Collection framework; used For traversing collection elements. It allows iterating over each element in the collection one by one, getting elements from the collection, or removing elements from the collection; but it is not possible to modify any element in the collection using an Iterator.

Iterator has an iterator() method, which returns the iterator to the beginning of the collection. Once you get an iterator to the beginning of the collection, and then iterate over the elements in the collection, set up a loop and call hasNext() each time the loop iterates.

If hasNext() returns true, it means that there is the next element in the collection; if it returns false, it means that all elements are traversed. Then inside the loop, you can use next() to get each element in the collection. The next() method returns the next element of the collection.

Disadvantages:

● Using Iterator, you can only move the collection forward.

● Using Iterator, you cannot manipulate or modify the elements in the collection.

What is ListIterator

ListIterator is an interface in the Collection framework; it is used to extend the Iterator interface. Using ListIterator, you can traverse the elements of a collection forward and backward. You can also add, remove, or modify any element in the collection. In short, we can say that it eliminates the disadvantages of Iterator.

The methods of ListIterator are as follows:

● hasNext(): If true is returned, it is confirmed that there are more elements in the collection.

● Next(): Returns the next element of the list.

● NextIndex(): Returns the index of the next element in the list.

● HasPrevious(): Returns true if there is an opposite element in the collection.

● previous(): Returns the previous element in the collection.

● previousIndex(): Returns the index of the previous element in the collection.

● Remove(): Remove elements from the collection.

● Set(): Modify the elements in the set.

● Add(): Add new elements to the collection.

The main difference between Iterator and ListIterator

1. Traversal

Using Iterator, you can traverse all collections, such as Map, List, Set; but can only traverse the elements in the set in the forward direction.

Using ListIterator, you can only traverse objects implemented by List, but you can traverse elements in the collection forward and backward.

2. Add elements

Iterator cannot add elements to the collection; however, ListIteror can add elements to the collection.

3. Modify elements

Iterator cannot modify the elements in the collection; however, ListIterator can use set() to modify the elements in the collection.

4. Index

Iterator cannot obtain the index of the elements in the collection; however, using ListIterator, you can obtain the index of the elements in the collection.

Recommended video tutorials: "Java Tutorial"

The above is the entire content of this article, I hope it will be helpful to everyone's learning. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of What is the difference between Iterator and ListIterator in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn