

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to Iterate over a Stream with Indices in Java 8
To iterate over a Stream with Indices in Java 8, the code is as follows −
Example
import java.util.stream.IntStream; import java.util.*; import java.util.concurrent.atomic.AtomicInteger; public class Demo{ public static void main(String[] args){ String[] my_array = { "T", "h", "i", "s", "s","a", "m", "p", "l", "e" }; AtomicInteger my_index = new AtomicInteger(); System.out.println("The elements in the string array are :"); Arrays.stream(my_array).map(str -> my_index.getAndIncrement() + " -> " + str).forEach(System.out::println); } }
Output
The elements in the string array are : 0 -> T 1 -> h 2 -> i 3 -> s 4 -> s 5 -> a 6 -> m 7 -> p 8 -> l 9 -> e
A class named Demo contains the main function. In this main function, an array of string type is declared and the AtomicInteger instance is created using the AtomicInteger class. The ‘getAndIncrement’ function is used to iterate over the elements of the string array and every element iterated over is printed on to the console.
- Related Questions & Answers
- Program to iterate over a List using Java 8 Lambda
- Java Program to Iterate over a HashMap
- Java Program to Iterate over a Set
- Java Program to Iterate over enum
- Java Program to Iterate over an ArrayList
- How to iterate over a Java list?
- How to iterate over a list in Java?
- Java 8 Stream Terminal Operations
- Java Program to Iterate over ArrayList using Lambda Expression
- C# program to iterate over a string array with for loop
- How to iterate List Using Java Stream API?
- How to Convert a Java 8 Stream to an Array?
- Python program to iterate over multiple lists simultaneously?
- Iterate over a dictionary in Python
- Iterate over a list in Python
Advertisements