Java Java Tutorial Java Methods Java Classes Java File Handling Java Reference



Java Iterator

Java Iterator is an interface in Java that is used to iterate over a collection of objects. It is a part of the Java Collections Framework and is used to traverse through the elements of a collection in a sequential manner. The Iterator interface provides methods to check if there are more elements in the collection, to retrieve the next element in the collection, and to remove elements from the collection.

The Iterator interface is implemented by all the collection classes in Java, such as ArrayList, LinkedList, HashSet, and TreeSet. The Iterator interface provides a uniform way of iterating over different types of collections, without having to know the specific implementation details of each collection class.

Java Iterator Methods

The Iterator interface provides the following methods:

  • boolean hasNext() - returns true if there are more elements in the collection
  • E next() - returns the next element in the collection
  • void remove() - removes the last element returned by the iterator from the collection

The hasNext() method returns true if there are more elements in the collection, and false otherwise. The next() method returns the next element in the collection, and throws a NoSuchElementException if there are no more elements in the collection. The remove() method removes the last element returned by the iterator from the collection. This method can only be called once per call to next(), and it throws an IllegalStateException if it is called before next() is called, or if it is called more than once per call to next().

Java Iterator Example

Here is an example of using the Iterator interface to iterate over an ArrayList:

<%
ArrayList<String> list = new ArrayList<String>();
list.add("Java");
list.add("Python");
list.add("C++");

Iterator<String> iterator = list.iterator();
while(iterator.hasNext()) {
  String element = iterator.next();
  out.println(element);
}
%>

In this example, we create an ArrayList of strings, and add three elements to it. We then create an Iterator object by calling the iterator() method on the ArrayList object. We use a while loop to iterate over the elements in the ArrayList, and call the next() method to retrieve each element. We then print out each element using the out.println() method.

Java Iterator with Remove Example

Here is an example of using the Iterator interface to iterate over an ArrayList and remove elements:

<%
ArrayList<String> list = new ArrayList<String>();
list.add("Java");
list.add("Python");
list.add("C++");

Iterator<String> iterator = list.iterator();
while(iterator.hasNext()) {
  String element = iterator.next();
  if(element.equals("Python")) {
    iterator.remove();
  }
}

for(String element : list) {
  out.println(element);
}
%>

In this example, we create an ArrayList of strings, and add three elements to it. We then create an Iterator object by calling the iterator() method on the ArrayList object. We use a while loop to iterate over the elements in the ArrayList, and call the next() method to retrieve each element. We then check if the element is equal to "Python", and if it is, we call the remove() method to remove it from the ArrayList. We then use a for-each loop to iterate over the remaining elements in the ArrayList, and print them out using the out.println() method.

Conclusion

The Iterator interface is a powerful tool in Java for iterating over collections of objects. It provides a uniform way of iterating over different types of collections, without having to know the specific implementation details of each collection class. The Iterator interface provides methods to check if there are more elements in the collection, to retrieve the next element in the collection, and to remove elements from the collection. By using the Iterator interface, you can write more efficient and flexible code that can work with different types of collections.

References

Activity