I want to add every element of an arraylist into a single object (Which has a generic base type T )
Im trying to do this but when i print out the final output which is the object, i only have the last element of the arraylist printed.
So its itterating through every element but stores and prints the last.
Any suggestions?
Assuming that your T is not an aggregate type, it is not possible to put many objects into one.
If your T is a collection and you want to collapse the collections, you can iterate each of the source collections and add each value to the target collection.
However, what it sounds to me like you are really after is nice printable view of the object. If this is for debugging, I recommend finding one of the many JSON support libraries, and printing the JSON conversion of the object, which wlll be readable. I rolled my own, since I had to support Domino API types, so I cannot recommend one.
I have been doing far, far too much C# lately, and cannot think in fluent Java right now, but something close to this (in spirit at least, if not syntactical or API detail) is what you are after: