How to Implement Tuple in Java

Creating custom tuple data type in java

Suraj Mishra
Javarevisited

--

Originally Published in https://asyncq.com/

Introduction

  • Tuple is a data structure that is designed to store heterogeneous objects.
  • It’s useful when we want to store or return heterogeneous objects such as Customer objects which consist of name, age, salary, etc.
  • In Java, we don’t have a tuple data structure out of the box provided by SDK, but we can use either Collection such as array or ArrayList.
  • We can also create our own Tuple class to provide such features.
  • Other than that we also have some libraries that support creating tuples in java.

Use Case

  • Let’s say we want to create customer object that would store name , age .
    name being string while age is integer.
  • Now In java if we want to store this heterogeneous data then we have few options in Java. Let’s explore them one by one.

Using List as Tuple

  • We generally store list to store a particular data type such as integer , string or even custom type .
  • But we can store object data type in List as well although its not recommended and can result in…

--

--

Suraj Mishra
Javarevisited

Staff Software Engineer @PayPal ( All opinions are my own and not of my employer )