-1

I am traversing over my allRecords list that is of type Abc using an iterator. To keep track of the elements iterated, i keep a position variable.

I use this position variable to further apply some logic in my program.

Code snippet is as follows:

Abc abc;
Iterator<Abc> iterator = allRecords.iterator();
while (iterator.hasNext()) {
  abc = iterator.next();
  position++;
  //validate the abc object with some conditions and break the loop if matches
  //I will have that position where the condition matched
}

My question is can I use some of Java's latest stream API or lambda expression to fulfill this task.

Any help would be appreciated.

Salman Kazmi
  • 3,038
  • 4
  • 21
  • 32
  • the point of streams would be avoiding things like indexes, what are you trying to achieve here? – Rogue Aug 19 '17 at 11:21

1 Answers1

1

You can achieve this by something like this

IntStream.range(0, allRecords.length)
     .filter(i -> allRecords[i].length() <= i)
     .mapToObj(i -> allRecords[i])

However Stream API design to make internal iteration possible and you shouldn't concern with index when you use this api.

refer to this post for more info on External and internal iteration.

Morteza Adi
  • 2,413
  • 2
  • 22
  • 37