This method returns true if a collection contains more elements otherwise, false.
hasNext( ) method of Iterator Interface in java.
In the following example we will show you how can hasNext( ) method be implemented in Iterator interface.
Syntax
boolean hasNext( )
This method returns 'true' if a collection keeps more elements otherwise, false.
In this method, you can check for further elements in a collection, one element at a time. This method is used with the Iterator object. It is useful for checking the availability of more elements sequentially in a collection.
Parameter description
This method does not take any argument.
Example of hasNext( ) method
In this example we will show you how does hasNext( ) method work in a collection. This example will help you to understand how can you check for further elements in collection. Through this example we will show you how iterator( ) method is used and how the hasNext( ) method is used for checking more elements with the Iterator object in a collection.
Example:-
package Devmanuals.com; import java.util.List; import java.util.ArrayList; import java.util.Iterator; public class IteratorHasNext { public static void main(String[] args) { ArrayList alist = new ArrayList(); alist.add("21"); alist.add("22"); alist.add("23"); alist.add("24"); alist.add("25"); System.out.println("Elements of list = "+alist); System.out.println("Size of list = "+alist.size()); Iterator itr = alist.itertor(); boolean b =itr.hasNext(); System.out.println("List has elements : "+b); } }
Output :
Elements of list = [21, 22, 23, 24, 25] Size of list = 5 List has elements : true |
[ 0 ] Comments