Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
523 views
in Technique[技术] by (71.8m points)

returning multiple values in Java

Preamble: I am aware of using a list or other collections to return a result but then I have to go through the list plucking the results out: see 2nd example

Preamble-2: I'm looking for an answer beyond "this is not supported in Java ..."


I'm looking for a convenient way to return multiple objects from a Java method call.

Kind of like in PHP:

list ($obj1, $obj2, ...) foobar();

I'm really getting tired of passing holder objects in the arguments for example:

class Holder {
   int value;
}

Holder h1=new Holder();
Holder h2=new Holder();

and then:

o.foobar(h1,h2);

... would be very interested if someone has figured an elegant way to get round this.


Using a list

List<String> = foobar();

There are two drawbacks to this:

I have to first pack the List on the callee side of the house:

// this is on the callee side of the house
ArrayList<String> result = new ArrayList<String>
result.add("foo");
result.add("bar");

Then on the caller side I have to pluck the results out:

// This is on the caller side
List<String> result = foobar();
String result1 = result.get(0);
String result2 = result.get(1); // this is not as elegant as the PHP equivalent

Further, say I wanted to return objects of different types say String, Integer I would have to return a list of Objects and then cast each object ... not pretty

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Using a generic tuple class is the closest I can imagine. To give an idea, such general purpose class for three return values could look something like this:

public class T3<A, B, C> {
    A _1;
    B _2;
    C _3;
    T3(A _1, B _2, C _3) {
        this._1 = _1;
        this._2 = _2;
        this._3 = _3;
    }
}

This allows a one statement constructor on the callee side, for example:

public T3<String, Integer, Character> getValues() {
    return new T3<String, Integer, Character>("string", 0, 'c');
}

but no holder instance construction by the callee is to be done.

Each number of return values needs a tuple-class of its own. There might be something like that provided in the Functional Java library. For reference, here's an N-Tuple implementation.

For the two-valued case java.util.AbstractMap.SimpleEntry would do:

return new SimpleEntry<String, Integer>("string", 0);

Anyhow, I see two important points to consider for multiple return values:

  1. Typed return values
  2. Pointing out the meaning of each return value

As boring as it is, I'd recommend creating a separate class to hold all the response values and declare that as the method return type. That way the meaning of each value can be highlighted.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...