Program to Iterate over a Stream with Indices in Java 8 - GeeksforGeeks

11 Dec, 2018
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() methodPrint the elements with indices// Java program to iterate over Stream with Indices  import java.util.stream.IntStream;  class GFG {    public static void main(String[] args)    {          String[] array = { "G", "e", "e", "k", "s" };          // Iterate over the Stream with indices        IntStream              // 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);    }}Output:0 -> G
1 -> e
2 -> e
3 -> k
4 -> s

Get the Stream from the array using range() method.Map each elements of the stream with an index associated with it using mapToObj() methodPrint the elements with indices
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 Indices  import java.util.stream.IntStream;  class GFG {    public static void main(String[] args)    {          String[] array = { "G", "e", "e", "k", "s" };          // Iterate over the Stream with indices        IntStream              // 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);    }}
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 Indices  import java.util.stream.IntStream;import java.util.*;import java.util.concurrent.atomic.AtomicInteger;  class GFG {    public static void main(String[] args)    {          String[] array = { "G", "e", "e", "k", "s" };          // Create an AtomicInteger for index        AtomicInteger index = new AtomicInteger();          // Iterate over the Stream with indices        Arrays              // 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);    }}Output:0 -> G
1 -> e
2 -> e
3 -> k
4 -> s

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.
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 Indices  import java.util.stream.IntStream;import java.util.*;import java.util.concurrent.atomic.AtomicInteger;  class GFG {    public static void main(String[] args)    {          String[] array = { "G", "e", "e", "k", "s" };          // Create an AtomicInteger for index        AtomicInteger index = new AtomicInteger();          // Iterate over the Stream with indices        Arrays              // 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);    }}
0 -> G
1 -> e
2 -> e
3 -> k
4 -> s

Java 8
java-basics
java-stream
Java-Stream-programs
Java
Java Programs
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Object Oriented Programming (OOPs) Concept in Java
HashMap in Java with Examples
Interfaces in Java
How to iterate any Map in Java
ArrayList in Java
Initializing a List in Java
Convert a String to Character Array in Java
Java Programming Examples
Convert Double to Integer in Java
Implementing a Linked List in Java using Class