是否有Java数据结构在对象集合中返回一组特定属性?

Ale*_*lue 3 java collections

我有一组对象,它们都是相同的对象类型.在该类型中,有一个我想要访问的属性 - 在这种情况下,它是将它添加到以逗号分隔的列表中:

String sep = ", ";
List<Item> items = db.findItems();

for (Item item : items) 
{
    result.append(item.getDescription() + sep);
} //for

return result.substring(0, results.length - sep.length());
Run Code Online (Sandbox Code Playgroud)

如果我可以直接从集合中的所有对象访问该属性,那么我可以调用Google Guava的Joiner函数:

return Joiner.on(", ").join(/*Description attribute collection here*/);
Run Code Online (Sandbox Code Playgroud)

我想到的结构类似于2D数组,其中每列代表一个属性,每行代表一个对象,所以我希望能够调用返回对象的特定行或特定列(属性)返回所有对象的属性集合.

Java或一个好的第三方是否有这样的数据结构,还是我需要创建自己的数据结构?

干杯,

阿列克谢蓝.

Hie*_*mus 5

你为什么不最充分地使用谷歌的番石榴呢?

Joiner.on(",").join(Collections2.transform(items, new Function<Item, String>() {
    public String apply(Item input) {
        return item.getDescription();
    }
}));
Run Code Online (Sandbox Code Playgroud)