Java iterator example

Abby 127 Published: 12/18/2024

Java iterator example

Here is a simple Java example of using an iterator to traverse a collection:

import java.util.*;

public class Main {

public static void main(String[] args) {

// Create a list of strings

List stringList = new ArrayList();

stringList.add("Hello");

stringList.add("World");

stringList.add("Java");

stringList.add("Iterator");

// Get an iterator for the list

Iterator iterator = stringList.iterator();

// Print out each item in the collection using the iterator

while (iterator.hasNext()) {

System.out.println(iterator.next());

}

}

}

When you run this program, it will print out each of the strings in the list:

Hello

World

Java

Iterator

This is a simple example, but iterators can be used with many types of collections (like sets or maps) and can be used to traverse them in different ways. For example, you could use an iterator to remove all the even numbers from a collection of integers.

Here's how to do that:

import java.util.*;

public class Main {

public static void main(String[] args) {

// Create a list of integers

List intList = new ArrayList();

for (int i = 0; i < 10; i++) {

intList.add(i);

}

// Get an iterator for the list

Iterator iterator = intList.iterator();

// Remove all even numbers from the list using the iterator

while (iterator.hasNext()) {

Integer num = iterator.next();

if (num % 2 == 0) {

iterator.remove();

}

}

// Print out each remaining item in the collection

for (Integer i : intList) {

System.out.println(i);

}

}

}

When you run this program, it will print out a list of all the odd numbers from 0 to 9:

1

3

5

7

9

As you can see, iterators are very powerful and flexible tools that can be used in many different ways.

Java iterator example

Here is a Java iterator example:

import java.util.*;

public class IteratorExample {

public static void main(String[] args) {

// Create a list of strings

List list = new ArrayList<>();

list.add("Apple");

list.add("Banana");

list.add("Cherry");

list.add("Date");

// Create an iterator for the list

Iterator iterator = list.iterator();

// Print all elements using hasNext() and next()

while (iterator.hasNext()) {

System.out.println(iterator.next());

}

}

}

This Java program demonstrates how to use an iterator to iterate over a collection of strings. An Iterator is an interface that allows you to traverse through a collection and access its elements one by one.

Here's what the code does:

We create a list of strings called list. We add some elements to this list: "Apple", "Banana", "Cherry", and "Date". We create an Iterator for the list using its iterator() method. We use a while loop to iterate over the elements in the list. In each iteration, we check if there are more elements left (hasNext()) before accessing the next element (next()). Inside the loop, we print out the current element using System.out.println().

When you run this program, it will output:

Apple

Banana

Cherry

Date

This demonstrates how to use an iterator to traverse through a collection of elements and access each one individually.

In addition to the above example, iterators can be used in other ways such as:

To iterate over a map's key set or entry set. To iterate over a set or list of objects without knowing their order. To skip certain elements during iteration (for instance, you might only want to print every third element).

The following is an example of how to use iterator for map:

import java.util.*;

public class IteratorExample {

public static void main(String[] args) {

// Create a map

Map<String, Integer> map = new HashMap<>();

map.put("Apple", 1);

map.put("Banana", 2);

map.put("Cherry", 3);

// Create an iterator for the map's entry set

Iterator<Map.Entry<String, Integer>> iterator = map.entrySet().iterator();

// Print all entries using hasNext() and next()

while (iterator.hasNext()) {

Map.Entry<String, Integer> entry = iterator.next();

System.out.println("Key: " + entry.getKey());

System.out.println("Value: " + entry.getValue());

}

}

}

When you run this program, it will output:

Key: Apple

Value: 1

Key: Banana

Value: 2

Key: Cherry

Value: 3