Wednesday, November 20, 2024
Google search engine
HomeLanguagesJavaJavaTuples removeFromX() method

JavaTuples removeFromX() method

The removeFromX() method in org.javatuples is used to remove a value from the existing tuple, from the index X. Since JavaTuples are immutable, hence removing a value from the existing tuple results in a new tuple with one value less. For example, removing a value from Pair tuple results in the formation of a Unit tuple. This method can be used for any tuple class object of the javatuples library, except the Unit class,, as Unit is the smallest class available in JavaTuples library with only 1 value. It returns the tuple class object of a class smaller than the called class.

Syntax:

Quartet<String, Integer, Double, String> quartet = ...
    ...
Triplet<String, Integer, Double> triplet = quartet.removeFromX();

Here X represents the index from which the value is to be removed.

Return Value: This method returns the tuple class object of a class smaller than the called class. The removed value is from the index X mentioned in the method name.

Note: This method do not exists with Unit Class, KeyValue Class, and LabelValue Class.

Below programs illustrate the various ways to use removeFromX() methods:

Program 1: When the removeFromX() method is used with any class from Pair to Decade, with a direct values as parameter:




// Below is a Java program to demonstrate
// use of removeFromX() method
  
import java.util.*;
import org.javatuples.Unit;
import org.javatuples.Pair;
  
class GfG {
    public static void main(String[] args)
    {
        // Creating a Pair with 2 values
        Pair<String, String> pair = Pair.with("Lazyroar",
                                              "A computer science portal");
  
        // Using with() method to instantiate unit object
        Unit<String> unit = pair.removeFrom1();
  
        // Printing the returned Unit
        System.out.println(unit);
    }
}


Output:

[Lazyroar]

Program 2:




// Below is a Java program to demonstrate
// use of removeFromX() method
  
import java.util.*;
import org.javatuples.Ennead;
import org.javatuples.Decade;
  
class GfG {
    public static void main(String[] args)
    {
        // Using with() method to instantiate Decade object
        Decade<Integer, Integer, Integer,
               Integer, Integer, Integer,
               Integer, Integer, Integer,
               Integer>
            decade
            = Decade.with(Integer.valueOf(1),
                          Integer.valueOf(2),
                          Integer.valueOf(3),
                          Integer.valueOf(4),
                          Integer.valueOf(5),
                          Integer.valueOf(6),
                          Integer.valueOf(7),
                          Integer.valueOf(8),
                          Integer.valueOf(9),
                          Integer.valueOf(10));
  
        // Using removeFromX() to create Ennead
        Ennead<Integer, Integer, Integer,
               Integer, Integer, Integer,
               Integer, Integer, Integer>
            ennead
            = decade.removeFrom0();
  
        // Printing the formed Ennead
        System.out.println(ennead);
    }
}


Output:

[2, 3, 4, 5, 6, 7, 8, 9, 10]

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

RELATED ARTICLES

Most Popular

Recent Comments