Open In App

JavaTuples | Introduction

Last Updated : 16 Sep, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

Tuple

The word "tuple" means "a data structure consisting of multiple parts". Hence Tuples can be defined as a data structure that can hold multiple values and these values may/may not be related to each other. Example:
[Geeks, 123, &#*@]
In this example, the values in the tuple are not at all related to each other. "Geeks" is a word that has a meaning. "123" are numbers. While "&#*@" are just some bunch of special characters. Hence the values in a tuple might or might not be related to each other.

JavaTuple

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. JavaTuples offers following classes to work with : Note: To run the JavaTuples program, org.javatuples library needs to be added in the IDE. The IDE can be Netbeans, Eclipse, etc. Download JavaTuples Jar library here. To add a library in Netbeans, refer this. The basic characteristics of JavaTuples are:
  • 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()

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 infinite elements.
Whereas, Tuples :

Similar Reads