Saturday, August 30, 2025
HomeLanguagesJavaJavaTuples toString() method

JavaTuples toString() method

The toString() method in org.javatuples is used to convert the values in TupleClass into a String. This method is inherited from the JavaTuple class. This method can be used to any tuple class object of javatuples library. It returns a String value formed with the values in the TupleClassObject.

Method Declaration:

public final String toString()

Syntax:

String str = TupleClassObject.toString()

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

Return Value: This method returns a String value which contains the elements of the TupleClassObject, separated by ‘, ‘.

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

Program 1: Using toString() with Unit class:




// Below is a Java program to use toString() 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 toString() method
        String str = unit.toString();
  
        System.out.println(str);
    }
}


Output:

[Lazyroar]

Program 2: Using toString() with Quartet class:




// Below is a Java program to use toString() 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 toString() method
        String str = quartet.toString();
  
        // Printing the String
        System.out.println(str);
    }
}


Output:

[1, Lazyroar, A computer portal, 20.18]

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

RELATED ARTICLES

Most Popular

Dominic
32250 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6617 POSTS0 COMMENTS
Nicole Veronica
11792 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11840 POSTS0 COMMENTS
Shaida Kate Naidoo
6732 POSTS0 COMMENTS
Ted Musemwa
7014 POSTS0 COMMENTS
Thapelo Manthata
6689 POSTS0 COMMENTS
Umr Jansen
6704 POSTS0 COMMENTS