How to return multiple objects from a Java method?

前端 未结 25 3098
眼角桃花
眼角桃花 2020-11-21 23:55

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<

25条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 00:23

    In C++ (STL) there is a pair class for bundling two objects. In Java Generics a pair class isn't available, although there is some demand for it. You could easily implement it yourself though.

    I agree however with some other answers that if you need to return two or more objects from a method, it would be better to encapsulate them in a class.

提交回复
热议问题