What are (1) the common practice and (2) possible solutions for returning a pair of objects in one method in java?
Example: Let's say I have a CheckersBoard
class in Model that has a method f
that takes BoardFieldIndex start, BoardFieldIndex end
describing a movement asked by a View and returns a pair of Figure f
(if start
is occupied) and MovementVector v
(if it is legal on the board).
In C++ I have two popular options:
(I.)
Figure *f; MovementVector *v;
board.f( start, end, f, v );
(II.)
Figure *f; MovementVector *v;
std::Pair<Figure*, MovementVector*> tuple = board.f( start, end );
f = tuple.first; v = tuple.second;
In Java the only solutions I can think of are:
- Using a List as a substitute for Tuple. But the problem is I do not define types there and using a raw List I think is discouraged.
- Implement custom generic class Pair.
I would like to know what is the best practice or how can I do it in a way that feels natural for Java. I personally like (I.) best, but it's impossible here. The problem I have with other solutions is that I access the elements of a tuple in a way that doesn't suggest what they contain and I think it goes a bit against literate programming paradigm.