Sunday, December 29, 2024
Google search engine
HomeLanguagesJavaDecade Class in JavaTuples

Decade Class in JavaTuples

A Decade is a Tuple from JavaTuples library that deals with 3 elements. Since this Decade is a generic class, it can hold any type of value in it.
Since Decade is a Tuple, hence it also has all the characteristics of JavaTuples: 
 

  • 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()

 

Class Declaration

 

public final class Decade<A, B, C, D, E, F, G, H, I, J>
extends Tuple
implements IValue0<A>, IValue1<B>, IValue2<C>, IValue3<D>, IValue4<E>, 
           IValue5<F, IValue6<G, IValue7<H>, IValue8<I, J>

 

Class hierarchy

 

Object
  ↳ org.javatuples.Tuple
      ↳ org.javatuples.Decade<A, B, C, D, E, F, G, H, I, J>

Creating Decade Tuple
 

  • From Constructor:
    Syntax
     
Decade<A, B, C, D, E, F, G, H, I, J> decade = 
    new Decade<A, B, C, D, E, F, G, H, I, J>
        (value1, value2, value3, value4, value5, value6, value7, value8, value9, value10);
  • Example
     

Java




// Below is a Java program to create
// a Decade tuple from Constructor
 
import java.util.*;
import org.javatuples.Decade;
 
class GfG {
    public static void main(String[] args)
    {
        Decade<Integer, Integer, Integer, Integer, Integer, Integer, Integer,
                                            Integer, Integer, Integer> decade
            = Decade.with(Integer.valueOf(1),
                        Integer.valueOf(2),
                        Integer.valueOf(3),
                        Integer.valueOf(4),
                        Integer.valueOf(5),
                        Integer.valueOf(6),
                        Integer.valueOf(7),
                        Integer.valueOf(8),
                        Integer.valueOf(9),
                        Integer.valueOf(10));
 
        System.out.println(decade);
    }
}


  • Output: 
     
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  •  
  • Using with() method: The with() method is a function provided by the JavaTuples library, to instantiate the object with such values.
    Syntax
     
Decade<type1, type2, type3, type4, type5, type6, type7, type8, type9> decade = 
    Decade.with(value1, value2, value3, value4, value5, value6, value7, value8, value9, value10);
  • Example
     

Java




// Below is a Java program to create
// a Decade tuple from with() method
 
import java.util.*;
import org.javatuples.Decade;
 
class GfG {
    public static void main(String[] args)
    {
        Decade<Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer,
                                                               Integer, Integer> decade
            = Decade.with(Integer.valueOf(1),
                          Integer.valueOf(2),
                          Integer.valueOf(3),
                          Integer.valueOf(4),
                          Integer.valueOf(5),
                          Integer.valueOf(6),
                          Integer.valueOf(7),
                          Integer.valueOf(8),
                          Integer.valueOf(9),
                          Integer.valueOf(10));
 
        System.out.println(decade);
    }
}


  • Output: 
     
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  •  
  • From other collections: The fromCollection() method is used to create a Tuple from a collection, and fromArray() method is used to create from an array. The collection/array must have the same type as of the Tuple and the number of values in the collection/array must match the Tuple class.
    Syntax
     
Decade<type1, type2, type3, type4, type5, type6, type7, type8, type9> decade = 
    Decade.fromCollection(collectionWith_10_value);

Decade<type1, type2, type3, type4, type5, type6, type7, type8, type9> decade = 
    Decade.fromArray(arrayWith_10_value);
  • Example
     

Java




// Below is a Java program to create
// a Decade tuple from Collection
 
import java.util.*;
import org.javatuples.Decade;
 
class GfG {
    public static void main(String[] args)
    {
        // Creating Decade from List
        List<Integer> list = new ArrayList<Integer>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
        list.add(6);
        list.add(7);
        list.add(8);
        list.add(9);
        list.add(10);
 
        Decade<Integer, Integer, Integer, Integer, Integer, Integer, Integer,
                                              Integer, Integer, Integer> decade
            = Decade.fromCollection(list);
 
        // Creating Decade from Array
        Integer[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
 
        Decade<Integer, Integer, Integer, Integer, Integer, Integer, Integer,
                                           Integer, Integer, Integer> otherDecade
            = Decade.fromArray(arr);
 
        System.out.println(decade);
        System.out.println(otherDecade);
    }
}


