A Java tuple class (Tuple2 or Pair, if you prefer)

After working with Scala for a long time, I had to come back to Java for a while to work on an Android app. Right away I missed a lot of things from the Scala world, including all of the built-in Scala collection methods, and other things as simple as the Scala Tuple classes.

If you haven’t used them before, a Scala Tuple class lets you write code like this:

Tuple<String, Integer> t = new Tuple<>("age", 41);

If you’re comfortable with generics, the Java implementation of a Tuple class like this is simple:

Scala: How to merge two sequential collections into pairs with ‘zip’

This is an excerpt from the Scala Cookbook, partially modified for the internet. This is Recipe 10.23, “How to Merge Two Sequential Collections into Pairs with the zip method”


You want to merge data from two sequential collections into a collection of key/value pairs.


Use the zip method that’s available to Scala sequential collections to join two sequences into one: