49

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.

2
  • 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/… Commented Nov 2, 2016 at 10:03
  • @trappski, that's just a wrapper for List.addAll which does not solve the problem. Commented Jun 28, 2017 at 16:18

1 Answer 1

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.)

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.