In Java, methods can only return one value. However, there are several ways to work around this limitation and effectively return multiple values from a method. In this tutorial, we will explore different approaches to achieve this.
Using a Custom Class
One way to return multiple values is by creating a custom class that encapsulates the values you want to return. This approach provides type safety and makes your code more readable.
Here’s an example:
public class MyResult {
private final int first;
private final int second;
public MyResult(int first, int second) {
this.first = first;
this.second = second;
}
public int getFirst() {
return first;
}
public int getSecond() {
return second;
}
}
public class Main {
public static MyResult something() {
int number1 = 1;
int number2 = 2;
return new MyResult(number1, number2);
}
public static void main(String[] args) {
MyResult result = something();
System.out.println(result.getFirst() + result.getSecond());
}
}
In this example, we create a MyResult
class with two fields: first
and second
. The something()
method returns an instance of MyResult
, which contains the two values. In the main()
method, we can access these values using the getter methods.
Using a Generic Pair Class
Another approach is to use a generic pair class that can hold two values of any type. This provides more flexibility than creating a custom class for each specific use case.
public class Pair<U, V> {
private final U first;
private final V second;
public Pair(U first, V second) {
this.first = first;
this.second = second;
}
public U getFirst() {
return first;
}
public V getSecond() {
return second;
}
}
public class Main {
public static Pair<Integer, Integer> something() {
int number1 = 1;
int number2 = 2;
return new Pair<>(number1, number2);
}
public static void main(String[] args) {
Pair<Integer, Integer> pair = something();
System.out.println(pair.getFirst() + pair.getSecond());
}
}
In this example, we create a Pair
class with two type parameters: U
and V
. The something()
method returns an instance of Pair
, which contains the two values. In the main()
method, we can access these values using the getter methods.
Using Java Built-in Classes
Java provides several built-in classes that can be used to return multiple values, such as AbstractMap.SimpleEntry
. This class is a simple implementation of the Map.Entry
interface and can hold two values: a key and a value.
import java.util.AbstractMap;
public class Main {
public static AbstractMap.SimpleEntry<String, Float> something() {
String key = "maximum possible performance reached";
Float value = 99.9f;
return new AbstractMap.SimpleEntry<>(key, value);
}
public static void main(String[] args) {
AbstractMap.SimpleEntry<String, Float> entry = something();
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
In this example, we create a SimpleEntry
instance with a key and a value. The something()
method returns this instance, which contains the two values. In the main()
method, we can access these values using the getKey()
and getValue()
methods.
Using Third-Party Libraries
There are also several third-party libraries available that provide classes for returning multiple values, such as Apache Commons Lang’s Pair
class.
import org.apache.commons.lang3.tuple.Pair;
public class Main {
public static Pair<Integer, Integer> something() {
int number1 = 1;
int number2 = 2;
return Pair.of(number1, number2);
}
public static void main(String[] args) {
Pair<Integer, Integer> pair = something();
System.out.println(pair.getLeft() + pair.getRight());
}
}
In this example, we use the Pair
class from Apache Commons Lang to create a pair of values. The something()
method returns an instance of Pair
, which contains the two values. In the main()
method, we can access these values using the getLeft()
and getRight()
methods.
In conclusion, there are several ways to return multiple values from a Java method, including using custom classes, generic pair classes, built-in classes, and third-party libraries. The choice of approach depends on the specific use case and personal preference.