Return multiple values from a method in Java
This post will discuss how to return multiple values from a method in Java.
As per the 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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
// POGO for grouping multiple fields final class Person { public String name; public int age; public char gender; public Person(String name, int age, char gender) { this.name = name; this.age = age; this.gender = gender; } } class Main { public static Person getDetails() { // return person details from the method String name = "Ryan"; int age = 25; char gender = 'M'; return new Person(name, age, gender); } // Return multiple values from a method in Java public static void main(String[] args) { Person person = getDetails(); System.out.println("Name is " + person.name); System.out.println("Age is " + person.age); System.out.println("Gender is " + person.gender); } } |
Output:
Name is Ryan
Age is 25
Gender is M
The benefits of using this method over other methods discussed below are that it provides the type-safety and considerably improves the code’s readability.
Following is a simpler version of the 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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
import java.util.Arrays; // Class for grouping multiple fields class Collection { private Object[] items; private final int size; // Constructs an object array and initialize it with given values public Collection(Object... values) { this.size = values.length; this.items = Arrays.copyOf(values, size); } public Object get(int i) { return items[i]; } public int getSize() { return size; } @Override public String toString() { return Arrays.toString(items); } } class Main { public static Collection getDetails() { String name = "Ryan"; int age = 25; char gender = 'M'; long income = 100000; return new Collection(name, age, gender, income); } // Return multiple values from a method in Java public static void main(String[] args) { Collection details = getDetails(); System.out.println("The returned values are " + details); System.out.println("The collection size is " + details.getSize()); System.out.println("The name field has value " + details.get(0)); } } |
Output:
The returned values are [Ryan, 25, M, 100000]
The collection size is 4
The 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. Using javafx.util.Pair class
From Java 8 onward, we can use the Pair class included in the javafx.util package representing the name-value pairs.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import javafx.util.Pair; class Main { public static Pair[] getDetails() { return new Pair[] { new Pair<>("name", "Ryan"), new Pair<>("age", 25), new Pair<>("gender", 'M') }; } // Return multiple values from a method in Java 8 and above public static void main(String[] args) { Pair[] person = getDetails(); for (int i = 0; i < person.length; i++) { System.out.println("{" + person[i].getKey() + " : " + person[i].getValue() + "}"); } } } |
Output:
{name : Ryan}
{age : 25}
{gender : M}
3. Return an array of specific type or an object array
This is the simplest approach so far but doesn’t offer type-safety (for an object array) or pass field information to the caller. For arguments of the same type, we can do something like:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.Arrays; class Main { public static int[] getDetails() { int v1 = 30; int v2 = 50; int v3 = 100; return new int[] { v1, v2, v3 }; } // Return multiple values from a method in Java public static void main(String[] args) { int[] ints = getDetails(); System.out.println(Arrays.toString(ints)); } } |
Output:
[30, 50, 100]
For multi-type arguments, we can return an array of Objects, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.Arrays; class Main { public static Object[] getDetails() { String name = "Ryan"; int age = 25; char gender = 'M'; long income = 100000; return new Object[] { name, age, gender, income }; } // Return multiple values from a method in Java public static void main(String[] args) { Object[] person = getDetails(); System.out.println(Arrays.toString(person)); } } |
Output:
[Ryan, 25, M, 100000]
4. Return a Collection
Instead of returning an array, we can also return a Collection. For example, the following program uses a List instead of an array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.Arrays; import java.util.List; class Main { public static List<Object> getDetails() { String name = "Ryan"; int age = 25; char gender = 'M'; long income = 100000; return Arrays.asList(name, age, gender, income); } // Return multiple values from a method in Java public static void main(String[] args) { List<Object> person = getDetails(); System.out.println(person); } } |
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:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import java.util.AbstractMap; import java.util.Map; import java.util.stream.Collectors; import java.util.stream.Stream; class Main { public static Map<String, String> getDetails() { return Stream.of( new AbstractMap.SimpleEntry<>("name", "Ryan"), new AbstractMap.SimpleEntry<>("age", "25"), new AbstractMap.SimpleEntry<>("gender", "Male"), new AbstractMap.SimpleEntry<>("income", "100000$") ) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); } // Return multiple values from a method in Java public static void main(String[] args) { Map<String, String> person = getDetails(); for (String key: person.keySet()) { System.out.println(key + " is " + person.get(key)); } } } |
Output:
income is 100000$
gender is Male
name is Ryan
age is 25
5. Using a delimiter
Here, the idea 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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
class Main { public static String getDetails() { return "George" + "~" + "Martin"; } // Return multiple values from a method in Java public static void main(String[] args) { String[] name = getDetails().split("~"); for (String s: name) { System.out.println(s); } } } |
Output:
George
Martin
That’s all about returning multiple values from a method in Java.
Also see:
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)