How to return multiple objects from a Java method?

I want to return two objects from a Java method and was wondering what could be a good way of doing so?

The possible ways I can think of are: return a HashMap (since the two Objects are related) or return an ArrayList of Object objects.

To be more precise, the two objects I want to return are (a) List of objects and (b) comma separated names of the same.

I want to return these two Objects from one method because I dont want to iterate through the list of objects to get the comma separated names (which I can do in the same loop in this method).

Somehow, returning a HashMap does not look a very elegant way of doing so.

25 Answers
25

Leave a Comment