What is Iterator in Java? Java Iterator is an Interface that is used to iterate over a collection of java object one by one. When we want to access collection elements, add/remove or process the elements. In order to do all this processing through a Java program, we should be able to traverse through the collection that we are using. This is where the iterator is used. Java Iterator is a collection framework interface and it is part of the java.util package. In order to use an Iterator, we need to get the iterator object using the iterator() method of the collection interface. Java Iterator supports only Forward Direction Iteration. So it is also know as Uni-Directional Cursor. Java Iterator consists 4 methods : hasNext() next() remove() forEachRemaining() Syntax : List<String> list = new ArrayList<>(); Iterator<String> iterator = list.iterator(); 1. hasNext() Method in Iterator The hasNext() method is used for checking if there's at least one element left
Welcome To Programming Tutorial. Here i share about Java Programming stuff. I share Java stuff with simple examples.