使用 Google Guava(Google Commons),有没有办法将两个相同大小的列表合并到一个列表中,新列表包含两个输入列表的复合对象?
例子:
public class Person {
public final String name;
public final int age;
public Person(String name, int age) {
this.name = 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
System.out.println(persons);
会输出:
[(Alice, 42), (Bob, 27), (Charles, 31)]