top of page
khushalkhoyani1998

Java Tuple

A tuple is just a sequence of objects that do not necessarily relate to each other in any way. For example: [23, "khushal", java.sql.Connection@li734s] can be considered a tuple of three elements (a triplet) containing an Integer, a String, and a JDBC Connection object.A tuple is a generic data structure that treats each element as an object, and these objects stores in a separate byte array. In other words, we can also say that tuple is an ordered collection of objects of different types.


Javatuples offers you tuple classes from one to ten elements:

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


All tuple classes are:

  • Typesafe

  • Immutable

  • Iterable

  • Serializable

  • Comparable (implements Comparable<Tuple>)

  • Implementing equals(...) and hashCode()

  • Implementing toString()


Tuple Vs List


Tuple

List

It is a set of comma-separated values that are enclosed in parenthesis.

It is a set of comma-separated values that are enclosed in square brackets.

It is immutable.

It is mutable.

It requires less memory.

It requires more memory.

It is suitable for large amounts of data.

It is suitable for a small amount of data.

It can be stored in a list.

It can be stored inside a tuple.

It is faster in comparison to List.

It is slower in comparison to the tuple.

It has a fixed length.

It has variable lengths.

It has fewer factory methods.

It has more factory methods.


20 views0 comments

Recent Posts

See All

Comments


bottom of page