Open In App

JavaTuples fromArray() method

Improve
Improve
Like Article
Like
Save
Share
Report

The fromArray() method in org.javatuples is used to instantiate a tuple in a semantically elegant way, with the values of the array, given as parameters. This method can be used to any tuple class object of javatuples library. It is a static function in each javatuple class and it returns the tuple class object of the called class, with the values initialized by the corresponding values of the array.

Method Declaration:

public static <X> TupleClass<X> fromArray(X[] array)

Syntax:

TupleClass<X> obj = TupleClass.fromArray(X[] array)

Parameters: This method takes array as parameter where:

  • X– represents the datatype of values in the array.
  • array– represents the array of values to be inserted into TupleClass.
  • TupleClass– represents the JavaTuple Class used like Unit, Quintet, Decade, etc.

Return Value: This method returns the object of TupleClass, which calls the method, with the values of the array, passed as the parameters.

Below programs illustrate the various ways to use fromArray() method:

Program 1: Using fromArray() with Unit class:




// Below is a Java program to create
// a Unit tuple from fromArray() method
  
import java.util.*;
import org.javatuples.Unit;
  
class GfG {
    public static void main(String[] args)
    {
        // Creating an array with one value
        String str[] = { "GeeksforGeeks" };
  
        // Using fromArray() method
        Unit<String> unit = Unit.fromArray(str);
  
        System.out.println(unit);
    }
}


Output:

[GeeksforGeeks]

Program 2: Using fromArray() with Decade class:




// Below is a Java program to create
// a Unit tuple from fromArray() method
  
import java.util.*;
import org.javatuples.Decade;
  
class GfG {
    public static void main(String[] args)
    {
        // Creating an array with 10 value
        String str[] = { "Geeks",
                         "for",
                         "Geeks",
                         "A",
                         "Computer",
                         "Science",
                         "Portal",
                         "for",
                         "Geeks",
                         "RishabhPrabhu" };
  
        // Using fromArray() method
        Decade<String, String, String, String, String,
               String, String, String, String, String>
            decade = Decade.fromArray(str);
  
        System.out.println(decade);
    }
}


Output:

[Geeks, for, Geeks, A, Computer, Science, Portal, for, Geeks, RishabhPrabhu]

Note: Similarly, it can be used with any other JavaTuple Class.



Last Updated : 30 Aug, 2018
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads