Author: saqibkhan

  • Quintet Class

    Introduction

    The org.javatuples.Quintet class represents a Tuple with five elements.

    Class Declaration

    Following is the declaration for org.javatuples.Quintet class −

    public final class Quintet<A, B, C, D, E>
       extends Tuple
    
      implements IValue0&lt;A&gt;, IValue1&lt;B&gt;, 
         IValue2&lt;C&gt;, IValue3&lt;D&gt;, IValue4&lt;E&gt;

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Class Constructor

    Sr.No.Constructor & Description
    1Quintet(A value0, B value1, C value2, D value3, E value4)This creates a Quintet Tuple.

    Class Methods

    Similarly setAt1() upto setAt4() set the value at index 1, and so on.

    Sr.No.Method & Description
    1Sextet add(Unit tuple)This method returns a Sextet tuple.Similarly other methods to add tuples are available e.g. add(Pair tuple) returns Septet and upto add(Quintet tuple) returns Decade tuple.
    2Sextet add(X0 value)This method add a value to the tuple and returns a Sextet tuple.Similarly other methods to add values are available e.g. add(X0 value0, X1 value1) returns Septet and so on upto add() with five parameters.
    3Sextet addAt0(Unit value)This method add a Unit tuple at index 0 and returns a Sextet tuple.Similarly other methods to add tuples are available e.g. addAt0(Pair value) returns Septet and so on upto addAt0(Quintet). Other similar method are addAt1(Unit value) which add a unit at index0 and have similar methods upto addAt4(Quintet).
    4Sextet addAt0(X0 value)This method add a value at index 0 and returns a Sextet tuple.Similarly other methods to add values are available e.g. addAt0(X0 value0, X1 value1) returns Septet and so on upto addAt0() with five parameters. Other similar method are addAt1(X0 value) which add a value at index0 and have similar methods upto addAt4() with five parameters.
    5static <X> Quintet<X,X,X,X,X> fromArray(X[] array)Create tuple from array.
    6static <X> Quintet<X,X,X,X,X> fromCollection(Collection<X> collection)Create tuple from collection.
    7static <X> Quintet<X,X,X,X,X> fromIterable(Iterable<X> iterable)Create tuple from iterable.
    8static <X> Quintet<X,X,X,X,X> fromIterable(Iterable<X> iterable, int index)Create tuple from iterable, starting from the specified index.
    9int getSize()Return the size of the tuple.
    10A getValue0()Returns the value of the tuple at index 0.Similarly getValue1() upto getValue4() returns the value at index 1 and so on.
    11Quartet<B,C,D,E> removeFrom0()Return the tuple after removing value of the tuple at index 0.Similarly removeFrom1() upto removeFrom4() returns the tuple after removing value of the tuple at index 1 and so on.
    12<X> Quintet<X,B,C,D,E> setAt0(X value)Set the value of the tuple at index 0.
    13static <A> Quintet<A,B,C,D,E> with(A value0, B value1, C value2, D value3, E value4)Create the tuple using given value.

    Methods inherite

    This class inherits methods from the following classes −

    • org.javatuples.Tuple
    • Object

    Example

    Let’s see Quintet Class in action. Here we’ll see how to use various methods.

    Create a java class file named TupleTester in C:\>JavaTuples.

    File: TupleTester.java

    package com.tutorialspoint;
    import java.util.ArrayList;
    import java.util.List;
    import org.javatuples.Quartet;
    import org.javatuples.Quintet;
    import org.javatuples.Sextet;
    import org.javatuples.Triplet;
    
    public class TupleTester {
       public static void main(String args[]){
    
      Quintet&lt;Integer, Integer, Integer, Integer, Integer&gt; quintet 
         = Quintet.with(5, 6, 7,8,9);
      System.out.println(quintet);
      boolean isPresent = quintet.contains(5);
      System.out.println("5 is present: " + isPresent);
      List&lt;Integer&gt; list = new ArrayList&lt;&gt;();
      list.add(1);
      list.add(2);
      list.add(3);
      list.add(4);
      list.add(5);
      Sextet&lt;Integer, Integer, Integer, Integer, Integer, String&gt; sextet 
         = quintet.add("Test");
      System.out.println(sextet);
      Integer value = quintet.getValue0();
      System.out.println(value);
      Quartet&lt;Integer, Integer, Integer, Integer&gt; quartet = quintet.removeFrom0();
      System.out.println(quartet);
      Quintet&lt;Integer, Integer, Integer, Integer, Integer&gt; quintet1 
         = Quintet.fromCollection(list);   
      System.out.println(quintet1);
    } }

    Verify the result

    Compile the classes using javac compiler as follows −

    C:\JavaTuples>javac -cp javatuples-1.2.jar ./com/tutorialspoint/TupleTester.java
    

    Now run the TupleTester to see the result −

    C:\JavaTuples>java  -cp .;javatuples-1.2.jar com.tutorialspoint.TupleTester
    

    Output

    Verify the Output

    [5, 6, 7, 8, 9]
    5 is present: true
    [5, 6, 7, 8, 9, Test]
    5
    [6, 7, 8, 9]
    [1, 2, 3, 4, 5]
    
  • Quartet Class

    Introduction

    The org.javatuples.Quartet class represents a Tuple with four elements.

    Class Declaration

    Following is the declaration for org.javatuples.Quartet class −

    public final class Quartet<A, B, C, D>
       extends Tuple
    
      implements IValue0&lt;A&gt;, IValue1&lt;B&gt;, IValue2&lt;C&gt;, IValue3&lt;D&gt;

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Class Constructor

    Sr.No.Constructor & Description
    1Quartet(A value0, B value1, C value2, D value3)This creates a Quartet Tuple.

    Class Methods

    Similarly setAt1() upto setAt3() set the value at index 1, and so on.

    Sr.No.Method & Description
    1Quintet add(Unit tuple)This method returns a Quintet tuple.Similarly other methods to add tuples are available e.g. add(Pair tuple) returns Sextet and upto add(Sextet tuple) returns Decade tuple.
    2Quintet add(X0 value)This method add a value to the tuple and returns a Quintet tuple.Similarly other methods to add values are available e.g. add(X0 value0, X1 value1) returns Sextet and so on upto add() with six parameters.
    3Quintet addAt0(Unit value)This method add a Unit tuple at index 0 and returns a Quintet tuple.Similarly other methods to add tuples are available e.g. addAt0(Pair value) returns Sextet and so on upto addAt0(Sextet). Other similar method are addAt1(Unit value) which add a unit at index0 and have similar methods upto addAt2(Sextet).
    4Quintet addAt0(X0 value)This method add a value at index 0 and returns a Quintet tuple.Similarly other methods to add values are available e.g. addAt0(X0 value0, X1 value1) returns Sextet and so on upto addAt0() with six parameters. Other similar method are addAt1(X0 value) which add a value at index0 and have similar methods upto addAt2() with six parameters.
    5static <X> Quartet<X,X,X,X> fromArray(X[] array)Create tuple from array.
    6static <X> Quartet<X,X,X,X> fromCollection(Collection<X> collection)Create tuple from collection.
    7static <X> Quartet<X,X,X,X> fromIterable(Iterable<X> iterable)Create tuple from iterable.
    8static <X> Quartet<X,X,X,X> fromIterable(Iterable<X> iterable, int index)Create tuple from iterable, starting from the specified index.
    9int getSize()Return the size of the tuple.
    10A getValue0()Returns the value of the tuple at index 0.Similarly getValue1() upto getValue3() returns the value at index 1 and so on.
    11Triplet<B,C,D> removeFrom0()Return the tuple after removing value of the tuple at index 0.Similarly removeFrom1() upto removeFrom3() returns the tuple after removing value of the tuple at index 1 and so on.
    12<X> Quartet<X,B,C,D> setAt0(X value)Set the value of the tuple at index 0.
    13static <A> Quartet<A,B,C,D> with(A value0, B value1, C value2, D value3)Create the tuple using given value.

    Methods inherite

    This class inherits methods from the following classes −

    • org.javatuples.Tuple
    • Object

    Example

    Let’s see Quartet Class in action. Here we’ll see how to use various methods.

    Create a java class file named TupleTester in C:\>JavaTuples.

    File: TupleTester.java

    package com.tutorialspoint;
    import java.util.ArrayList;
    import java.util.List;
    import org.javatuples.Quartet;
    import org.javatuples.Quintet;
    import org.javatuples.Triplet;
    
    public class TupleTester {
       public static void main(String args[]){
    
      Quartet&lt;Integer, Integer, Integer, Integer&gt; quartet = Quartet.with(
         5, 6, 7,8
      );
      System.out.println(quartet);
      boolean isPresent = quartet.contains(5);
      System.out.println("5 is present: " + isPresent);
      List&lt;Integer&gt; list = new ArrayList&lt;&gt;();
      list.add(1);
      list.add(2);
      list.add(3);
      list.add(4);
      Quintet&lt;Integer, Integer, Integer, Integer, String&gt; quintet = quartet.add("Test");
      System.out.println(quintet);
      Integer value = quartet.getValue0();
      System.out.println(value);
      Triplet&lt;Integer, Integer, Integer&gt; triplet = quartet.removeFrom0();
      System.out.println(triplet);
      Quartet&lt;Integer, Integer, Integer, Integer&gt; quartet1 = Quartet.fromCollection(list);   
      System.out.println(quartet1);
    } }

    Verify the result

    Compile the classes using javac compiler as follows −

    C:\JavaTuples>javac -cp javatuples-1.2.jar ./com/tutorialspoint/TupleTester.java
    

    Now run the TupleTester to see the result −

    C:\JavaTuples>java  -cp .;javatuples-1.2.jar com.tutorialspoint.TupleTester
    

    Output

    Verify the Output

    [5, 6, 7, 8]
    5 is present: true
    [5, 6, 7, 8, Test]
    5
    [6, 7, 8]
    [1, 2, 3, 4]
    
  • Triplet Class

    Introduction

    The org.javatuples.Triplet class represents a Tuple with three elements.

    Class Declaration

    Following is the declaration for org.javatuples.Triplet class −

    public final class Triplet<A,B,C>
       extends Tuple
    
      implements IValue0&lt;A&gt;, IValue1&lt;B&gt;, IValue2&lt;C&gt;

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Class Constructors

    Sr.No.Constructor & Description
    1Triplet(A value0, B value1, C value2)This creates a Triplet Tuple.

    Class Methods

    Similarly setAt1() upto setAt2() set the value at index 1 and so on.

    Sr.No.Method & Description
    1Quartet add(Unit tuple)This method returns a Quartet tuple.Similarly other methods to add tuples are available e.g. add(Pair tuple) returns Quintet and upto add(Septet tuple) returns Decade tuple.
    2Quartet add(X0 value)This method add a value to the tuple and returns a Quartet tuple.Similarly other methods to add values are available e.g. add(X0 value0, X1 value1) returns Quintet and so on upto add() with seven parameters.
    3Quartet addAt0(Unit value)This method add a Unit tuple at index 0 and returns a Quartet tuple.Similarly other methods to add tuples are available e.g. addAt0(Pair value) returns Quintet and so on upto addAt0(Septet). Other similar method are addAt1(Unit value) which add a unit at index0 and have similar methods upto addAt2(Septet).
    4Quartet addAt0(X0 value)This method add a value at index 0 and returns a Quartet tuple.Similarly other methods to add values are available e.g. addAt0(X0 value0, X1 value1) returns Quintet and so on upto addAt0() with seven parameters. Other similar method are addAt1(X0 value) which add a value at index0 and have similar methods upto addAt2() with seven parameters.
    5static <X> Triplet<X,X,X> fromArray(X[] array)Create tuple from array.
    6static <X> Triplet<X,X,X> fromCollection(Collection<X> collection)Create tuple from collection.
    7static <X> Triplet<X,X,X> fromIterable(Iterable<X> iterable)Create tuple from iterable.
    8static <X> Triplet<X,X,X> fromIterable(Iterable<X> iterable, int index)Create tuple from iterable, starting from the specified index.
    9int getSize()Return the size of the tuple.
    10A getValue0()Returns the value of the tuple at index 0.Similarly getValue1() upto getValue2() returns the value at index 1 and so on.
    11Pair<B,C> removeFrom0()Return the tuple after removing value of the tuple at index 0.Similarly removeFrom1() upto removeFrom2() returns the tuple after removing value of the tuple at index 1 and so on.
    12<X> Triplet<X,B,C> setAt0(X value)Set the value of the tuple at index 0.
    13static <A> Triplet<A,B,C> with(A value0, B value1, C value2)Create the tuple using given value.

    Methods inherite

    This class inherits methods from the following classes −

    • org.javatuples.Tuple
    • Object

    Example

    Let’s see Triplet Class in action. Here we’ll see how to use various methods.

    Create a java class file named TupleTester in C:\>JavaTuples.

    File: TupleTester.java

    package com.tutorialspoint;
    import java.util.ArrayList;
    import java.util.List;
    import org.javatuples.Pair;
    import org.javatuples.Quartet;
    import org.javatuples.Triplet;
    
    public class TupleTester {
       public static void main(String args[]){
    
      Triplet&lt;Integer, Integer, Integer&gt; triplet = Triplet.with(5, 6, 7);
      System.out.println(triplet);
      boolean isPresent = triplet.contains(5);
      System.out.println("5 is present: " + isPresent);
      List&lt;Integer&gt; list = new ArrayList&lt;&gt;();
      list.add(1);
      list.add(2);
      list.add(3);
      Quartet&lt;Integer, Integer, Integer, String&gt; quartet = triplet.add("Test");
      System.out.println(quartet);
      Integer value = triplet.getValue0();
      System.out.println(value);
      Pair&lt;Integer, Integer&gt; pair = triplet.removeFrom0();
      System.out.println(pair);
      Triplet&lt;Integer, Integer, Integer&gt; triplet1 = 
         Triplet.fromCollection(list);   
      System.out.println(triplet1);
    } }

    Verify the result

    Compile the classes using javac compiler as follows −

    C:\JavaTuples>javac -cp javatuples-1.2.jar ./com/tutorialspoint/TupleTester.java
    

    Now run the TupleTester to see the result −

    C:\JavaTuples>java  -cp .;javatuples-1.2.jar com.tutorialspoint.TupleTester
    

    Output

    Verify the Output

    [5, 6, 7]
    5 is present: true
    [5, 6, 7, Test]
    5
    [6, 7]
    [1, 2, 3]
    
  • Pair Class

    Introduction

    The org.javatuples.Pair class represents a Tuple with two elements.

    Class Declaration

    Following is the declaration for org.javatuples.Pair class −

    public final class Pair<A,B>
       extends Tuple
    
      implements IValue0&lt;A&gt;, IValue1&lt;B&gt;

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Class Constructor

    Sr.No.Constructor & Description
    1Pair(A value0, B value1)This creates a Pair Tuple.

    Class Methods

    Similarly setAt1() set the value at index 1.

    Sr.No.Method & Description
    1Triplet add(Unit tuple)This method returns a Triplet tuple.Similarly other methods to add tuples are available e.g. add(Pair tuple) returns Quartet and upto add(Octet tuple) returns Decade tuple.
    2Triplet add(X0 value)This method add a value to the tuple and returns a Triplet tuple.Similarly other methods to add values are available e.g. add(X0 value0, X1 value1) returns Quartet and so on upto add() with eight parameters.
    3Triplet addAt0(Unit value)This method add a Unit tuple at index 0 and returns a Triplet tuple.Similarly other methods to add tuples are available e.g. addAt0(Pair value) returns Quartet and so on upto addAt0(Octet). Other similar method are addAt1(Unit value) which add a unit at index0 and have similar methods upto addAt2(Octet).
    4Triplet addAt0(X0 value)This method add a value at index 0 and returns a Triplet tuple.Similarly other methods to add values are available e.g. addAt0(X0 value0, X1 value1) returns Quartet and so on upto addAt0() with eight parameters. Other similar method are addAt1(X0 value) which add a value at index0 and have similar methods upto addAt2() with eight parameters.
    5static <X> Pair<X,X> fromArray(X[] array)Create tuple from array.
    6static <X> Pair<X,X> fromCollection(Collection<X> collection)Create tuple from collection.
    7static <X> Pair<X,X> fromIterable(Iterable<X> iterable)Create tuple from iterable.
    8static <X> Pair<X,X> fromIterable(Iterable<X> iterable, int index)Create tuple from iterable, starting from the specified index.
    9int getSize()Return the size of the tuple.
    10A getValue0()Returns the value of the tuple at index 0.Similarly getValue1() returns the value at index 1.
    11Unit<B> removeFrom0()Return the tuple after removing value of the tuple at index 0.Similarly removeFrom1() returns the tuple after removing value of the tuple at index 1.
    12<X> Pair<X,B> setAt0(X value)Set the value of the tuple at index 0.
    13static <A,B> Pair<A,B> with(A value0, B value1)Create the tuple using given value.

    Methods inherite

    This class inherits methods from the following classes −

    • org.javatuples.Tuple
    • Object

    Example

    Let’s see Pair Class in action. Here we’ll see how to use various methods.

    Create a java class file named TupleTester in C:\>JavaTuples.

    File: TupleTester.java

    package com.tutorialspoint;
    import java.util.ArrayList;
    import java.util.List;
    import org.javatuples.Pair;
    import org.javatuples.Triplet;
    import org.javatuples.Unit;
    
    public class TupleTester {
       public static void main(String args[]){
    
      Pair&lt;Integer, Integer&gt; pair = Pair.with(5,6);
      System.out.println(pair);
      boolean isPresent = pair.contains(5);
      System.out.println("5 is present: " + isPresent);
      List&lt;Integer&gt; list = new ArrayList&lt;&gt;();
      list.add(1);
      list.add(2);
      Triplet&lt;Integer,Integer, String&gt; triplet = pair.add("Test");
      System.out.println(triplet);
      Integer value = pair.getValue0();
      System.out.println(value);
      Unit&lt;Integer&gt; unit = pair.removeFrom0();
      System.out.println(unit);
      Pair&lt;Integer, Integer&gt; pair1 = Pair.fromCollection(list);   
      System.out.println(pair1);
    } }

    Verify the result

    Compile the classes using javac compiler as follows −

    C:\JavaTuples>javac -cp javatuples-1.2.jar ./com/tutorialspoint/TupleTester.java
    

    Now run the TupleTester to see the result −

    C:\JavaTuples>java  -cp .;javatuples-1.2.jar com.tutorialspoint.TupleTester
    

    Output

    Verify the Output

    [5, 6]
    5 is present: true
    [5, 6, Test]
    5
    [6]
    [1, 2]
    
  • Unit Class

    Introduction

    The org.javatuples.Unit class represents a Tuple with single element.

    Class declaration

    Following is the declaration for org.javatuples.Unit class −

    public final class Unit<A>
       extends Tuple
    
      implements IValue0&lt;A&gt;

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Class constructors

    Sr.No.Constructor & Description
    1Unit(A value0)This creates a Unit Tuple.

    Class Methods

    Sr.No.Method & Description
    1Pair add(Unit tuple)This method returns a Pair tuple.Similarly other methods to add tuples are available e.g. add(Pair tuple) returns Triplet and upto add(Ennead tuple) returns Decade tuple.
    2Pair add(X0 value)This method add a value to the tuple and returns a Pair tuple.Similarly other methods to add values are available e.g. add(X0 value0, X1 value1) returns Triplet and so on upto add() with nine parameters.
    3Pair addAt0(Unit value)This method add a Unit tuple at index 0 and returns a Pair tuple.Similarly other methods to add tuples are available e.g. addAt0(Pair value) returns Triplet and so on upto addAt0(Ennead). Other similar method are addAt1(Unit value) which add a unit at index0 and have similar methods upto addAt1(Ennead).
    4Pair addAt0(X0 value)This method add a value at index 0 and returns a Pair tuple.Similarly other methods to add values are available e.g. addAt0(X0 value0, X1 value1) returns Triplet and so on upto addAt0() with nine parameters. Other similar method are addAt1(X0 value) which add a value at index0 and have similar methods upto addAt1() with nine parameters.
    5static <X> Unit<X> fromArray(X[] array)Create tuple from array.
    6static <X> Unit<X> fromCollection(Collection<X> collection)Create tuple from collection.
    7static <X> Unit<X> fromIterable(Iterable<X> iterable)Create tuple from iterable.
    8static <X> Unit<X> fromIterable(Iterable<X> iterable, int index)Create tuple from iterable, starting from the specified index.
    9int getSize()Return the size of the tuple.
    10A getValue0()Return the value of the tuple.
    11<X> Unit<X> setAt0(X value)Set the value of the tuple.
    12static <A> Unit<A> with(A value0)Create the tuple using given value.

    Methods inherits

    This class inherits methods from the following classes −

    • org.javatuples.Tuple
    • Object

    Example

    Let’s see Unit Class in action. Here we’ll see how to use various methods.

    Create a java class file named TupleTester in C:\>JavaTuples.

    File: TupleTester.java

    package com.tutorialspoint;
    import java.util.ArrayList;
    import java.util.List;
    import org.javatuples.Pair;
    import org.javatuples.Unit;
    public class TupleTester {
       public static void main(String args[]){
    
      Unit&lt;Integer&gt; unit = Unit.with(5);
      System.out.println(unit);
      boolean isPresent = unit.contains(5);
      System.out.println("5 is present: " + isPresent);
      List&lt;Integer&gt; list = new ArrayList&lt;&gt;();
      list.add(1);
      Pair&lt;Integer, String&gt; pair = unit.add("Test");
      System.out.println(pair);
      Integer value = unit.getValue0();
      System.out.println(value);
      Unit&lt;Integer&gt; unit1 = Unit.fromCollection(list);   
      System.out.println(unit1);
    } }

    Verify the result

    Compile the classes using javac compiler as follows −

    C:\JavaTuples>javac -cp javatuples-1.2.jar ./com/tutorialspoint/TupleTester.java
    

    Now run the TupleTester to see the result −

    C:\JavaTuples>java  -cp .;javatuples-1.2.jar com.tutorialspoint.TupleTester
    

    Output

    Verify the Output

    [5]
    5 is present: true
    [5, Test]
    5
    [1]
    
  • Checking Elements

    Each tuple provides utility methods to check their elements in similar fashion as collection.

    • contains(element) − checks if element is present or not.
    • containsAll(collection) − checks if elements are present or not.
    • indexOf(element) − returns the index of first element if present otherwise -1.
    • lastIndexOf(element) − returns the index of last element if present otherwise -1.
    Pair<String, Integer> pair = Pair.with("Test", Integer.valueOf(5)); 
    boolean isPresent = pair.contains("Test");
    

    Example

    Let’s see JavaTuples in action. Here we’ll see how to check elements in a tuple.

    Create a java class file named TupleTester in C:\>JavaTuples.

    File: TupleTester.java

    package com.tutorialspoint;
    import java.util.List;
    import org.javatuples.Quartet;
    public class TupleTester {
       public static void main(String args[]){
    
      Quartet&lt;String, Integer, String, String&gt; quartet = Quartet.with(
         "Test1", Integer.valueOf(5), "Test3", "Test3"
      );
      System.out.println(quartet);
      boolean isPresent = quartet.contains(5);
      System.out.println("5 is present: " + isPresent);
      isPresent = quartet.containsAll(List.of("Test1", "Test3"));   
      System.out.println("Test1, Test3 are present: " + isPresent);
      int indexOfTest3 = quartet.indexOf("Test3");
      System.out.println("First Test3 is present at: " + indexOfTest3);
      int lastIndexOfTest3 = quartet.lastIndexOf("Test3");
      System.out.println("Last Test3 is present at: " + lastIndexOfTest3);
    } }

    Verify the result

    Compile the classes using javac compiler as follows −

    C:\JavaTuples>javac -cp javatuples-1.2.jar ./com/tutorialspoint/TupleTester.java
    

    Now run the TupleTester to see the result −

    C:\JavaTuples>java  -cp .;javatuples-1.2.jar com.tutorialspoint.TupleTester
    

    Output

    Verify the Output

    [Test1, 5, Test3, Test3]
    5 is present: true
    Test1, Test3 are present: true
    First Test3 is present at: 2
    Last Test3 is present at: 3
    
  • Iteration

    Each tuple implements Iterable interface and can be iterated in similar fashion as collection.

    Pair<String, Integer> pair = Pair.with("Test", Integer.valueOf(5)); 
    for(Object object: Pair){
    	System.out.println(object);
    }
    

    Example

    Let’s see JavaTuples in action. Here we’ll see how to iterate tuples.

    Create a java class file named TupleTester in C:\>JavaTuples.

    File: TupleTester.java

    package com.tutorialspoint;
    import org.javatuples.Quartet;
    import org.javatuples.Triplet;
    public class TupleTester {
       public static void main(String args[]){
    
      Triplet&lt;String, Integer, String&gt; triplet = Triplet.with(
         "Test1", Integer.valueOf(5), "Test2"
      );
      for(Object object: triplet) {
         System.out.print(object + " " );
      }
      System.out.println();
      System.out.println(triplet);
      String&#91;] strArray = new String&#91;] {"a", "b" , "c" , "d"};
      Quartet&lt;String, String, String, String&gt; quartet = Quartet.fromArray(strArray);
      for(Object object: quartet) {
         System.out.print(object + " " );
      }
      System.out.println();
      System.out.println("Quartet:" + quartet);
    } }

    Verify the result

    Compile the classes using javac compiler as follows −

    C:\JavaTuples>javac -cp javatuples-1.2.jar ./com/tutorialspoint/TupleTester.java
    

    Now run the TupleTester to see the result −

    C:\JavaTuples>java  -cp .;javatuples-1.2.jar com.tutorialspoint.TupleTester
    

    Output

    Verify the Output

    Test1 5 Test2 
    [Test1, 5, Test2]
    a b c d 
    Quartet:[a, b, c, d]
    
  • Conversion

    Tuple to List/Array

    A tuple can be converted to List/Array but at cost of type safety and converted list is of type List<Object>/Object[].

    List<Object> list = triplet.toList();
    Object[] array = triplet.toArray();
    

    Collection/Array to Tuple

    A collection can be converted to tuple using fromCollection() method and array can be converted to tuple using fromArray() method.

    Pair<String, Integer> pair = Pair.fromCollection(list);
    Quartet<String,String,String,String> quartet = Quartet.fromArray(array); 
    

    If size of array/collection is different than that of tuple, then IllegalArgumentException will occur.

    Exception in thread "main" java.lang.IllegalArgumentException: 
    Array must have exactly 4 elements in order to create a Quartet. Size is 5
       at ...	
    

    Example

    Let’s see JavaTuples in action. Here we’ll see how to convert tuple to list/array and vice versa.

    Create a java class file named TupleTester in C:\>JavaTuples.

    File: TupleTester.java

    package com.tutorialspoint;
    import java.util.List;
    import org.javatuples.Quartet;
    import org.javatuples.Triplet;
    public class TupleTester {
       public static void main(String args[]){
    
      Triplet&lt;String, Integer, String&gt; triplet = Triplet.with(
         "Test1", Integer.valueOf(5), "Test2"
      );
      List&lt;Object&gt; list = triplet.toList();
      Object&#91;] array = triplet.toArray();
      System.out.println("Triplet:" + triplet);
      System.out.println("List: " + list);  
      System.out.println();
      for(Object object: array) {
         System.out.print(object + " " );
      }
      System.out.println();
      String&#91;] strArray = new String&#91;] {"a", "b" , "c" , "d"};
      Quartet&lt;String, String, String, String&gt; quartet = Quartet.fromArray(strArray);
      System.out.println("Quartet:" + quartet);      
    } }

    Verify the result

    Compile the classes using javac compiler as follows −

    C:\JavaTuples>javac -cp javatuples-1.2.jar ./com/tutorialspoint/TupleTester.java
    

    Now run the TupleTester to see the result −

    C:\JavaTuples>java  -cp .;javatuples-1.2.jar com.tutorialspoint.TupleTester
    

    Output

    Verify the Output

    Triplet:[Test1, 5, Test2]
    List: [Test1, 5, Test2]
    
    Test1 5 Test2 
    Quartet:[a, b, c, d]
    
  • Remove Elements

    A tuple has removeAtX() methods to remove value at particular index. For example Triplet class has following methods.

    • removeAt0() − remove value at index 0 and return the resulted tuple.
    • removeAt1() − remove value at index 1 and return the resulted tuple.
    • removeAt2() − remove value at index 2 and return the resulted tuple.

    Removing an element returns a new tuple.

    Example

    Let’s see JavaTuples in action. Here we’ll see how to remove value in a tuple.

    Create a java class file named TupleTester in C:\>JavaTuples.

    File: TupleTester.java

    package com.tutorialspoint;
    import org.javatuples.Pair;
    import org.javatuples.Triplet;
    public class TupleTester {
       public static void main(String args[]){
    
      Triplet&lt;String, Integer, String&gt; triplet = Triplet.with(
         "Test1", Integer.valueOf(5), "Test2"
      );
      Pair&lt;String, Integer&gt; pair = triplet.removeFrom2();
      System.out.println("Triplet:" + triplet);
      System.out.println("Pair: " + pair);  
    } }

    Verify the result

    Compile the classes using javac compiler as follows −

    C:\JavaTuples>javac -cp javatuples-1.2.jar ./com/tutorialspoint/TupleTester.java
    

    Now run the TupleTester to see the result −

    C:\JavaTuples>java  -cp .;javatuples-1.2.jar com.tutorialspoint.TupleTester
    

    Output

    Verify the Output

    Triplet:[Test1, 5, Test2]
    Pair: [Test1, 5]
    
  • Add Elements

    A tuple has add() method at the end of a tuple and it changes the type of tuple as well. For example adding a element to Triplet tuple will convert it to a Quartet tuple.

    Quartet<String,String,String,String> quartet = triplet.add("Test");
    

    A tuple has addAtX() methods as well to add a position at particular index starting from 0.

    Quartet<String,String,String,String> quartet = triplet.addAt1("Test");
    

    A tuple can add more than one elements using addAtX() methods.

    Quartet<String,String,String,String> quartet = pair.addAt1("Test1", "Test2");
    

    A tuple can add a tuple as well using addAtX() methods.

    Quartet<String,String,String,String> quartet = pair.addAt1(pair1);
    

    Example

    Let’s see JavaTuples in action. Here we’ll see how to add values in a tuple using various ways.

    Create a java class file named TupleTester in C:\>JavaTuples.

    File: TupleTester.java

    package com.tutorialspoint;
    import org.javatuples.Pair;
    import org.javatuples.Quartet;
    import org.javatuples.Quintet;
    import org.javatuples.Triplet;
    public class TupleTester {
       public static void main(String args[]){
    
      Pair&lt;String, Integer&gt; pair = Pair.with("Test", Integer.valueOf(5));   
      Triplet&lt;String, Integer, String&gt; triplet = pair.add("Test2");
      Quartet&lt;String, String, Integer, String&gt; quartet = triplet.addAt1("Test1");
      Quintet&lt;String, Integer, String, String, Integer&gt; quintet = triplet.add(pair);
      System.out.println("Pair: " + pair);
      System.out.println("Triplet:" + triplet);
      System.out.println("Quartet:" + quartet);
      System.out.println("Quintet:" + quintet);     
    } }

    Verify the result

    Compile the classes using javac compiler as follows −

    C:\JavaTuples>javac -cp javatuples-1.2.jar ./com/tutorialspoint/TupleTester.java
    

    Now run the TupleTester to see the result −

    C:\JavaTuples>java  -cp .;javatuples-1.2.jar com.tutorialspoint.TupleTester
    

    Output

    Verify the Output

    Pair: [Test, 5]
    Triplet:[Test, 5, Test2]
    Quartet:[Test, Test1, 5, Test2]
    Quintet:[Test, 5, Test2, Test, 5]