What is Javatuple

Posted By :Rozi Ali |31st March 2020

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.

Javatuples

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:

  • Unit<A> (One element)
  • Pair<A,B> (Two element)
  • Triplet<A,B,C> (Three element)
  • Quartet<A,B,C,D> (Four element)
  • Quintet<A,B,C,D,E> (Five element)
  • Sextet<A,B,C,D,E,F> (Six element)
  • Septet<A,B,C,D,E,F,G> (Seven element)
  • Octet<A,B,C,D,E,F,G,H> (Eight element)
  • Ennead<A,B,C,D,E,F,G,H,I> (Nine element)
  • Decade<A,B,C,D,E,F,G,H,I,J> (Ten element)

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

Pair<String, String> pair = new Pair<String, String>("John Doe", "Manager");

Another way is,

Pair<String, String> Pair.with("John Doe", "Manager");

 

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.

Pair<String, String> pair = new Pair<String, String>("John Doe", "Manager")

System.out.println("Employee Name: "+ pair.getValue0());
System.out.println("Employee Designation: "+ pair.getValue1());

 

3. Add elements in tuple

Pair<String, String> pair = new Pair<String, String>("John Doe", "Manager");
Triplet<String, String, Integer> triplet = pair.add("30"); //["John Doe", "Manager", 30]

 

4. Remove elements from tuple

Pair<String, String> pair = new Pair<String, String>("John Doe", "Manager");
Unit<String> unit = pair.removeFrom0();

 

5. Iteration through tuple

Quartet<String, Integer, Double, String> quartet = Quartet.with("A", 1, 0.1, "B");

for(Object  obj : quartet){
     System.out.println(obj);
}

Output:
A
1
0.1
B

About Author

Rozi Ali

Rozi Ali is an accomplished software developer with extensive experience in the field of JAVA. She possesses a solid grasp of programming languages such as Java/Spring-boot, Python, and Typescript/Nodejs/GraphQL. Rozi has a strong background in Object-oriented programming (OOP) and is skilled in working with both relational databases like MySql, PostgreSQL and non-relational databases like MongoDb. She is proficient in REST APIs, Microservices, and code deployment, along with the development tools such as Jira, Git, and Bash. Additionally, Rozi has experience working with Cloud providers such as AWS and Azure. She has contributed significantly to a number of projects, including Konfer, VNS, Influsoft, VN Platform, QuickDialog, and Oodles-Dashboard.

Request For Proposal

[contact-form-7 404 "Not Found"]

Ready to innovate ? Let's get in touch

Chat With Us