  • Output: 
     
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  •  

 

Getting value

The getValueX() method can be used to fetch the value in a Tuple at index X. The indexing in Tuples starts with 0. Hence the value at index X represents the value at position X+1.
Syntax
 

Decade<type1, type2, type3, type4, type5, type6, type7, type8, type9> decade = 
    new Decade<type1, type2, type3, type4, type5, type6, type7, type8, type9>
        (value1, value2, value3, value4, value5, value6, value7, value8, value9, value10);

type1 val1 = decade.getValue0();

Example
 

Java




// Below is a Java program to get
// a Decade value
 
import java.util.*;
import org.javatuples.Decade;
 
class GfG {
    public static void main(String[] args)
    {
        Decade<Integer, Integer, Integer, Integer, Integer, Integer, Integer,
                                               Integer, Integer, Integer> decade
            = Decade.with(Integer.valueOf(1),
                          Integer.valueOf(2),
                          Integer.valueOf(3),
                          Integer.valueOf(4),
                          Integer.valueOf(5),
                          Integer.valueOf(6),
                          Integer.valueOf(7),
                          Integer.valueOf(8),
                          Integer.valueOf(9),
                          Integer.valueOf(10));
 
        System.out.println(decade.getValue0());
        System.out.println(decade.getValue2());
    }
}


Output: 
 

1
3

 

Setting Decade value

Since the Tuples 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
 

Decade<type1, type2, type3, type4, type5, type6, type7, type8, type9> decade = 
    new Decade<type1, type2, type3, type4, type5, type6, type7, type8, type9>
                (value1, value2, value3, value4, value5, value6, value7, value8, value9, value10);

Decade<type1, type2, type3, type4, type5, type6, type7, type8, type9> 
    otherDecade = decade.setAtX(value);

Example
 

Java




// Below is a Java program to set
// a Decade value
 
import java.util.*;
import org.javatuples.Decade;
 
class GfG {
    public static void main(String[] args)
    {
        Decade<Integer, Integer, Integer, Integer, Integer, Integer, Integer,
                                               Integer, Integer, Integer> decade
            = Decade.with(Integer.valueOf(1),
                          Integer.valueOf(2),
                          Integer.valueOf(3),
                          Integer.valueOf(4),
                          Integer.valueOf(5),
                          Integer.valueOf(6),
                          Integer.valueOf(7),
                          Integer.valueOf(8),
                          Integer.valueOf(9),
                          Integer.valueOf(10));
 
        Decade<Integer, Integer, Integer, Integer, Integer, Integer, Integer,
                                            Integer, Integer, Integer> otherDecade
            = decade.setAt3(40);
 
        System.out.println(otherDecade);
    }
}


Output: 
 

[1, 2, 3, 40, 5, 6, 7, 8, 9, 10]

 

Adding a value

JavaTuples do not support Tuple with more than 10 values. Hence there is no function to add a value in Decade.
 

Searching in Decade

An element can be searched in a tuple with the pre-defined method contains(). It returns a boolean value whether the value is present or not.
Syntax
 

Decade<type1, type2, type3, type4, type5, type6, type7, type8, type9> decade = 
    new Decade<type1, type2, type3, type4, type5, type6, type7, type8, type9>
        (value1, value2, value3, value4, value5, value6, value7, value8, value9, value10);

boolean res = decade.contains(value2);

Example
 

Java




// Below is a Java program to search
// a value in a Decade
 
import java.util.*;
import org.javatuples.Decade;
 
class GfG {
    public static void main(String[] args)
    {
        Decade<Integer, Integer, Integer, Integer, Integer, Integer, Integer,
                                             Integer, Integer, Integer> decade
            = Decade.with(Integer.valueOf(1),
                          Integer.valueOf(2),
                          Integer.valueOf(3),
                          Integer.valueOf(4),
                          Integer.valueOf(5),
                          Integer.valueOf(6),
                          Integer.valueOf(7),
                          Integer.valueOf(8),
                          Integer.valueOf(9),
                          Integer.valueOf(10));
 
        boolean exist = decade.contains(5);
        boolean exist1 = decade.contains(false);
 
        System.out.println(exist);
        System.out.println(exist1);
    }
}


Output: 
 

true
false

 

Iterating through Decade

Since Decade implement the Iterable<Object> interface. It means that they can be iterated in the same way as collections or arrays.
Syntax
 

Decade<type1, type2, type3, type4, type5, type6, type7, type8, type9> decade = 
    new Decade<type1, type2, type3, type4, type5, type6, type7, type8, type9>
            (value1, value2, value3, value4, value5, value6, value7, value8, value9, value10);

for (Object item : decade) {
        ...
}

Example
 

Java




// Below is a Java program to iterate
// a Decade
 
import java.util.*;
import org.javatuples.Decade;
 
class GfG {
    public static void main(String[] args)
    {
        Decade<Integer, Integer, Integer, Integer, Integer, Integer, Integer,
                                               Integer, Integer, Integer> decade
            = Decade.with(Integer.valueOf(1),
                          Integer.valueOf(2),
                          Integer.valueOf(3),
                          Integer.valueOf(4),
                          Integer.valueOf(5),
                          Integer.valueOf(6),
                          Integer.valueOf(7),
                          Integer.valueOf(8),
                          Integer.valueOf(9),
                          Integer.valueOf(10));
 
        for (Object item : decade)
            System.out.println(item);
    }
}


Output: 
 

1
2
3
4
5
6
7
8
9
10

 

RELATED ARTICLES

Most Popular

Recent Comments