Steve Kuo Steve Kuo - 4 months ago 57x
Java Question

Google Guava "zip" two lists

Using Google Guava (Google Commons), is there a way to merge two equally sized lists into one list, with the new list containing composite objects of the two input lists?


public class Person {
public final String name;
public final int age;

public Person(String name, int age) { = name;
this.age = age;

public String toString() {
return "(" + name + ", " + age + ")";


List<String> names = Lists.newArrayList("Alice", "Bob", "Charles");
List<Integer> ages = Lists.newArrayList(42, 27, 31);

List<Person> persons =
transform with a function that converts (String, Integer) to Person

Would output:

[(Alice, 42), (Bob, 27), (Charles, 31)]


Looks like this is not currently in Guava, but is a desired feature. See this guava-libraries discussion this github issue, in particular