Tutorialspoint.dev

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), "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]


This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter