1

I'd like to know what is the best way to go about counting stream iterations (if possible). This is the stream I am using:

list.stream().map(o->o.toString()).forEach(System.out::println);

This is my toString method:

@Override
public String toString() {
    String string = "something"

    return string;
}

What I'm trying to do is also print out a number of iterations, so the list would print out as:

1. List:
something
2. List:
something

I'd definitely have to use stream, toString and forEach, as this is an assignment. I'm just learning how streams work.

Stefan Zobel
  • 3,182
  • 7
  • 28
  • 38
Ivan C
  • 1,591
  • 2
  • 8
  • 9
  • 3
    Use an `IntStream` going from 0 to the size of your list, this is the way to iterate with indexes with the Stream API. – Tunaki Nov 13 '16 at 22:03

0 Answers0