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

I have a set of objects that are the same type of object. Inside this type there is an attribute that I want to refer to - in this case, it should add it to the 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 access this attribute from all the objects in the collections so that I can call the Guava joiner function of Guava:

return Joiner.on(", ").join(/*Description attribute collection here*/);

The type of structure that I think of is similar to a 2D array, where each column represents an attribute, and each row represents an object, and therefore I want to be able to call either a specific row that returns an object or a specific column (attribute) that returns a collection of attributes from all objects.

Java , ?

,

.

+3
4

Google Guava ?

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

Iterable, . . @Hiery, ...

+2

JDK 8 -

List<Jedi> jedis = asList(new Jedi("Obiwan",80), new Jedi("Luke", 35));
List<String> jediNames = jedis.map(Jedi::getName).into(new ArrayList<>()); //with metdod ref
Iterable<Integer> jediAges = jedis.map(j -> j.getAge()); //with lambdas

, , JDK 8, .

, Lambdaj .

+2

, Item.toString(). , ()?

0

All Articles