开发者

how to return two values(1.Collection, 2.Single Boolean value) from a method in java with less expense?

开发者 https://www.devze.com 2023-03-09 15:30 出处:网络
I have one Main class and VOCollection Class. in main class there is a method called getStatus(), from this method only i am getting some status(true,false), if the status is true, i need to return a

I have one Main class and VOCollection Class. in main class there is a method called getStatus(), from this method only i am getting some status(true,false), if the status is true, i need to return a collection. at present i have two ideas, but both are expensive.

  1. return map, it's expensive because setting Boolean for collection make confusion in开发者_StackOverflow code, and only one Boolean value is enough (but we are returning multiple).

  2. creating an instance variable in VOCollection class, and having getter & setter to get & set the Boolean value. this is also expensive. (creating a variable in another class).

give me less expensive solution.


There are a number of ways to do this:

  • Return null to indicate that there is nothing present like java.util.Map.get()
  • Create a custom class to return both parameters. (See other answer)
  • Use a 1 element array for one of the return values. boolean method(List[] result) { result[0] = answer; return flag; }
  • Use a library like Google Guava that has a Pair/Tuple class: Pair method() { return new Pair(flag, answer); }
  • Change your code so that it isn't necesssary, this is usually the right answer. See the exception comment or change the way things are passed around.


The desire to return either an object or a boolean is smelly. You see this kind of "flag" very often in code of starters who don't fully understand how to handle exceptional or "nothing" outcomes.

If the boolean represents an exception, just let it go or rethrow it in another exception. E.g. IllegalArgumentException, IllegalStateException, UnsupportedOperationException, etc depending on the functional requirement. You can put the calling code in a try-catch and handle it accordingly.

Or if the boolean represents a state of "nothing", just return null or an empty collection. You can handle it by testing the return value after calling the method.


You can introduce an class like this

class StatusResult
{
    public final bool status;
    public final VOCollection result;

    ...
}

and change the signature of getStatus() to

StatusResult getStatus()


I have come across the same situation in my past.

Both the solutions which you have mentioned are feasible as you said they may be expensive.

In fact, Java doesnot support the multiple return values from a method.

Solution Which I implemented was

I will try to parse/create/encode the string in such a way, after decoding i should be able to know what i need to do.

In your case, I would have choosen the following path Create a string like returnValue, let that string start with either 0 or 1, if its 1 its true, else false. and append the second return value to that.

For example you are trying to return like "returnValue1" and "true" for true let us keep it 1 and for false lets keep it 2 when you do this, your return value will become "1returnValue1"


Simply return the collection when 'status is true'. Otherwise return null.

But: Getting a collection with calling a method named "getStatus()" is not what I would expect.

"getStatus()" sounds getting some constant value indicating internal state. (like enum constant, int status code whatever)

In your scenario I would recommend having one method returning a boolean indicating that "i have something or not" and another method returning the collection - that what you have or null when not. The boolean-method can then be easy implemented by calling the collection-method checking != null.

public boolean isXyz() {
    return getXyz() != null;
}

public Collection getXyz() {
    return yourCollection; //maybe null
}


The simplest approach may be to always return a VOCollection. For the situation where there is no data you could return a collection with no data i.e. isEmpty() is true, or a collection which has a flag (but I prefer the first option).

To avoid creating an object each time, create a single instance of an immutable VOCollection with no entries. This is what you can return when you have no data.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号