A tuple is a data structure that can hold objects of different types. These objects are not connected to each other but have meaning when we consider them collectively. In this section, we discuss what is tuple, features, size, and operations of tuples. Also, we will discuss the tuple implementation in Java.
What is a tuple?
In general, a tuple is an ordered collection of objects. In tuple data is stored as an object in a separate byte array. It has comma-separated values enclosed in a pair of square brackets . Tuples are immutable, unlike Lists data structure. A tuple can hold multiple tuples. It can also be considered as an anonymous object.
Features of Tuple
Tuple has the following features:
Let's consider the following example.
The above tuple is a quartet tuple because it has four elements (objects). We observe that each object is of a different type. But when we consider it collectively, it has a specific meaning. The above tuple represents the data of an employee such as name, gender, age, and designation.
Let's see some other examples of tuples.
Tuple in Java
In Java, 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.
The functionality of a tuple can be implemented using the List and Array data structure but these data structures do not hold different types of data types by design. Hence, it is clear that heterogeneous tuple using a standard data structure (List/ Array) is not possible in Java. Since we required tuple data structure to fulfill the requirement of holding homogeneous data structure.
Note that tuple data structure is not present in Java programming, by default. But we can implement the tuple data structure by using the third-party library named javatuples.
Before moving to the implementation, first, we will download the javatuples.jar file. And add this file to the path of the project.
We can also use the following dependency in pom.xml file to implement the tuples data structure in Java.
Let's implement a tuple and create a simple Java tuple program.
The javatuples library has the tuple classes that are corresponding to the size of a tuple. Tuples may be different in size. A tuple may hold a maximum of 10 elements. The implementation of each tuple is different. The class hierarchy is as follows.
Java Tuple Class
The Tuple is an abstract base class for all the tuple classes that belongs to org.javatuples package. All the methods of the tuple class are public and final. The following table summarizes the methods of the tuple class. It implements Iterable and Serializable interfaces.
Direct Known Subclasses
Besides the above classes, there are two additional classes provided by javatuples library i.e. KeyValue<A, B> and LabelValue<A, B>. These two classes are similar to the Pair class and provide the same functionality but in different semantics.
Each tuple class implements the following three interfaces:
Implementation of Tuple
The implementation of a tuple in Java is very easy. We have to create an instance of tuple class that corresponds to size.
The details of the employee are: [Sophia, Female, 22, Marketing Manager]
The following operations can be performed on a tuple:
There are three ways to create a tuple:
Let's see the above three ways to create a tuple.
By Using the with() Method
The javatuples library provides the with() method that creates a tuple with the specified values. The method belongs to the org.javatuples.Pair package. It is used to instantiate objects with values.
The above Pair class object creates a tuple with two values. Let's create a Java program for the same.
[9086651, Dell Laptop]
By Using Constructor
In this case, we create a constructor of the class, according to requirement.
Let's create a Java program to create a tuple using constructor.
[91237, Mac Book Air, 88490.0, 8-Core CPU, 4]
By Using Collection
The javatuples library allows us to create a tuple from the collection by using the fromCollection() method. It also allows us to create a tuple from an array by using the fromArray() method. Note that the collection/ array must have the same type and values as the tuple.
The collection/array must have the same type as the Tuple and the number of values in the collection/ array must match the Tuple class.
[C, C++, Java, Python, Scala, Ruby, PHP, COBOL] [One, Two, Three, Four, Five, Six]
The javatuples library also allows us to fetch values from the tuple at the specified index by using the getValueX() method. Where X denotes the index value of the object. Indexing starts from 0.
As we discussed above tuples are immutable. Hence, they cannot be modified once they are created. To overcome the problem, javatuples library provides the setValueX() method. Where X is the index value at which we want to set the specific value. The method creates a copy of the tuple with the newly added value at the specified index and returns the same tuple.
Adding a Value
There are two ways to add values in a tuple:
At the end of the Tuple
The javatuples library provides the add() method to add objects to the tuple. It adds the object at the end of the tuple and returns a new tuple by matching the number of elements.
Suppose, we have a tuple having two elements and we want to add another element to the tuple. In such a case, the Pair tuple will not support the third element. Therefore, when we add an element to a Pair tuple, it gets converted into a Triplet tuple. Let's see an example.
[Jack, 46] [Jack, 46, Finance Professional]
We can also add one tuple to another tuple. It increases the number of elements in the newly generated tuple. Hence, it returns the type of tuple based on the number of elements present after addition.
[Mango, Grapes, Papaya] [Mango, Banana, Grapes, Papaya] [Mango, Banana, Grapes, Papaya, Mango, Grapes, Papaya]
At Specified Index
By default, new elements are added at the end of the tuple. But we can add elements at the specified index by using the addX() method.
[MCA, M.Sc., MBBS] [MCA, M.Sc., M.Tech, MBBS]
Searching an Element
We can also search for an element that resides in the tuple. For searching javatuples library provides the contains() method of the Tuple class. It returns a Boolean value true if an element is present, else returns false. Let's see an example.
Convert Tuple to Collection or Array
Each tuple class has asList() and toArray() methods that returns List and Array, respectively. Let's see an example.
[Dog, 12, German Shepherd, 23.89] [Dog, 12, German Shepherd, 23.89]
Note that tuple can contain heterogeneous types so the resulting type will be of List<Object> or Object accordingly.
Iteration Over Tuple
All the tuple classes implement the Iterable interface. So, we can iterate a tuple in the same way as collections or arrays.
Dell 5600.0 34 Digital Solutions
Tuple Vs. List/Array