Is there a Java data structure that returns a set of a specific attribute in a collection of objects?

collections, java

Solution

Why don't you use Google's Guava to the fullest then?

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

Problem

I have a collection of objects which are all the same object type. Within that type there is an attribute that I want to access - in this case it's to add it to a comma separated list: ``` 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()); ``` It would be nice if I could just directly access that attribute from all the objects in the collections so that I could call Google Guava's Joiner function: ``` return Joiner.on(", ").join(/*Description attribute collection here*/); ``` The kind of structure I'm thinking of is like a 2D array where each column represents an attribute and each row represents an object and so I want to be able to call either a specific row which returns the object or a specific column (attribute) that returns a collection of the attributes from all objects. Does Java or a good third party have a data structure like this or would I need to create my own? Cheers, Alexei Blue.

Original source