Core Java

Object and Index Streams

I was going to write a post about how to blend a stream with the indices of each element, but the folks over on Baeldung have covered this subject enormously well! Given I’m part of their editorial team, I’m proud of them/us.

It’s interesting how functional programming in Java, Streams especially, have trained us out of some common patterns. Example:

List<Foo> foos = new ArrayList<>();
for (int i=0; i < bars.size(); i++) {
    foos.add(new Foo(i, bars.get(i));

The above code creates a new Foo from the index and the bar from a source list.

We don’t like using add in the midst of a for loop, when Stream.collect is a bigger signal that we’re pulling data from an iteration. Similarly, we don’t like hiding the effective inside the loop, when we could express it more explicitly in a streaming declaration. We all know that the above ought to read:

List<Foo> foos =
   .map( ... something ...)

The awkward challenge is that the something can’t easily be done.

How to choose what to do

On the whole, cooking a complex brew of functions around a streaming operation is a way of hiding the very thing that the stream was meant to show us – namely how the data from one place goes to another.

So here are some thoughts:

  • Maybe just have a for loop – it might be easier
  • If you find that the main player within the body of the operation is the index, then make a stream of indices, and mix in the object with a List.get at the last minute, assuming your list is efficiently indexed
  • If the main player is an object, then maybe store a counter (AtomicInteger is a good bet) and increment it at the last minute, watching out for ordering and parallel streams
  • If the code gets complex, try to divorce the streaming logic from your business logic by extracting a function
  • Maybe delegate this to a library – the Baeldung article shows ProtonPack and others that might just be better and which come ready tested and documented.

Whatever you do, don’t make things difficult for yourself on a point of principle.

Published on Java Code Geeks with permission by Ashley Frieze, partner at our JCG program. See the original article here: Object and Index Streams

Opinions expressed by Java Code Geeks contributors are their own.

Want to know how to develop your skillset to become a Java Rockstar?

Join our newsletter to start rocking!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Ashley Frieze

Software developer, stand-up comedian, musician, writer, jolly big cheer-monkey, skeptical thinker, Doctor Who fan, lover of fine sounds
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
4 years ago

spam article?

Back to top button