Share on Google+Share on Google+

Java Hasnext Iterator

Advertisement
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

Advertisements

Posted on: November 3, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.