Thursday, September 4, 2025
HomeLanguagesJavaJavaTuple toArray() method

JavaTuple toArray() method

The toArray() method in org.javatuples is used to convert the values in TupleClass into an array of Object type. This method is inherited from the JavaTuple class. This method can be used to any tuple class object of javatuples library. It returns an array of Object type formed with the values in the TupleClassObject.

Method Declaration:

public final Object[] toArray()

Syntax:

Object obj[] = TupleClassObject.toArray()

Here TupleClassObject represents the JavaTuple Class object used like Unit, Quintet, Decade, etc.

Return Value: This method returns an array of Object type formed with the values in the TupleClassObject.

Below are programs that will illustrate the various ways to use toArray() method:

Program 1: Using toArray() with Unit class:




// Below is a Java program to use toArray() method
  
import java.util.*;
import org.javatuples.Unit;
  
class GfG {
    public static void main(String[] args)
    {
        // Creating an Unit with one value
        Unit<String> unit = Unit.with("Lazyroar");
  
        // Using toArray() method
        Object[] object = unit.toArray();
  
        // Printing the array elements
        for (Object obj : object) {
            System.out.println(obj);
        }
    }
}


Output:

Lazyroar

Program 2: Using toArray() with Quartet class:




// Below is a Java program to use toArray() method
  
import java.util.*;
import org.javatuples.Quartet;
  
class GfG {
    public static void main(String[] args)
    {
        // Creating a quartet
        Quartet<Integer, String, String, Double> quartet
            = Quartet.with(Integer.valueOf(1),
                           "Lazyroar",
                           "A computer portal",
                           Double.valueOf(20.18));
  
        // Using toArray() method
        Object[] object = unit.toArray();
  
        // Printing the array elements
        for (Object obj : object) {
            System.out.println(obj);
        }
    }
}


Output:

1
Lazyroar
A computer portal
20.18

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

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32260 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6625 POSTS0 COMMENTS
Nicole Veronica
11795 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6746 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6694 POSTS0 COMMENTS
Umr Jansen
6714 POSTS0 COMMENTS