Program to Iterate over a Stream with Indices in Java 8 Last Updated : 11 Dec, 2018 Comments Improve Suggest changes Like Article Like Report 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 // 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 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 // 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 Comment More infoAdvertise with us Next Article Program to Iterate over a Stream with Indices in Java 8 R RishabhPrabhu Follow Improve Article Tags : Java Practice Tags : Java Similar Reads Program to convert a Set to Stream in Java using Generics Java Set is a part of java.util package and extends java.util.Collection interface. It does not allow the use of duplicate elements and at max can accommodate only one null element. A Stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result. 3 min read Convert an Iterator to Stream in Java Given an Iterator, the task is to convert it into Stream in Java. Examples: Input: Iterator = {1, 2, 3, 4, 5} Output: {1, 2, 3, 4, 5} Input: Iterator = {'G', 'e', 'e', 'k', 's'} Output: {'G', 'e', 'e', 'k', 's'} Approach: Get the Iterator. Convert the iterator to Spliterator using Spliterators.split 1 min read Convert an Iterable to Stream in Java Given an Iterable, the task is to convert it into Stream in Java. Examples: Input: Iterable = [1, 2, 3, 4, 5] Output: {1, 2, 3, 4, 5} Input: Iterable = ['G', 'e', 'e', 'k', 's'] Output: {'G', 'e', 'e', 'k', 's'} Approach: Get the Iterable. Convert the Iterable to Spliterator using Iterable.spliterat 1 min read Stream iterate(T,Predicate,UnaryOperator) method in Java with examples The iterate(T, java.util.function.Predicate, java.util.function.UnaryOperator) method allows us to iterate stream elements till the specified condition. This method returns a sequential ordered Stream produced by iterative application of the given next function to an initial element, conditioned on 2 min read Stream.of(T t) in Java with examples Stream of(T t) returns a sequential Stream containing a single element i.e, a singleton sequential stream. A sequential stream work just like for-loop using a single core. On the other hand, a Parallel stream divide the provided task into many and run them in different threads, utilizing multiple co 2 min read Intermediate Methods of Stream in Java The Stream API is used to process collections of objects. A stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result. The method provided by the stream are broadly categorized as Intermediate MethodsTerminal Methods Here, we will be discussin 6 min read Set iterator() method in Java with Examples The java.util.Set.iterator() method is used to return an iterator of the same elements as the set. The elements are returned in random order from what present in the set. Syntax: Iterator iterate_value = Set.iterator(); Parameters: The function does not take any parameter. Return Value: The method i 1 min read How to print elements of a Stream in Java 8 Introduced in Java 8, the Stream API is used to process collections of objects. A stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result. The features of Java stream are â A stream is not a data structure instead it takes input from the Col 5 min read Vector iterator() method in Java with Examples iterator() method of Vector class that is present inside java.util package is used to return an iterator of the same elements as that of the Vector. The elements are returned in random order from what was present in the vector. Syntax: Iterator iterate_value = Vector.iterator(); Parameters: The func 2 min read Generate Infinite Stream of Integers in Java Given the task is to generate an infinite sequential unordered stream of integers in Java. This can be done in following ways: Using IntStream.iterate(): Using the IntStream.iterate() method, iterate the IntStream with i by incrementing the value with 1. Print the IntStream with the help of forEach( 2 min read Like