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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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("GeeksforGeeks",
                                              "A computer science portal");
  
        // Using with() method to instantiate unit object
        Unit<String> unit = pair.removeFrom1();
  
        // Printing the returned Unit
        System.out.println(unit);
    }
}

chevron_right


Output:

[GeeksforGeeks]

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

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

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



My Personal Notes arrow_drop_up

Technical Content Engineer at GeeksForGeeks

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.