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.
- 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. commons.apache.org/proper/commons-collections/apidocs/org/…trappski– trappski2016-11-02 10:03:24 +00:00Commented Nov 2, 2016 at 10:03
- @trappski, that's just a wrapper for List.addAll which does not solve the problem.micseydel– micseydel2017-06-28 16:18:14 +00:00Commented Jun 28, 2017 at 16:18
Add a comment |
1 Answer
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.)
1 Comment
Jörg W Mittag
Actually, 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 <B,C> List<C> zipWith(List<B> bs, F2<A,B,C> 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.