JavaTuples allows maximum of 10 tuples.
The classes for each are:
For 1 element – Unit
For 3 elements – Triplet<A, B, C>
For 4 elements – Quartet<A, B, C, D>
For 5 elements – Quintet<A, B, C, D, E>
For 6 elements – Sextet<A, B, C, D, E, F>
For 7 elements – Septet<A, B, C, D, E, F, G>
For 8 elements – Octet<A, B, C, D, E, F, G, H>
KeyValue<A, B>
LabelValue<A, B>
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()
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
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
There are a number of methods which can be used to create Tuples listed below:
3. org.javatuples.Unit.fromCollection(Collection) : Create tuple from a collection. Collection has to have exactly one element.
There are many other methods in the class.
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.
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.
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);
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);
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);
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);
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);
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);
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);
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());
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.