Get an iterator over an object array in Java
This post will discuss how to get an iterator over an object array in Java. The iterator should be able to iterate over all values in the object array.
In the previous post, we have discussed how to get an iterator over a primitive array in Java. This post will discuss how to get an iterator over an array of objects in Java.
1. Convert array to a list
For Wrapper types or arrays with non-primitive types, we can use Arrays.asList() to get a list backed by the array. Then we can simply use the iterator() method provided by the List interface to get an iterator over the object array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.Arrays; import java.util.Iterator; // Program to get an iterator over an object array in Java class Main { public static void main(String[] args) { // An array of String wrapper type String[] arr = { "A", "B", "C" }; // converting to list Iterator<String> iterator = Arrays.asList(arr).iterator(); iterator.forEachRemaining(System.out::println); } } |
Output:
A
B
C
2. Implement our own iterator
Another solution is to implement our own Iterator. We just need to override two abstract methods of the Iterator interface – hasNext() and next(). This can be done, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import java.util.Iterator; // Program to get an iterator over an object array in Java class Main { public static void main(String[] args) { String[] arr = { "A", "B", "C" }; // Write our own iterator Iterator<String> iterator = new Iterator<String>() { private int i = 0; @Override public boolean hasNext() { return arr.length > i; } @Override public String next() { return String.valueOf(arr[i++]); } }; iterator.forEachRemaining(System.out::println); } } |
Output:
A
B
C
3. Using Guava Library
Guava library Iterators class contains several static utility methods that operate on or return objects of type Iterator. We can use the forArray() method that returns an iterator containing the elements of the object array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import com.google.common.collect.Iterators; import java.util.Iterator; // Program to get an iterator over an object array in Java class Main { public static void main(String[] args) { String[] arr = { "A", "B", "C" }; // Using Guava Iterator<String> iterator = Iterators.forArray(arr); iterator.forEachRemaining(System.out::println); } } |
Output:
A
B
C
4. Using Apache Commons Collections
Apache Commons Collections ObjectArrayIterator class provides an Iterator over an array of objects. The advantage of using this method is that it has a reset() method, which can reset the iterator back to the start whenever needed.
We can also use the ArrayIterator class, but the ObjectArrayIterator will perform better for an object array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import org.apache.commons.collections4.iterators.ObjectArrayIterator; // Program to get an iterator over an object array in Java class Main { public static void main(String[] args) { String[] arr = { "A", "B", "C" }; // Using Apache Commons Collections ObjectArrayIterator iterator = new ObjectArrayIterator(arr); while (iterator.hasNext()) { System.out.println(iterator.next()); } } } |
Output:
A
B
C
That’s all about getting an iterator over an object array in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)