mercredi 5 février 2020

Map an Array of Objects to a single Object

I would like to map an array of objects to a single object, where I'd be able to access the fields with the corresponding names.

Example:

class Item{
int value;
Date date;
String type;
}

An Instance could look like this:

Item itemOne = Item(value=3, type="BANANA", date=Date.now())

And imagine we would have an array/list of these Item objects:

Item itemTwo = Item(value=4, type="APPLE", date=Date.now())
list.add(itemOne)
list.add(itemTwo)

Is there a library or method with what I could have a code generation that would generate a class such that I'd be able to access the data of the Item objects in the following way:

ItemGroup itemGroup = getItemGroup()
itemGroup.getBananaDate()
itemGroup.getAppleValue()

Aucun commentaire:

Enregistrer un commentaire