Java

Java Tuple

A tuple in Java is defined as a type of data structure that contains many different values, some of which may or may not be linked to one another. The Java tuple is an immutable and organized sequence of values. Note that Java programming does not, by default, support the tuple data format. The third-party package “JavaTuple” is required to build the tuple data in Java. The “JavaTuple” package must be downloaded before the tuple implementation. It provides the working of tuple classes, functions, and data structure. The following are the example programs of the Java tuple methods which are supported by the “JavaTuple” library.

Example 1:

The first operation of the Java tuple is to create it. The with() method is required from the JavaTuple library which creates the Java tuple with the given values:

We import the “org.javatuples.Pair” package which is required to create the tuple in Java. After that, we define a Java class “CreatingTuple” and implement a main() method inside it. The main() method is declared with the pair class object, “MyPairVal”. The “MyPairVal ” object creates the tuple which has one integer value and one string value. We employ the with() method to insert the values in the specified tuple. After the creation of the tuple, we print the tuple with the print class of Java.

The Java tuple is created which is compiled by the Java and it is displayed on the console screen.

Example 2:

The values from the tuple at any given index can also be fetched in Java using the getValue and getValueX() methods.

We include the import statement in the previous program which allows us to get the values from the Java tuple. Next, we have a creation of the Java class “GettingTuple” where the main() method block is established. Here, we use the Java tuple class “triplet” which is used to store three elements in the tuple object, “MyTuple”. The triplet class first stores the string value, then the integer value, and lastly, the double value because the data types are mentioned by this class. We use the with() method to store the elements in the tuple according to the specified data types.

After that, we deploy the getValue() method by declaring the “val1” object. We perform the casting on the getValue() method because it is not type-safe. The getValue() method takes the index value “1” and returns the integer value which is placed at this index. Next, we declare another object, “val2, where the getValueX() method is deployed. The X value is replaced with the index position “1” which means that this method also fetches the value of index position “1”. The results from both methods are printed on the output screen.

The getValue() and the getValueX() methods fetch the value from the tuple with the specified indexes.

Example 3:

As we mentioned, Java tuples are immutable. But we can modify the value within a tuple with the setAtX() method. The X can be any index value in the tuple which needs to be altered.

We construct the main() method within the Java class, “SettingTuple”. Then, inside the main() method brackets, we use the pair class to store the integer elements in the tuple object, “Set1”. The with() method is employed to insert the two integer elements inside the tuple. After that, we define another pair class object “set2” to set the tuple “set1”. For this, we employ the setAtX() method which modifies the specified argument value at index “1” of the tuple “set1” by creating a copy of the tuple “set1”. Then, we display both tuples, one with the previous value and the second tuple with the newly set value at a given position.

The immutable tuple “set1” is modified with the new value at the index position “1” as follows. The setAtX() method creates another copy of the tuple and inserts the new value there:

Example 4:

Every tuple class complies with the Iterable interface. Hence, the iteration operation can be performed on the Java tuple.

We build a Java class “IterateTuple” where the main() method is assigned. We create the object “StringTuple” of the class pair inside the main() method section. Then, we add a paired element of a string with the help of the with() method inside the pair tuple. After creating the tuple, we perform the foreach-loop operation on the specified tuple for iteration on each of the values inside the tuple. The “StringTuple” tuple object assigns a pair of the tuple to the object “obj”. The foreach loop cycles over the paired tuple and displays the iterated tuple on the screen.

The iterated elements from the specified tuple are displayed on the following screen of the terminal:

Example 5:

The elements of the Java tuple can also be searched via contains() method of the tuple class. The Boolean results are obtained from the contains() method.

We created the “SearchingTuple” class in the previous Java program and called out the main() method in that class. After that, we implement the tuple class “Unit” which stores only one element in the tuple. The “Str” object is declared for the Java tuple class “Unit” and store the single string element in the Unit class. Next, we declare the Boolean primitive type of “Result” object and then the string data type, “find” object. The “find” object is initialized with the string.

The contains() method of the Java tuple is used to find the specified string from the Unit tuple and display the results in Boolean form. Then, we find another character of string which is searched by the contains() method from the given unit tuple. The results of the contains() method are obtained in the form of Boolean values.

As both the specified strings are not part of the unit tuple, the contains() method provides the Boolean value which is false for both of the specified strings in the following output image:

Example 6:

The transformation of Java tuples to array and list can also be accomplished from the toArray() and toList() methods.

We implement the Java class “TupleToArrayList” where we have the main() method definition for tuple conversion. We use the “Quarter” class provided by the Java tuple. This class stores only four elements. To insert the element in the “Quarter” tuple, we define the “TupleIs” object of the tuple class and assign four string elements inside it. Then, we create the “TupleArray” object which is initialized with the toArray() method to transform the quartet tuple into the array.

Furthermore, we transform the Quartet tuple to the list by declaring the “TupleList” object for the list class and deploy the toList() method there. The print method prints the tuple and the transformed tuple to an array and list.

The output screen shows the tuple which is created with the Quartet class of Java tuple. Then, the tuple is represented in the array and the list:

Conclusion

Java tuple is the fundamental part of data collection. They are used to maintain the record with different types of data fields. The JavaTuple library enables us to work with the Java tuples where different classes are available. We have taken a look at this library with the example programs. We also tried to explore all the possible classes of the tuple class and the method associated with it.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.