Program to Iterate over a Stream with Indices in Java 8
Given a Stream in Java, the task is to iterate over it with the help of indices.
Examples:
Input: Stream = [G, e, e, k, s]
Output: [0 -> G, 1 -> e, 2 -> e, 3 -> k, 4 -> s]Input: Stream = [G, e, e, k, s, F, o, r, G, e, e, k, s]
Output: [0 -> G, 1 -> e, 2 -> e, 3 -> k, 4 -> s, 5 -> F, 6 -> o, 7 -> r, 8 -> G, 9 -> e, 10 -> e, 11 -> k, 12 -> s]
- Method 1: Using IntStream.
- Get the Stream from the array using range() method.
- Map each elements of the stream with an index associated with it using mapToObj() method
- Print the elements with indices
// Java program to iterate over Stream with Indicesimportjava.util.stream.IntStream;classGFG {publicstaticvoidmain(String[] args){String[] array = {"G","e","e","k","s"};// Iterate over the Stream with indicesIntStream// Get the Stream from the array.range(0, array.length)// Map each elements of the stream// with an index associated with it.mapToObj(index -> String.format("%d -> %s",index, array[index]))// Print the elements with indices.forEach(System.out::println);}}chevron_rightfilter_noneOutput:0 -> G 1 -> e 2 -> e 3 -> k 4 -> s
- Method 2: Using AtomicInteger.
- Create an AtomicInteger for index.
- Get the Stream from the array using Arrays.stream() method.
- Map each elements of the stream with an index associated with it using map() method where the index is fetched from the AtomicInteger by auto-incrementing index everytime with the help of getAndIncrement() method.
- Print the elements with indices.
// Java program to iterate over Stream with Indicesimportjava.util.stream.IntStream;importjava.util.*;importjava.util.concurrent.atomic.AtomicInteger;classGFG {publicstaticvoidmain(String[] args){String[] array = {"G","e","e","k","s"};// Create an AtomicInteger for indexAtomicInteger index =newAtomicInteger();// Iterate over the Stream with indicesArrays// Get the Stream from the array.stream(array)// Map each elements of the stream// with an index associated with it.map(str -> index.getAndIncrement() +" -> "+ str)// Print the elements with indices.forEach(System.out::println);}}chevron_rightfilter_noneOutput:0 -> G 1 -> e 2 -> e 3 -> k 4 -> s
Recommended Posts:
- Stream iterate(T,Predicate,UnaryOperator) method in Java with examples
- Program to convert a Map to a Stream in Java
- Program to convert a Set to Stream in Java using Generics
- Program to Convert Stream to an Array in Java
- Program to Convert List to Stream in Java
- Program to convert Boxed Array to Stream in Java
- Program to convert Primitive Array to Stream in Java
- How to iterate any Map in Java
- Difference between Stream.of() and Arrays.stream() method in Java
- Character Stream Vs Byte Stream in Java
- Java Stream | Collectors toCollection() in Java
- Stream In Java
- Java 8 Stream
- Stream.distinct() in Java
- How to get ArrayList from Stream in Java 8
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



