Sunday, October 5, 2025
HomeLanguagesJavaImplement Triplet Class with Pair Class in Java using JavaTuples

Implement Triplet Class with Pair Class in Java using JavaTuples

Following are the ways to implement Triplet Class with Pair Class

  1. Using direct values




    import java.util.*;
    import org.javatuples.*;
      
    class GfG {
      
        public static void main(String[] args)
        {
            // create Pair
            Pair<Integer, String>
                pair = new Pair<Integer, String>(
                    Integer.valueOf(1), "Lazyroar");
      
            // Print the Pair
            System.out.println("Pair: " + pair);
      
            // Create Triplet from Pair
            Triplet<String, Integer, String>
                triplet = new Triplet<String, Integer, String>(
                    "Triplet 1", pair.getValue0(), pair.getValue1());
      
            // Print the Triplet
            System.out.println("Triplet: " + triplet);
        }
    }

    
    

    Output:

    Pair: [1, Lazyroar]
    Triplet: [Triplet 1, 1, Lazyroar]
  2. Using Pair.add() method




    import java.util.*;
    import org.javatuples.*;
      
    class GfG {
        public static void main(String[] args)
        {
            // create Pair
            Pair<Integer, String>
                pair = new Pair<Integer, String>(
                    Integer.valueOf(1), "Lazyroar");
      
            // Print the Pair
            System.out.println("Pair: " + pair);
      
            // Using add() to create Triplet
            Triplet<Integer, String, String>
                triplet = pair.add("Triplet 1");
      
            // Print the Triplet
            System.out.println("Triplet: " + triplet);
        }
    }

    
    

    Output:

    Pair: [1, Lazyroar]
    Triplet: [1, Lazyroar, Triplet 1]
  3. Using Pair.addAtX() method

    Program 1: Adding at Position 0 using addAt0()




    import java.util.*;
    import org.javatuples.*
      
        class GfG {
        public static void main(String[] args)
        {
            // create Pair
            Pair<Integer, String>
                pair = new Pair<Integer, String>(
                    Integer.valueOf(1), "Lazyroar");
      
            // Print the Pair
            System.out.println("Pair: " + pair);
      
            // Using add() to create Triplet
            Triplet<String, Integer, String>
                triplet = pair.addAt0("Triplet 1");
      
            // Print the Triplet
            System.out.println("Triplet: " + triplet);
        }
    }

    
    

    Output:

    Pair: [1, Lazyroar]
    Triplet: [Triplet 1, 1, Lazyroar]

    Program 2: Adding at Position 1 using addAt1()




    import java.util.*;
    import org.javatuples.*;
      
    class GfG {
        public static void main(String[] args)
        {
            // create Pair
            Pair<Integer, String>
                pair = new Pair<Integer, String>(
                    Integer.valueOf(1), "Lazyroar");
      
            // Print the Pair
            System.out.println("Pair: " + pair);
      
            // Using add() to create Triplet
            Triplet<Integer, String, String>
                triplet = pair.addAt1("Triplet 1");
      
            // Print the Triplet
            System.out.println("Triplet: " + triplet);
        }
    }

    
    

    Output:

    Pair: [1, Lazyroar]
    Triplet: [1, Triplet 1, Lazyroar]

    Program 2: Adding at Position 2 using addAt2()




    // Below is a Java program to demonstrate
    // use of addAt2() method with
    // direct value
      
    import java.util.*;
    import org.javatuples.*
      
        class GfG {
        public static void main(String[] args)
        {
            // create Pair
            Pair<Integer, String>
                pair = new Pair<Integer, String>(
                    Integer.valueOf(1), "Lazyroar");
      
            // Print the Pair
            System.out.println("Pair: " + pair);
      
            // Using add() to create Triplet
            Triplet<Integer, String, String>
                triplet = pair.addAt2("Triplet 1");
      
            // Print the Triplet
            System.out.println("Triplet: " + triplet);
        }
    }

    
    

    Output:

    Pair: [1, Lazyroar]
    Triplet: [1, Lazyroar, Triplet 1]
RELATED ARTICLES

Most Popular

Dominic
32337 POSTS0 COMMENTS
Milvus
86 POSTS0 COMMENTS
Nango Kala
6706 POSTS0 COMMENTS
Nicole Veronica
11871 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11934 POSTS0 COMMENTS
Shaida Kate Naidoo
6822 POSTS0 COMMENTS
Ted Musemwa
7088 POSTS0 COMMENTS
Thapelo Manthata
6779 POSTS0 COMMENTS
Umr Jansen
6778 POSTS0 COMMENTS