JavaTuples | Introduction

Tuple

Example:
[Beginner, 123, &#*@]

JavaTuple

Note:
org.javatuples
Download JavaTuples Jar library here.
refer this.
basic characteristics
  • They are Typesafe
  • They are Immutable
  • They are Iterable
  • They are Serializable
  • They are Comparable (implements Comparable<Tuple>)
  • They implement equals() and hashCode()
  • They also implement toString()

Why to prefer JavaTuples over Lists/Arrays?

  • Can be of a specific type only.
  • Can be of infinite elements.
  • Can be of any type, yet they are typesafe
  • Can be of limited number only, from 1-10
    • Methods in JavaTuples

      JavaTuples library has many functions that help with the easy working of them. These are:
      fromArray() fromCollection() fromIterable() with() addAtX() add()
      compareTo() equals() contains() containsAll() getSize() hashCode()
      toString() toArray() indexOf() lastIndexOf() getValueX() getValue()
      setAtX() removeFromX() getKey() setKey() getLabel() setLabel()
      setValue()

      Creating JavaTuples

      Note: Below examples are shown to be as a generic approach. They can be used for any of the JavaTuple by replacing with the number of parameters and the corresponding Tuple class with values, as required.
      • From Constructor: Syntax:
        NthTuple<type 1, type 2, .., type n> nthTuple = new NthTuple
                        <type 1, type 2, .., type n>(value 1, value 2, .., value n);
        
        Example:
        Pair<Integer, String> pair = new Pair<Integer, String>( 
                                      Integer.valurOf(1), "Beginner");
           
        Sextet<Integer, String, Integer, String, Integer, String> sextet
                                   = new Sextet<Integer, String, Integer, 
                                                 String, Integer, String>(
                                                     Integer.valueOf(1), "Beginner",
                                                     Integer.valueOf(2), "For",
                                                     Integer.valueOf(3), "Beginner"
                                                     );
        
                            
      • Using with() method: Syntax:
        NthTuple<type 1, type 2, .., type n> nthTuple 
            = NthTuple.with(value 1, value 2, .., value n);
        
        Example:
        Pair<Integer, String> pair = Pair.with(Integer.valurOf(1), "Beginner");
          
        Sextet<Integer, String, Integer, String, Integer, String> sextet
            = Sextet.with(Integer.valueOf(1), "Beginner",
                          Integer.valueOf(2), "For",
                          Integer.valueOf(3), "Beginner");
        
                            
      • From other collections: Syntax:
        NthTuple<type, type, .., type> nthTuple 
                 = NthTuple.fromCollection(collectionWith_n_values);
        
        Example:
        Pair<String, String> pair = Pair.fromCollection(collectionWith2Values);
          
        Sextet<Integer, String, Integer, String, Integer, String> sextet
            = Sextet.fromCollection(collectionWith6Values);
        
                            

      Getting JavaTuples values

      • Syntax:
        NthTuple<type 1, type 2, .., type n> nthTuple = new NthTuple
                        <type 1, type 2, .., type n>(value 1, value 2, .., value n);
        typeX valX = nthTuple.getValueX-1();
        
      • Example:
        Pair < Integer, String >
        pair = new Pair<Integer, String>(Integer.valurOf(1), "Beginner");
           
        // This will set val2 = "Beginner"
        String val2 = pair.getValue1();
           
        Sextet<Integer, String, Integer, String, Integer, String> sextet
            = new Sextet<Integer, String, Integer, String, Integer, String>(
                                                Integer.valueOf(1), "Beginner",
                                                Integer.valueOf(2), "For",
                                                Integer.valueOf(3), "Beginner"
                                                );
           
        // This will set val5 = 3
        Integer val5 = sextet.getValue4();
        
                            
        Note:
        • Value numbering starts with index 0. Hence for nth value, the getter will be getValuen-1()
        • NthTuple object can only have getValue0() to getValuen-1() valid getters (e.g. Sextet does not have a getValue6() method).
        • All getValueX() getters are typesafe, i.e. no cast needed. It is because of the type parameterization of the tuple classes (the “” in Triplet) the compiler will know that value0 is of type A, value1 of type B, and so on.
        • There is also a getValue(int pos) method, but its use is not recommended, because the compiler cannot know the type of the result beforehand and therefore a cast will be needed: Triplet<String, Integer, Double> triplet = … … Integer intVal = (Integer) triplet.getValue(1);
      • Classes KeyValue and LabelValue have their getValue0()/getValue1() methods renamed as getKey()/getValue() and getLabel()/getValue(), respectively.

      Setting JavaTuples values

      Since the JavaTuples are immutable, it means that modifying a value at any index is not possible. Hence JavaTuples offer setAtX(value) which creates a copy of the Tuple with a new value at index X, and returns that Tuple. Syntax:
      NthTuple<type 1, type 2, .., type n> nthTuple = new NthTuple
                      <type 1, type 2, .., type n>(value 1, value 2, .., value n);
      nthTuple = nthTuple.setAtX(val);
      
      Example:
      Pair<Integer, String> pair = new Pair<Integer, String>(
                                  Integer.valueOf(1), "Beginner");
         
      pair = pair.setAt1("For"); // This will return a pair (1, "For")
         
      Sextet<Integer, String, Integer, String, Integer, String> sextet
          = new Sextet<Integer, String, Integer, String, Integer, String>(
                                              Integer.valueOf(1), "Beginner",
                                              Integer.valueOf(2), "For",
                                              Integer.valueOf(3), "Beginner"
                                              );
         
      // This will return sextet (1, "Beginner", 2, "For", 3, "Everyone")
      Sextet<Integer, String, Integer, String, Integer, String> otherSextet
          = sextet.setAt5("Everyone");
      
                          

      Iterating JavaTuple values

      All tuple classes in javatuples implement the Iterable<Object> interface. It means that they can be iterated in the same way as collections or arrays. Example:
      Triplet<String, Integer, Double> triplet = ...... 
        
      for (Object value : tuple)
      {
          ...
      }
      
                          



Contact Us