What are tuples?
A tuple is an ordered collection of different types of objects that may or may not relate with each other. It can also be seen as a set, in which the values don't relate with each other but together, they make sense in an application. For example: ["James Doe", "IT Professional", 28]
In this example, it is clear that this data is related to a specific employee, that consist his name, profession and age.
In java, to store objects, we have ArrayList, HashMap, Set etc. But when it comes to a situation when we want to store data in the form of tuple, we use java built-in Javatuples classes. Javatuples offers classes to hold upto 10 elements:
In addition with the above classes, KeyValue<A,B> and LabelValue<A,B> also provide functionalites similar to Pair<A,B> for code semantics.
All tuple classes implements the Iterable, Comparable and Serializable interfaces and each of them are immutable and typesafe.
Tuples Common Operations
1. Creating a tuple
Another way is,
2. Retrieve values from a tuple
To retrieve values, getValueX() method is used, where X is to be replace with the position of element inside the tuple.
3. Add elements in tuple
4. Remove elements from tuple
5. Iteration through tuple