Category: 08. Java Tuples

https://cdn-icons-png.flaticon.com/512/226/226777.png

  • Implementing Decade Using Ennead Class

    Problem Description

    How to implement Decade class using Ennead class?

    Example

    Following example shows how to accomplish the above task. Each tuple has add() and addAtX() methods to convert the tuple.

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

    File: TupleTester.java

    package com.tutorialspoint;
    import org.javatuples.Decade;
    import org.javatuples.Ennead;
    public class TupleTester {
       public static void main(String args[]){
    
      Ennead<Integer, Integer, Integer, Integer, Integer, Integer,
         Integer, Integer, Integer> ennead = Ennead.with(5,6,7,8,9,10,11,12,13);
      System.out.println(ennead);
      Decade<Integer, Integer, Integer, Integer, Integer, Integer, 
         Integer, Integer, Integer, String> decade = ennead.add("test");
      
      Decade<String, Integer, Integer, Integer, Integer, Integer, 
         Integer, Integer, Integer, Integer> decade1 = ennead.addAt0("test");
      
      System.out.println(decade);
      System.out.println(decade1);
    } }

    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
    

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

    Output

    Verify the Output

    [5, 6, 7, 8, 9, 10, 11, 12, 13]
    [5, 6, 7, 8, 9, 10, 11, 12, 13, test]
    [test, 5, 6, 7, 8, 9, 10, 11, 12, 13]
    
  • Implementing Ennead Using Octet Class

    Problem Description

    How to implement Ennead class using Octet class?

    Example

    Following example shows how to accomplish the above task. Each tuple has add() and addAtX() methods to convert the tuple.

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

    File: TupleTester.java

    package com.tutorialspoint;
    import org.javatuples.Ennead;
    import org.javatuples.Octet;
    public class TupleTester {
       public static void main(String args[]){
    
      Octet<Integer, Integer, Integer, Integer, Integer, Integer,
         Integer, Integer> octet = Octet.with(5,6,7,8,9,10,11,12);
      System.out.println(octet);
      Ennead<Integer, Integer, Integer, Integer, Integer, Integer, 
         Integer, Integer, String> ennead = octet.add("test");
      Ennead<String, Integer, Integer, Integer, Integer, Integer, 
         Integer, Integer, Integer> ennead1 = octet.addAt0("test");
      System.out.println(ennead);
      System.out.println(ennead1);
    } }

    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
    

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

    Output

    Verify the Output

    [5, 6, 7, 8, 9, 10, 11, 12]
    [5, 6, 7, 8, 9, 10, 11, 12, test]
    [test, 5, 6, 7, 8, 9, 10, 11, 12]
    
  • Implementing Octet using Septet Class

    Problem Description

    How to implement Octet class using Septet class?

    Example

    Following example shows how to accomplish the above task. Each tuple has add() and addAtX() methods to convert the tuple.

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

    File: TupleTester.java

    package com.tutorialspoint;
    import org.javatuples.Octet;
    import org.javatuples.Septet;
    public class TupleTester {
       public static void main(String args[]){
    
      Septet<Integer, Integer, Integer, Integer, Integer, Integer,
         Integer> septet = Septet.with(5,6,7,8,9,10,11);
      System.out.println(septet);
      Octet<Integer, Integer, Integer, Integer, Integer, Integer, 
         Integer, String> octet = septet.add("test");
      Octet<String, Integer, Integer, Integer, Integer, Integer, 
         Integer, Integer> octet1 = septet.addAt0("test");
      System.out.println(octet);
      System.out.println(octet1);
    } }

    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
    

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

    Output

    Verify the Output

    [5, 6, 7, 8, 9, 10, 11]
    [5, 6, 7, 8, 9, 10, 11, test]
    [test, 5, 6, 7, 8, 9, 10, 11]
  • Implementing Septet using Sextet Class

    Problem Description

    How to implement Septet class using Sextet class?

    Example

    Following example shows how to accomplish the above task. Each tuple has add() and addAtX() methods to convert the tuple.

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

    File: TupleTester.java

    package com.tutorialspoint;
    import org.javatuples.Septet;
    import org.javatuples.Sextet;
    public class TupleTester {
       public static void main(String args[]){
    
      Sextet<Integer, Integer, Integer, Integer, Integer, Integer> sextet 
         = Sextet.with(5,6,7,8,9,10);
      System.out.println(sextet);
      Septet<Integer, Integer, Integer, Integer, Integer, Integer, String> 
         septet = sextet.add("test");
      Septet<String, Integer, Integer, Integer, Integer, Integer, Integer> 
         septet1 = sextet.addAt0("test");
      System.out.println(septet);
      System.out.println(septet1);
    } }

    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
    

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

    Output

    Verify the Output

    [5, 6, 7, 8, 9, 10]
    [5, 6, 7, 8, 9, 10, test]
    [test, 5, 6, 7, 8, 9, 10]
  • Implementing Sextet Using Quintet Class

    Problem Description

    How to implement Sextet class using Quintet class?

    Example

    Following example shows how to accomplish the above task. Each tuple has add() and addAtX() methods to convert the tuple.

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

    File: TupleTester.java

    package com.tutorialspoint;
    import org.javatuples.Quintet;
    import org.javatuples.Sextet;
    public class TupleTester {
       public static void main(String args[]){
    
      Quintet<Integer, Integer, Integer, Integer, Integer> quintet 
         = Quintet.with(5,6,7,8,9);
      System.out.println(quintet);
      Sextet<Integer, Integer, Integer, Integer, Integer, String> sextet 
         = quintet.add("test");
      Sextet<String, Integer, Integer, Integer, Integer, Integer> sextet1 
         = quintet.addAt0("test");
      System.out.println(sextet);
      System.out.println(sextet1);
    } }

    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
    

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

    Output

    Verify the Output

    [5, 6, 7, 8, 9]
    [5, 6, 7, 8, 9, test]
    [test, 5, 6, 7, 8, 9]
  • Implementing Quintet Using Quartet Class

    Problem Description

    How to implement Quintet class using Quartet class?

    Example

    Following example shows how to accomplish the above task. Each tuple has add() and addAtX() methods to convert the tuple.

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

    File: TupleTester.java

    package com.tutorialspoint;
    import org.javatuples.Quintet;
    import org.javatuples.Quartet;
    public class TupleTester {
       public static void main(String args[]){
    
      Quartet<Integer, Integer, Integer, Integer> quartet = Quartet.with(5,6,7,8);
      System.out.println(quartet);
      Quintet<Integer, Integer, Integer, Integer, String> quintet = quartet.add("test");
      Quintet<String, Integer, Integer, Integer, Integer> quintet1 = quartet.addAt0("test");
      System.out.println(quintet);
      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
    

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

    Output

    Verify the Output

    [5, 6, 7, 8]
    [5, 6, 7, 8, test]
    [test, 5, 6, 7, 8]
  • Implementing Quartet Using Triplet Class

    Problem Description

    How to implement Quartet class using Triplet class?

    Example

    Following example shows how to accomplish the above task. Each tuple has add() and addAtX() methods to convert the tuple.

    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<Integer, Integer, Integer> triplet = Triplet.with(5,6,7);
      System.out.println(triplet);
      Quartet<Integer, Integer, Integer, String> quartet = triplet.add("test");
      Quartet<String, Integer, Integer, Integer> quartet1 = triplet.addAt0("test");
      System.out.println(quartet);
      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
    

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

    Output

    Verify the Output

    [5, 6, 7]
    [5, 6, 7, test]
    [test, 5, 6, 7]
  • Implementing Triplet Using Pair Class

    Problem Description

    How to implement Triplet class using Pair class?

    Example

    Following example shows how to accomplish the above task. Each tuple has add() and addAtX() methods to convert the tuple.

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

    File: TupleTester.java

    package com.tutorialspoint;
    import org.javatuples.Triplet;
    import org.javatuples.Pair;
    public class TupleTester {
       public static void main(String args[]){
    
      Pair<Integer, Integer> pair = Pair.with(5,6);
      System.out.println(pair);
      Triplet<Integer, Integer, String> triplet = pair.add("test");
      Triplet<String, Integer, Integer> triplet1 = pair.addAt0("test");
      System.out.println(triplet);
      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
    

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

    Output

    Verify the Output

    [5, 6]
    [5, 6, test]
    [test, 5, 6]
    
  • Implementing Pair Using Unit Class

    Problem Description

    How to implement Pair class using Unit class?

    Example

    Following example shows how to accomplish the above task. Each tuple has add() and addAtX() methods to convert the tuple.

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

    File: TupleTester.java

    package com.tutorialspoint;
    import org.javatuples.Pair;
    import org.javatuples.Unit;
    public class TupleTester {
       public static void main(String args[]){
    
      Unit<Integer> unit = Unit.with(5);
      System.out.println(unit);
      Pair<Integer, String> pair = unit.add("test");
      Pair<String, Integer> pair1 = unit.addAt0("test");
      System.out.println(pair);
      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
    

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

    Output

    Verify the Output

    [5]
    [5, test]
    [test, 5]
    
  • KeyValue Class

    Introduction

    The org.javatuples.KeyValue class represents a Tuple with two elements with positions 0 and 1 renamed as “key” and “value”, respectively.

    Class Declaration

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

    public final class KeyValue<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
    1KeyValue(A value0, B value1)This creates a KeyValue Tuple.

    Class Methods

    Sr.No.Method & Description
    1static <X> KeyValue<X,X> fromArray(X[] array)Create tuple from array.
    2static <X> KeyValue<X,X> fromCollection(Collection<X> collection)Create tuple from collection.
    3static <X> KeyValue<X,X> fromIterable(Iterable<X> iterable)Create tuple from iterable.
    4static <X> KeyValue<X,X> fromIterable(Iterable<X> iterable, int index)Create tuple from iterable, starting from the specified index.
    5A getKey()Return the key.
    6int getSize()Return the size of the tuple.
    7A getValue()Returns the value of the tuple.
    8<X> KeyValue<X,B> setKey(X key)set the label and return the tuple.
    9<X> KeyValue<A,Y> setValue(Y value)set the value and return the tuple.
    10static <A,B> KeyValue<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 KeyValue 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.KeyValue;
    public class TupleTester {
       public static void main(String args[]){
    
      KeyValue&lt;Integer, Integer&gt; keyValue = KeyValue.with(5,6);
      System.out.println(keyValue);
      List&lt;Integer&gt; list = new ArrayList&lt;&gt;();
      list.add(1);
      list.add(2);
      Integer key = KeyValue.getKey();
      System.out.println(key);
      Integer value = KeyValue.getValue();
      System.out.println(value);
      KeyValue&lt;Integer, Integer&gt; keyValue1 = KeyValue.fromCollection(list);   
      System.out.println(keyValue1);
    } }

    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
    6
    [1, 2]