Google Guava“zip”两个清单 [英] Google Guava "zip" two lists

查看:150
本文介绍了Google Guava“zip”两个清单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Google Guava(Google Commons),有没有办法将两个大小相同的列表合并到一个列表中,新列表包含两个输入列表的复合对象?

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) {
        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)]


推荐答案

看起来这个目前不在Guava中,但是是一个理想的功能。请参阅此github问题,特别是 Iterators.zip()

Looks like this is not currently in Guava, but is a desired feature. See this github issue, in particular Iterators.zip().

这篇关于Google Guava“zip”两个清单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