I've got two List
objects and I want to pair them up, just like the zip()
function in Python. I'm pretty sure this isn't available in the JDK, but is there something like this in a fairly widespread library, similar to Apache Commons Collections? Thanks.
Asked
Active
Viewed 1.1k times
48

Hank Gay
- 70,339
- 36
- 160
- 222
-
Not well versed in Python and how zip is used but a Quick glance makes me thing that ListUtils from the Collections library should do the trick. combinedList = ListUtils.union(list1, list2) ; Iterate over the combined list. https://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/ListUtils.html#union(java.util.List,%20java.util.List) – trappski Nov 02 '16 at 10:03
-
@trappski, that's just a wrapper for List.addAll which does not solve the problem. – micseydel Jun 28 '17 at 16:18
1 Answers
24
Functional Java has zip
, zipWith
and zipIndex
the way you would expect from Haskell or Scala. (Indeed, the authors are pretty much all Haskell programmers.)

Community
- 1
- 1

Jörg W Mittag
- 363,080
- 75
- 446
- 653
-
2Actually, the version I linked to is the first-class version. (I couldn't figure out how to link to the other version without Markdown gobbling up the link.) IOW: it's not the `zipWith` *function* it's a function that *returns* the `zipWith` function. The signature for the *real* `zipWith` is `public List
zipWith(List bs, F2 f)`, which is basically the same as the Haskell one: `(a → b → c) → [a] → [b] → [c]`. It takes a list of as (the implicit `this`), a list of bs and a function from a and b to c and returns a list of cs. – Jörg W Mittag Jan 24 '10 at 11:19