amaembo / streamex

Enhancing Java Stream API
Apache License 2.0
2.18k stars 249 forks source link

Implement mapIndexed function #242

Closed gzsombor closed 3 years ago

gzsombor commented 3 years ago

It's not a very complex function, but I think, this could be a nice addition, I definitely missed from Kotlin's collections

coveralls commented 3 years ago

Coverage Status

Coverage increased (+0.03%) to 99.687% when pulling 021f46ffe617b5ab7ea9dc43b4149f030d939392 on gzsombor:map-indexed into 1ceeeb09acbae8c8ab72d9704b612a929398cd52 on amaembo:master.

amaembo commented 3 years ago

I'm not actually sure we need something like this. Indexes play not very nice with streams, as they should be counted through the whole stream (imagine you have upstream flatMap or filter), so this creates the global stream state, and it cannot be parallelized nicely. If you need indexes for List or array, you can use EntryStream.of(arrayOrList).mapKeyValue((index, element) -> ...), and this should be more parallel-friendly. Otherwisely, I would prefer having zip operation explicit.

gzsombor commented 3 years ago

Thanks, I haven't noticed this EntryStream.of(arrayOrList) trick, and it's nearly good for my use case. Unfortunately, I have a couple of filter/flatMap between my list and the indexing function. The original code was something like this:

int lineNo = 1;
for (MyObj obj: list) {
  var lines = myObj.getChildren().stream().filter(...).toList();
  for (Line line : lines) {
....
      line.setNumber(lineNo++);
  }
}

Not the best, and certainly not the most common problem, and in the end, my solution with the zipWith(IntStreamEx.ints()...) didn't look too nice as well. It hided the intent, that we wanted to count the lines, and put these line numbers in the objects itself - so I've created a static helper function, which made it a little bit better, but still ehh. So that's why I forked this repo, to put on the original class.