0

I have a code which is as follows:

Arrays.stream(myArray).forEach(item -> System.out.println(item));

Does streams in Java have any ability of getting the index of the current item that I can use them inside the lambda expression?

For example in JavaScript we have this kind of code which can give us the index:

myArray.forEach((index, item) => console.log(`${index} ${item}`));

Do we have any equivalent in Java?

Stefan Zobel
  • 2,847
  • 7
  • 25
  • 34
M.barg
  • 11
  • 2
  • 1
    Does this answer your question? [Get Index while iterating list with stream](https://stackoverflow.com/questions/49080255/get-index-while-iterating-list-with-stream) – Bashir Jun 26 '20 at 16:43
  • 1
    You can check this https://stackoverflow.com/questions/18552005/is-there-a-concise-way-to-iterate-over-a-stream-with-indices-in-java-8 – Harmandeep Singh Kalsi Jun 26 '20 at 16:44
  • 2
    Does this answer your question? [Is there a concise way to iterate over a stream with indices in Java 8?](https://stackoverflow.com/questions/18552005/is-there-a-concise-way-to-iterate-over-a-stream-with-indices-in-java-8) – mohammedkhan Jun 26 '20 at 16:46

2 Answers2

0

Yes to answer your question, in Java you can iterate over a stream with indices.

This is a very simple basic code which shows how to get the index :

import java.util.stream.IntStream;

public class Main {
    public static void main(String[] args) {

        String[] array = { "Goat", "Cat", "Dog", "Crow", "Snake" }; 
        
        IntStream.range(0, array.length)
                 .mapToObj(index -> String.format("%d -> %s", index, array[index]))
                 .forEach(System.out::println); 
                 
    }
    
}

Output :

0 -> Goat                                                                                                                                                  
1 -> Cat                                                                                                                                                   
2 -> Dog                                                                                                                                                   
3 -> Crow                                                                                                                                                  
4 -> Snake
Som
  • 1,342
  • 1
  • 11
  • 35
0

You can do:

Arrays.stream(array).forEach(item -> 
        System.out.println(item + " " + Arrays.asList(array).indexOf(item)));

Update

If you have duplicate items, one option is to use AtomicInteger:

AtomicInteger index = new AtomicInteger(0);
Arrays.stream(array).forEach(item -> 
    System.out.println(item + " " + index.getAndIncrement()));

If you are allow to use Guava you can do:

Streams.mapWithIndex(Arrays.stream(array),
        (item, index) -> item + " " + index)
        .forEach(System.out::println);
Oboe
  • 2,546
  • 2
  • 7
  • 16
  • What happens if there are duplicate items in the array? – Federico klez Culloca Jun 26 '20 at 18:15
  • @Federico klez Culloca, from List javadoc: *returns the lowest index i such that Objects.equals(o, get(i))*. – Oboe Jun 26 '20 at 18:18
  • Let me rephrase that: what happens *with your code* when there's a duplicate? I'm trying to point out that your code doesn't take that into account and will behave in a way that's not coherent (i.e., you'll get wrong indexes at some point). – Federico klez Culloca Jun 26 '20 at 22:37
  • @Federico klez Culloca, if you have duplicate items, one option is to use AtomicInteger. I updated the answer to include that alternative. – Oboe Jun 26 '20 at 22:49
  • @Federico klez Culloca, if you are allow to use Guava, you can do`Streams.mapWithIndex`. See answer update. – Oboe Jun 26 '20 at 23:04