JavaTuples - Overview



Tuple

Tuple is a sequence of objects which may or may not be of same type. Consider the following example −

[12,"TutorialsPoint", java.sql.Connection@li757b]

Above object is a tuple of three elements, an Integer, a string and a Connection Object.

JavaTuple

JavaTuples is a very simple library which offers ten different tuple classses which are sufficient to handle most of the tuple related requirements.

  • Unit<A> - 1 element

  • Pair<A,B> - 2 elements

  • Triplet<A,B,C> - 3 elements

  • Quartet<A,B,C,D> - 4 elements

  • Quintet<A,B,C,D,E> - 5 elements

  • Sextet<A,B,C,D,E,F> - 6 elements

  • Septet<A,B,C,D,E,F,G> - 7 elements

  • Octet<A,B,C,D,E,F,G,H> - 8 elements

  • Ennead<A,B,C,D,E,F,G,H,I> - 9 elements

  • Decade<A,B,C,D,E,F,G,H,I,J> - 10 elements

Apart from these tuple classes, JavaTuples also provides two additional classes for semantics sake.

  • KeyValue<A,B>

  • LabelValue<A,B>

All tuple classes are typesafe and immutable and implements following interfaces and methods.

  • Iterable

  • Serializable

  • Comparable<Tuple>

  • equals()

  • hashCode()

  • toString()

Tuple vs List/Array

List or Array can contain any number of elements but each element must be of same type whereas tuples can contain only specific number of elements, can have different type of elements but still are typesafe.

Advertisements