0

So my problem is really simple. Having two lists, lets say: listA = {1,2,3,4} and listB = {5,6,7,8} I want to do some operations on pairs of items from different lists on coresponding positions so I want basically (1,5), (2,6), (3,7), (4,8). I know how to do it with external iterator but was wondering is there any nice solution of this problem using Stream Java 8 API. Second question is is there any way to skip creating Pair<T> class?

Tunaki
  • 132,869
  • 46
  • 340
  • 423
Julian Rubin
  • 1,175
  • 1
  • 11
  • 23
  • 1
    What you are trying to do is called "zipping" in functional programming. See the linked question. – Tunaki Jan 01 '16 at 13:58
  • Sorry for duplication. So is there any way to do something like `someList.stream().???????.foreach((x,y) -> ... )` ? Like changing the arity of stream, so in each element of stream i would have a pair which could be used as above? – Julian Rubin Jan 01 '16 at 15:13
  • Not with the Stream API itself. You either have to use an `IntStream` over the indices of the list or you can use StreamEx library which provdes a zip facility. – Tunaki Jan 01 '16 at 15:30

0 Answers0