Today we are going to talk about the Basic of Java Tuple Class Library Implementation and Example.
What do you mean by Tuple?
Tuple in simple computer science language as a data structure consisting of multiple parts i.e it is a data structure which can hold multiple values which can be/cannot be related to each other. Tuple is a an abstract class which implements Iterable, Serializable, Comparable interface. Example: tuple = [ABC, 123, @%^&] Here we see that ABC represents some string, 123 is a number and @%^& are special characters.
JavaTuples is a Java library that offers classes, functions and data structures to work with tuples. It is one of the simplest java library ever made.This is an external library which needs to be added externally to use it. The package it provides are org.tuples with all the classes and interfaces.

JavaTuples also gives the 2 very common 2-element tuple classes equivalent to Pair:

KeyValue<A, B>

LabelValue<A, B>

 

The basic characterstics of JavaTuples are:

They are Typesafe

They are Immutable

They are Iterable

They are Serializable

They are Comparable (implements Comparable)

They implement equals() and hashCode()

They also implement toString()

Why to prefer JavaTuples over Lists/Arrays?
Think of a scenario where you want to store the details of a student in just one entity, like Name, Roll Number,
Father’s Name, Contact Number. Now the most common approach that strikes the mind is to construct a data structure that would take the fields as required. This is where Tuples come into play. With Tuples, any separate data structure need not to be created. Instead, for this scenario, a Quartet<A, B, C, D> can be simply used.

Therefore, common data structures like List, Array :

-> Can be of a specific type only.
-> Can be of infinte elements.
Whereas, Tuples :

-> Can be of any type, yet they are typesafe
-> Can be of limited number only, from 1-10

Java Tuples
Java Tuples are immutable so modifying tuples are not possible if doing so then it will create a copy of tuple with a new value and return the tuple.
Unit Tuple:

Unit is a final Tuple class which can contain only one element of any type.

Class Declaration:

public final class Unit extends Tuple implements IValue0 

Unit Creation:

Unit unit = new Unit(“Java”);

There are a number of methods which can be used to create Tuples listed below:

1. org.javatuples.Unit.with(A) : this is a static method that returns a new instance of Unit with A passed in with method.

2. org.javatuples.Unit.fromArray(X[]) : this is a static method that is used to create Unit from an array.

3. org.javatuples.Unit.fromCollection(Collection) : Create tuple from a collection. Collection has to have exactly one element.

4. org.javatuples.Unit.fromIterable(Iterable) : Create tuple from iterable. Iterable has to have exactly one element.

5. org.javatuples.Unit.fromIterable(Iterable, int): Create tuple from iterable, starting from the specified index. Iterable can have more (or less) elements than the tuple to be created.

There are many other methods in the class.

Pair Tuple:
Pair is a final Tuple class which can contain only two element of any type.
Class Declaration:

public final class Pair<A, B> extends Tuple
implements IValue0<A>, IValue1<B>
Pair Creation:

Pair<String, Integer> pair = new Pair<String, Integer>(“java”, 2018);

There are a number of methods which can be used to create Tuples listed below:

1.org.javatuples.Pair.fromArray(X[]) : Create tuple from array. Array has to have exactly two elements.
2.org.javatuples.Pair.fromCollection(Collection<X>) : Create tuple from collection. Collection has to have exactly two elements.
3.org.javatuples.Pair.fromIterable(Iterable<X>) : Create tuple from iterable. Iterable has to have exactly two elements.
4.org.javatuples.Pair.fromIterable(Iterable<X>, int) : Create tuple from iterable, starting from the specified index.
Iterable can have more (or less) elements than the tuple to be created.
5. org.javatuples.Pair.with(A, B) : this is static method that return new instance of Pair with A and B passed in with method.

There are many other method in the class.

Triplet Tuple:
Triplet is a final Tuple class which can contain only three elements of any type.

Class Declaration:
public final class Triplet<A,B,C>
extends Tuple
implements IValue0<A>,
IValue1<B>,
IValue2<C> {

Triplet Creation:

Triplet<String, String, Integer> triplet = new Triplet<String, String, Integer>(“java”, “java10”, 2018);

There are a number of methods which can be used to create Tuples listed below:

1.org.javatuples.Triplet.fromArray(X[]) : Create tuple from array. Array has to have exactly three elements.
2.org.javatuples.Triplet.fromCollection(Collection<X>) : Create tuple from collection. Collection has to have exactly three elements.
3.org.javatuples.Triplet.fromIterable(Iterable<X>) : Create tuple from iterable. Iterable has to have exactly three elements.
4.org.javatuples.Triplet.fromIterable(Iterable<X>, int) : Create tuple from iterable, starting from the specified index.
Iterable can have more (or less) elements than the tuple to be created.
5. org.javatuples.Triplet.with(A, B, C) : this is static method that return new instance of Tripet with A and B and C passed in with method.

There are many other method in the class.

Quartet Tuple:
Quartet is a final Tuple class which can contain only four element of any type.

Class Declaration:
public final class Quartet<A,B,C,D>
extends Tuple
implements IValue0<A>,
IValue1<B>,
IValue2<C>,
IValue3<D>

Quartet Creation:

Quartet<String, String, String, Integer> quartlet = new Quartet<String, String, String, Integer>(“Java”,
“java10”, “enhancement”, 2018);

Quintet Tuple:
Quintet is a final Tuple class which can contain only five element of any type.

Class Declaration:
public final class Quintet<A,B,C,D,E>
extends Tuple
implements IValue0<A>,
IValue1<B>,
IValue2<C>,
IValue3<D>,
IValue4<E>

Quintet Creation:

Quintet<String, String, String, String, Integer> quintet = new Quintet<String, String, String, String, Integer>(
“Java”, “java10”, “blog”, “amitclive”, 2018);

Sextet Tuple:
Sextet is a final Tuple class which can contain only six element of any type.

Class Declaration:
public final class Sextet<A,B,C,D,E,F>
extends Tuple
implements IValue0<A>,
IValue1<B>,
IValue2<C>,
IValue3<D>,
IValue4<E>,
IValue5<F>

Sextet Creation:

Sextet<String, String, String, String, String, Integer> sextet = new Sextet<String, String, String, String, String, Integer>(
“Java”, “java10”, “blog”, “amitclive”, “github”, 2018);

Septet Tuple:
Septet is a final Tuple class which can contain only seven element of any type.

Class Declaration:
public final class Septet<A,B,C,D,E,F,G>
extends Tuple
implements IValue0<A>,
IValue1<B>,
IValue2<C>,
IValue3<D>,
IValue4<E>,
IValue5<F>,
IValue6<G>

Septet Creation:

Septet<String, String, String, String, String, String, Integer> septet = new Septet<String, String, String, String, String, String, Integer>(
“Java”, “java10”, “blog”, “amitclive”, “github”, “linkedin”, 2018);

Octet Tuple:
Octet is a final Tuple class which can contain only eight elements of any type.

Class Declaration:
public final class Octet<A,B,C,D,E,F,G,H>
extends Tuple
implements IValue0<A>,
IValue1<B>,
IValue2<C>,
IValue3<D>,
IValue4<E>,
IValue5<F>,
IValue6<G>,
IValue7<H>

Octet Creation:

Octet<String, String, String, String, String, String, String, Integer> octet = new Octet<String, String, String, String, String, String, String, Integer>(
“Java”, “java10”, “blog”, “amitclive”, “github”, “linkedin”, “competitive programming”, 2018);

Ennead Tuple:
Ennead is a final Tuple class which can contain only nine element of any type.

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

Ennead Creation:

Ennead<String, String, String, String, String, String, String, String, Integer> ennead = new Ennead<String, String, String, String, String, String, String, String, Integer>(
“Java”, “java10”, “blog”, “amitclive”, “github”, “linkedin”, “competitive programming”, “algorithms”,
2018);

Decade Tuple:
Decade is a final Tuple class which can contain only ten element of any type.

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>,
IValue9<J>

Decade Creation:

Decade<String, String, String, String, String, String, String, String, String, Integer> decade = new Decade<String, String, String, String, String, String, String, String, String, Integer>(
“Java”, “java10”, “blog”, “amitclive”, “github”, “linkedin”, “competitive programming”, “algorithms”,
“datastructure”, 2018);

KeyValue Tuple:
KeyValue is a final Tuple class two elements, with positions 0 and 1 renamed as “key” and “value”, respectively.

Class Declaration:
public final class KeyValue<A,B>
extends Tuple
implements IValueKey<A>,
IValueValue<B>

KeyValue Creation:

KeyValue<String, Integer> keyVal =new KeyValue<String, Integer>(“blog”, 2018);

System.out.println(keyVal.getKey());
System.out.println(keyVal.getValue());

LabelValue Tuple:
LabelValue is a final Tuple class two elements, with positions 0 and 1 renamed as “label” and “value”, respectively.

Class Declaration:
public final class LabelValue<A,B>
extends Tuple
implements IValueLabel<A>,
IValueValue<B>

LabelValue Creation:

LabelValue<String, Integer> labVal =new LabelValue<String, Integer>(“java”, 1995);
System.out.println(labVal.getLabel());
System.out.println(labVal.getValue());

 

 

We will talk about this in more detail in some other article, For the Basic understanding purpose, it will help you to start and make your Java Coding experience better.

Stay tuned to our blog for more such developer stuff.

Amit is a software developer by profession. He is passionate about all the technologies which can be used to build an application like java related technologies and many more and loves to implement it his career or for knowledge purpose and share his experience with you all. To know more see the About me profile.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.