Java Hasnext Iterator


 

Java Hasnext Iterator

In this section of tutorial we will learn how to use the HashNext method of the Iterator interface. We will create an example to display the contents of the ArrayList using hashNext

In this section of tutorial we will learn how to use the HashNext method of the Iterator interface. We will create an example to display the contents of the ArrayList using hashNext

  • Java HasNext Iterator is the method of the Iterator Interface.
  • It returns the boolean.So it is used in the loop to traverse the next value.
  • In case of false vale loop terminates.


Java HasNext Iterator Example
import java.util.ArrayList;
import java.util.Iterator;

public class hasnext {

	public static void main(String[] args) {
		String months[] = { "jan", "feb", "march", "april", "may", "june" };
		ArrayList list = new ArrayList();
		for (String m : months) {
			list.add(m);
		}
		Iterator iterator = list.iterator();
		while (iterator.hasNext()) {
			System.out.println(iterator.next());
		}
	}
}

Output

jan feb march april may june

Ads