Iterate over a Stream with indices in Java
This post will discuss how to iterate over a stream with indices in Java.
We know that we cannot directly access elements of a stream in Java 8 and above by using their indices, unlike in lists and arrays, but there are few workarounds in Java that makes this feasible. These are discussed below in detail:
1. Using IntStream
We can take advantage of the fact that elements in an array or a collection are accessible using an index. The idea is to get an IntStream of array indices, ranging from 0 to n-1, where n is the array’s length. Then we use the mapToObj(mapper) method to returns a stream of string, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.util.stream.IntStream; // Iterate over a stream with indices in Java 8 and above class Main { public static void main(String[] args) { String[] array = { "A", "B", "C", "D" }; IntStream.range(0, array.length) .mapToObj(index -> String.format("%d -> %s", index, array[index])) .forEach(System.out::println); } } |
Output:
0 -> A
1 -> B
2 -> C
3 -> D
We can also write the above code as:
|
1 2 |
IntStream.range(0, array.length) .forEach(index -> System.out.println(index + " -> " + array[index])); |
2. Using AtomicInteger
We can also use AtomicInteger class in Java. The idea is to get a sequential stream of an array or the collection and call getAndIncrement() to get the next index, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.Arrays; import java.util.concurrent.atomic.AtomicInteger; // Iterate over a stream with indices in Java 8 and above class Main { public static void main(String[] args) { String[] array = { "A", "B", "C", "D" }; AtomicInteger index = new AtomicInteger(); Arrays.stream(array) .map(str -> index.getAndIncrement() + " -> " + str) .forEach(System.out::println); } } |
Output:
0 -> A
1 -> B
2 -> C
3 -> D
We can also write the above code as:
|
1 2 3 |
AtomicInteger index = new AtomicInteger(); Arrays.stream(array) .forEach(str -> System.out.println(index.getAndIncrement() + " -> " + str)); |
Please note that this approach won’t work with parallel streams as the processing order of the elements in the stream might differ from the original order of the array elements.
3. Using Guava’s Streams Class
We know that the Guava Streams class provides several static utility methods related to stream instances. One such method is mapWithIndex, which returns a stream consisting of the results of applying the specified method to the elements of the stream and their indices in the stream.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import com.google.common.collect.Streams; import java.util.Arrays; // Iterate over a stream with indices in Java 8 and above class Main { public static void main(String[] args) { String[] array = { "A", "B", "C", "D" }; Streams.mapWithIndex(Arrays.stream(array), (str, index) -> index + " -> " + str) .forEach(System.out::println); } } |
Output:
0 -> A
1 -> B
2 -> C
3 -> D
That’s all about iterating over a Stream with indices 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 :)