Implement Pair Class with Unit Class in Java using JavaTuples

Following are the ways to implement Pair Class with Unit Class

  1. Using direct values
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    Unit: [GeeksforGeeks]
    Pair: [1, GeeksforGeeks]
    
  2. Using Unit.add() method
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Below is a Java program to demonstrate
    // use of add() method with
    // single value
      
    import java.util.*;
    import org.javatuples.*;
      
    class GfG {
        public static void main(String[] args)
        {
            // Using with() method to instantiate unit object
            Unit<String> unit = Unit.with("Geeks");
      
            // print unit
            System.out.println("Unit: " + unit);
      
            // Using add() to create Pair
            Pair<String, String> pair = unit.add("forGeeks");
      
            // Print the Pair
            System.out.println("Pair: " + pair);
        }
    }

    chevron_right

    
    

    Output:



    Unit: [Geeks]
    Pair: [Geeks, forGeeks]
    
  3. Using Unit.addAtX() method
    • Program 1: Adding Unit at Position 0 using addAt0()
      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      // Below is a Java program to demonstrate
      // use of addAt0() method with
      // direct value
        
      import java.util.*;
      import org.javatuples.Unit;
      import org.javatuples.Pair;
        
      class GfG {
          public static void main(String[] args)
          {
              // Using with() method to instantiate unit object
              Unit<String> unit = Unit.with("Geeks");
        
              // print unit
              System.out.println("Unit: " + unit);
        
              // Using addAtX() to create Pair
              Pair<String, String> pair = unit.addAt0("forGeeks");
        
              // Print the Pair
              System.out.println("Pair: " + pair);
          }
      }

      chevron_right

      
      

      Output:

      Unit: [Geeks]
      Pair: [forGeeks, Geeks]
      
    • Program 2: Adding Unit at Position 1 using addAt1()
      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      // Below is a Java program to demonstrate
      // use of addAt1() method with
      // direct value
        
      import java.util.*;
      import org.javatuples.Unit;
      import org.javatuples.Pair;
        
      class GfG {
          public static void main(String[] args)
          {
              // Using with() method to instantiate unit object
              Unit<String> unit = Unit.with("Geeks");
        
              // print unit
              System.out.println("Unit: " + unit);
        
              // Using addAtX() to create Pair
              Pair<String, String> pair = unit.addAt1("forGeeks");
        
              // Print the Pair
              System.out.println("Pair: " + pair);
          }
      }

      chevron_right

      
      

      Output:

      Unit: [Geeks]
      Pair: [Geeks, forGeeks]
      


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.