Iterator in java is used to iterate Collection to retrieve elements one by one.
Iterator interface defines these two methods :
This can be understand by a simple code snippet shared below:
ArrayList names = new ArrayList();
names.add("jack");
names.add("james");
names.add("Don");
Iterator it = names.iterator();
while(it.hasNext()) { // it.hasNext() checking if iterator has next value?
String name= (String)it.next(); // it.next() returning the next value
System.out.println(name);
}
Hope this will clear your doubt.
Happy Coding!!!