How to return multiple objects from a Java method?

前端 未结 25 3119
眼角桃花
眼角桃花 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:42

    Apache Commons has tuple and triple for this:

    • ImmutablePair An immutable pair consisting of two Object elements.
    • ImmutableTriple An immutable triple consisting of three Object elements.
    • MutablePair A mutable pair consisting of two Object elements.
    • MutableTriple A mutable triple consisting of three Object elements.
    • Pair A pair consisting of two elements.
    • Triple A triple consisting of three elements.

    Source: https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/tuple/package-summary.html

提交回复
热议问题