Tuesday, November 19, 2024
Google search engine
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

Recent Comments