Java Iterator

Java Iterator is an interface in the Java Collections Framework that provides a way to traverse the elements of a collection (such as ArrayList, HashSet, LinkedList, etc.) one by one. In this answer, we will discuss the various aspects of Iterator in detail.

Creating an Iterator

To create an Iterator for a collection, you need to call the iterator() method on the collection object:

Iterator<datatype> iterator = collection.iterator();

Here, datatype is the type of element that you are storing in the collection, and collection is the collection object that you want to iterate over.

Iterating over Elements using Iterator

Once you have an Iterator, you can use the hasNext() and next() methods to iterate over the elements of the collection:

while (iterator.hasNext()) {
    datatype element = iterator.next();
    // Do something with element
}

Here, hasNext() returns true if there are more elements in the collection, and false otherwise. next() returns the next element in the collection.

Note that if you try to call next() when there are no more elements in the collection, a NoSuchElementException will be thrown.

Removing Elements using Iterator

You can also remove elements from a collection while iterating over it using an Iterator. To remove the current element that the Iterator is pointing to, you can use the remove() method:

iterator.remove();

Note that you can only call remove() once per call to next(). Also, if you try to call remove() before calling next(), an IllegalStateException will be thrown.

Iterator Characteristics

Some important characteristics of Iterator are:

  1. Iterators are used to traverse collections: An iterator is used to traverse a collection of elements such as ArrayList, LinkedList, HashSet, TreeSet, etc.
  2. Iterator is an interface: In Java, an iterator is an interface that belongs to the java.util package. It provides methods to iterate over a collection of elements.
  3. Iterators are fail-fast: Iterators in Java are designed to be fail-fast, meaning they will throw a ConcurrentModificationException if the collection is modified while iterating.
  4. Iterators have three basic methods: The three basic methods of an iterator are hasNext(), next(), and remove(). The hasNext() method returns true if there are more elements in the collection to iterate over. The next() method returns the next element in the collection, and the remove() method removes the last element returned by the iterator from the collection.
  5. Iterators are unidirectional: Iterators in Java can only traverse a collection in one direction. Once an iterator has moved to the next element, it cannot move back to the previous element.
  6. Iterators can be used in enhanced for loops: An enhanced for loop can be used with an iterator to simplify iterating over a collection of elements.
  7. Iterators support read-only access: Some implementations of iterators in Java, such as the iterator returned by Collections.unmodifiableList(), only support read-only access to the collection. Any attempt to modify the collection through the iterator will result in an UnsupportedOperationException.
Wordpress Social Share Plugin powered by Ultimatelysocial
Wordpress Social Share Plugin powered by Ultimatelysocial