5 ways to Return multiple values from a method in Java

In this post, we will see how to return multiple values from a method in Java.

 

As per Java Language Specification, the methods in Java can return only one value at a time. So returning multiple values from a method is theoretically not possible in Java. But the beauty of Java lies in the fact that we can do desired things with some smart workarounds. This post provides an overview of some of the available alternatives to accomplish this.

 

1. Using a POJO class instance

This is the most commonly used method to return multiple values from a method in Java. The idea is to return an instance of a class containing all fields we want to return.

This class can be a POJO with public member variables and a public constructor to initiate its fields with required values or a JavaBean with corresponding getters and setters for their private member variables and no-arg public constructor.

 

Download   Run Code

Output:

Name is Ryan
Age is 25
Gender is M

 
The benefits of using this method over other methods discussed below is that it provides the type safety and considerably improves readability of the code.

Below is simpler version of above program that uses an Object array to store all the required fields but it doesn’t offer any type safety or even pass field information to the caller.

 

Download   Run Code

Output:

Returned Values are [Ryan, 25, M, 100000]
Collection size is : 4
Name field has value Ryan

 
We can also implement a generic Pair or generic Tuple which also offers type safety if we just need to return two or three fields from the method.

 

2. javafx.util.Pair class in Java 8

From Java 8 onward, we can make use of the Pair class included in javafx.util package that represents the name-value pairs.

 

Download

Output:

{name : Ryan}
{age : 25}
{gender : M}

 

3. Return an Array of Specific Type or an Object Array

This is simplest approach so far but doesn’t doesn’t offer type safety (for an Object array) or pass field information to the caller. For arguments of same type, we can do something like:

 

Download   Run Code

Output:

[30, 50, 100]

 
For multi-type arguments, we can return an array of Objects as shown below:

Download   Run Code

Output:

[Ryan, 25, M, 100000]

 

4. Return a Collection

Instead of returning an array, we can also return a Collection. For example, below program uses a List instead of an array.

Download   Run Code

Output:

[Ryan, 25, M, 100000]

 
Using a List doesn’t pass field information to the caller. We can create a Map instead to pass the field name and its value as key-value pairs as shown below:

 

Download   Run Code

Output:

income is 100000$
gender is Male
name is Ryan
age is 25

 

5. Using a delimiter

The idea here is to return a String consisting of all values separated by a delimiter. We then need to split the String using the same delimiter before using the values inside the caller method.

This approach is not so neat but still vastly used in many legacy projects to transfer data between model and view.

 

Download   Run Code

Output:

George
Martin

 
Also see:

1. Return multiple values from a method in C

2. How to return multiple values from a function in C++

 
Thanks for reading.




Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 





Leave a Reply

Notify of
avatar
wpDiscuz