Returning Multiple values in Java

In this instructional exercise, we’ll gain various ways of returning different qualities from a Java strategy. To begin with, we’ll restore clusters and assortments. Then, at that point, we’ll tell the best way to utilize holder classes for complex information and figure out how to make conventional tuple classes. At last, we’ll see instances of how to utilize outsider libraries to return numerous qualities.

Returning Multiple values in Java

We can return more than one qualities that are of similar information type utilizing an exhibit. As cluster stores numerous qualities so we can return an exhibit from a strategy as in our model. Beneath, we make a strategy method1() that has a return sort of String[] exhibit of Strings. In method1(), we make three nearby factors and relegate them with values, presently we make a variety of type String called array1.

Presently we set the files of array1 with the factors. Then, at that point, we return the exhibit utilizing return array1. In the primary() strategy, we call method1() and convert it to a String utilizing Arrays.toString() and we can see the variety of the relative multitude of qualities in the result.

If all returned elements are of the same type

// A Java program to demonstrate that a method
// can return multiple values of same type by
// returning an array
class Test {
    // Returns an array such that first element
    // of array is a+b, and second element is a-b
    static int[] getSumAndSub(int a, int b)
    {
        int[] ans = new int[2];
        ans[0] = a + b;
        ans[1] = a - b;
 
        // returning array of elements
        return ans;
    }
 
    // Driver method
    public static void main(String[] args)
    {
        int[] ans = getSumAndSub(100, 50);
        System.out.println("Sum = " + ans[0]);
        System.out.println("Sub = " + ans[1]);
    }
}
Output:

Sum = 150
Sub = 50

Using Pair (If there are only two returned values)

// Returning a pair of values from a function
import javafx.util.Pair;
 
class GfG {
    public static Pair<Integer, String> getTwo()
    {
        return new Pair<Integer, String>(10, "GeeksforGeeks");
    }
 
    // Return multiple values from a method in Java 8
    public static void main(String[] args)
    {
        Pair<Integer, String> p = getTwo();
        System.out.println(p.getKey() + " " + p.getValue());
    }
}

If there are more than two returned values

Let us have a look at the following code.

// A Java program to demonstrate that we can return
// multiple values of different types by making a class
// and returning an object of class.
 
// A class that is used to store and return
// three members of different types
class MultiDivAdd {
    int mul; // To store multiplication
    double div; // To store division
    int add; // To store addition
    MultiDivAdd(int m, double d, int a)
    {
        mul = m;
        div = d;
        add = a;
    }
}
 
class Test {
    static MultiDivAdd getMultDivAdd(int a, int b)
    {
        // Returning multiple values of different
        // types by returning an object
        return new MultiDivAdd(a * b, (double)a / b, (a + b));
    }
 
    // Driver code
    public static void main(String[] args)
    {
        MultiDivAdd ans = getMultDivAdd(10, 20);
        System.out.println("Multiplication = " + ans.mul);
        System.out.println("Division = " + ans.div);
        System.out.println("Addition = " + ans.add);
    }
}
Output:

Multiplication = 200
Division = 0.5
Addition = 30

Returning list of Object Class

// Java program to demonstrate return of
// multiple values from a function using
// list Object class.
import java.util.*;
 
class GfG {
    public static List<Object> getDetails()
    {
        String name = "Geek";
        int age = 35;
        char gender = 'M';
 
        return Arrays.asList(name, age, gender);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        List<Object> person = getDetails();
        System.out.println(person);
    }
}
Output:

[35, M]

Leave a Reply

Your email address will not be published. Required fields are marked *