Difference Between next() and hasNext() Method in Java Collections

15 Sep, 2021
In Java, objects are stored dynamically using objects. Now in order to traverse across these objects is done using a for-each loop, iterators, and comparators. Here will be discussing iterators. The iterator interface allows visiting elements in containers one by one which indirectly signifies retrieval of elements of the collection in forwarding direction only.
This interface compromises of three methods :
next()hasNext()remove()
next()
hasNext()
remove()
(A) hasNext() Method
hasNext() method is used to check whether there is any element remaining in the List. This method is a boolean type method that returns only true and false as discussed as it is just used for checking purposes. The hasNext() methods of the Iterator and List Iterator returns true if the collection object over which is used to check during traversal whether the pointing element has the next element. If not it simply returns false. So,
Return Value:
        True  - if iteration has more elements 
        False - if iteration has no more elements
Return type: boolean
Example:
Java
// Java program to demonstrate// the use of hasNext() method // Importing java input output classes// Importing all classesfrom// java.util packageimport java.io.*;import java.util.*; // Classclass GFG {     // Main driver method    public static void main(String[] args)    {        // Creating an ArrayList        // Declaring the ArrayList        ArrayList<String> list = new ArrayList<String>();         // Adding (appending) new elements at        // the end of the List        // Custom inputs        list.add("Geeks");        list.add("for Geeks");         // Declaring the Iterator        Iterator<String> iterator = list.iterator();         // Printing hasNext() values        // Prints true because iterator has two more values        System.out.println(iterator.hasNext());         // Go to next value using next() method        iterator.next();         // Prints true because iterator has one more values        System.out.println(iterator.hasNext());         // Go to next value using next() method        iterator.next();         // Prints false because iterator has no more values        System.out.println(iterator.hasNext());    }}
  
true
true
false
(B) next() method
If there is an element after where hasNext() has returned false on which some execution is to be performed then this method is used to display that element on which execution is supposed to be carried on with help of this method. The next() methods of the Iterator and List Iterator return the next element of the collection. And if there is a need to remove this element remove() method is used.
Return type: Same as collection such as ArrayList, Linked List, etc.
Return value: The next element in the iteration.
Exception: Throws NoSuchElementException if the iteration has no more elements.
Example:
Java
// Java program to demonstrate// the use of next() method // Importing java input output classesimport java.io.*;// Importing all classes from// java.util packageimport java.util.*; // Classclass GFG {     // Main driver method    public static void main(String[] args)    {        // Creating an ArrayList        // (Declaring ArrayList of String type)        ArrayList<String> list = new ArrayList<String>();         // Adding elements to above List at        // the end of the list        // Custom inputs        list.add("Element1");        list.add("Element2");        list.add("Element3");         // Declaring the Iterator        Iterator<String> iterator = list.iterator();         // Printing values showcasing next() method which        // shows traversal over elements        // only in forward direction         // Prints first element traversed        System.out.println(iterator.next());         // Prints the succeeding element        System.out.println(iterator.next());         // Prints another eleemnt succeeding        // to previous element        System.out.println(iterator.next());    }}
Element1
Element2
Element3
ranjanashutosh2003
Java-Collections
Picked
Technical Scripter 2020
Difference Between
Java
Technical Scripter
Java
Java-Collections
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Difference Between Method Overloading and Method Overriding in Java
Similarities and Difference between Java and C++
Difference between Internal and External fragmentation
Difference between Compile-time and Run-time Polymorphism in Java
Arrays in Java
Split() String method in Java with examples
Arrays.sort() in Java with examples
Object Oriented Programming (OOPs) Concept in Java
Reverse a string in Java