Skip to content
Related Articles

Related Articles

Implement Triplet Class with Pair Class in Java using JavaTuples
  • Difficulty Level : Basic
  • Last Updated : 20 Sep, 2018
GeeksforGeeks - Summer Carnival Banner

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), "GeeksforGeeks");
      
            // 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, GeeksforGeeks]
    Triplet: [Triplet 1, 1, GeeksforGeeks]
  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), "GeeksforGeeks");
      
            // 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, GeeksforGeeks]
    Triplet: [1, GeeksforGeeks, 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), "GeeksforGeeks");
      
            // 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, GeeksforGeeks]
    Triplet: [Triplet 1, 1, GeeksforGeeks]

    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), "GeeksforGeeks");
      
            // 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, GeeksforGeeks]
    Triplet: [1, Triplet 1, GeeksforGeeks]

    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), "GeeksforGeeks");
      
            // 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, GeeksforGeeks]
    Triplet: [1, GeeksforGeeks, Triplet 1]

    Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

    My Personal Notes arrow_drop_up
Recommended Articles
Page :