【发布时间】:2014-10-11 22:30:21
【问题描述】:
我有一个对象集合如下:
Collection<Foo>
Foo 在哪里
public class Foo {
private User user;
private Item item;
public Foo(User user, Item item) {
this.user = user;
this.item = item;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Item getItem() {
return item;
}
public void setItem(Item item) {
this.item = item;
}
}
我想使用Collection<Foo> 返回另一个Collection<Item> 类型的集合。我可以通过使用for loop 并遍历Collection、获取项目并将其添加到新列表来做到这一点。到目前为止,我已经使用 Google Guava 使用谓词创建了我的 Collection<Foo>。
Google guava 中是否有一种方法/功能可以让我从Collection<Foo> 创建一个Collection<Item>?我应该使用转换功能吗?
【问题讨论】: