Java tutorials for Beginners – Java Iterator

Hits: 3

(Java programming Example for Beginners)

Java Iterator Interface

In this tutorial, we will learn about the Java Iterator interface with the help of an example.

The Iterator interface of the Java collections framework allows us to access elements of a collection. It has a subinterface ListIterator.

The Listiterator interface extends the Java Iterator interface.

All the Java collections include an iterator() method. This method returns an instance of iterator used to iterate over elements of collections.


Methods of Iterator

The Iterator interface provides 4 methods that can be used to perform various operations on elements of collections.

  • hasNext() – returns true if there exists an element in the collection
  • next() – returns the next element of the collection
  • remove() – removes the last element returned by the next()
  • forEachRemaining() – performs the specified action for each remaining element of the collection

 


Example: Implementation of Iterator

In the example below, we have implemented the hasNext()next(), remove() and forEachRemining() methods of the Iterator interface in an array list.


import java.util.ArrayList;
import java.util.Iterator;

class Main{
    public static void main(String[] args){
        // Creating an ArrayList
        ArrayList<Integer> numbers = new ArrayList<>();
        numbers.add(1);
        numbers.add(3);
        numbers.add(2);
        System.out.println("ArrayList: " + numbers);

        // Creating an instance of Iterator
        Iterator<Integer> iterate = numbers.iterator();

        // Using the next() method
        int number = iterate.next();
        System.out.println("Accessed Element: " + number);

        // Using the remove() method
        iterate.remove();
        System.out.println("Removed Element: " + number);

        System.out.print("Updated ArrayList: ");

        // Using the hasNext() method
        while(iterate.hasNext()) {
            // Using the forEachRemaining() method
            iterate.forEachRemaining((value) -> System.out.print(value + ", "));
        }
    }
}
ArrayList: [1, 3, 2]
Acessed Element: 1
Removed Element: 1
Updated ArrayList: 3, 2,

In the above example, notice the statement:

iterate.forEachRemaining((value) -> System.put.print(value + ", "));

 

Now the method will print all the remaining elements of the array list.

 

 

Java tutorials for Beginners – Java Iterator

Sign up to get end-to-end “Learn By Coding” example.


Two Machine Learning Fields

There are two sides to machine learning:

  • Practical Machine Learning:This is about querying databases, cleaning data, writing scripts to transform data and gluing algorithm and libraries together and writing custom code to squeeze reliable answers from data to satisfy difficult and ill defined questions. It’s the mess of reality.
  • Theoretical Machine Learning: This is about math and abstraction and idealized scenarios and limits and beauty and informing what is possible. It is a whole lot neater and cleaner and removed from the mess of reality.
Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.