Java Tuple Tutorial [+Examples]

Danielle Ellis
Danielle Ellis



Are you working with a lot of data? Do you need to store and reference multiple values but don't want the clutter of a class declaration? If so, Java tuples may be just what you're looking for. Tuples are like classes, but they are declared in a much more concise way making them ideal for working with large amounts of data.

Senior Engineer teaching Java Tuple to Junior Engineer

Download Now: An Introduction to Java & JavaScript

In this Java tuple tutorial, we'll show you what Java tuples are, the different types of tuples, and how to use them with examples.

So let's get started!

What is a Java Tuple?

A Java tuple is a data structure that allows you to store a set of data values. This data structure is similar to a Java list but with some key differences. A tuple is immutable, meaning you cannot add or remove elements from it. Tuples are also typically used to store data items that may not necessarily be directly related but together has a meaning, such as the coordinates of a point on a map. Take a look at some examples below.

Tuple Output Examples:

[320, 739] ["John Doe", 23, "New York"]

In computer programming, a tuple is often seen as a sequence of values. As shown in the example above, a tuple can contain many elements and any data type (including mixed data types). For example, a 2-element tuple might contain true and false Boolean values. A 3-element tuple might contain a Floating-point number, a Character, and a String.

Now that you know a bit more about tuples let's look at some of the different ways you can create them in Java.

Creating a Tuple in Java

A tuple is created by placing all the items (elements) inside the parenthesis (), separated by commas. The syntax for creating a tuple in Java is shown below.

Tuple t = new Tuple (a, b, c);

In the example, "a", "b", and "c" are the values assigned to the tuple.

Java tuples support tuples of sizes up to 10. We have listed a few common tuple types below:

  • Tuple with 2 elements - also called a Pair
  • Tuple with 3 elements - also called a Triple
  • Tuple with 4 elements - also called a Quad

Implementing Java Tuple

When you implement a tuple in Java, you have a few different options. We will begin discussing the ArrayList method then the Constructor method.

Using ArrayList With Java Tuple

The first way we will look at is to use the built-in Java class java.util.ArrayList:

ArrayList myTuple = new ArrayList(); myTuple.add("John Doe"); myTuple.add(23); myTuple.add("New York");

In the example above, we created an ArrayList object and stored it in a variable called myTuple. We then added three elements to the tuple - "John Doe", 23, and "New York". You can access the individual elements of the tuple using the get() method, which we will discuss later.

Using Constructor Method With Java Tuple

You can also create a tuple in Java by using the Pair, Triple, or Quad classes. These are generic classes, meaning you need to specify the data type of the elements when creating a tuple.

Pair pair = new Pair<>("One", 1); 
Triple triple = new Triple<>("One", "Two", 3); Quad quad = new Quad<>("One", "Two", "Three", 4);

As you can see from the examples above, each tuple is created by specifying the data type of each element in angle brackets (<>). You can then initialize the tuple by passing in the values for each element.

Now you see a few different ways you can create a tuple in Java. The method you choose to use will depend on your specific needs.

We will now learn about some of the different operations you can perform on tuples.

Java Tuple Operations With Examples

As we mentioned earlier, one key difference between a list and a tuple is that tuples are immutable. This means you cannot add or remove elements from a tuple. However, there are still some operations you can perform on tuples. Let's take a look. 

Accessing Elements in a Tuple

Once you have created a tuple, you may want to access its data. To do this, you can use the get() method:

myTuple.get(0); // returns "John Doe" myTuple.get(1); // returns 23 myTuple.get(2); // returns "New York"

You can also use the set() method to update the value of an element in a tuple:

myTuple.set(0, "Jane Doe"); // myTuple now contains ["Jane Doe", 23, "New York"]

However, it is important to note that the set() method does not change the tuple itself. Instead, it returns a new tuple with the updated element.

Iterating Over a Tuple

You can use the forEach() method to iterate over the elements in a tuple:

myTuple.forEach(System.out::println); // prints "John Doe", 23, "New York"

Alternatively, you can use the for loop to achieve the same result:

for (int i = 0; i < myTuple.size(); i++) { System.out.println(myTuple.get(i)); }

Converting a Tuple to a String

You can use the toString() method to convert a tuple to a String:

myTuple.toString(); // returns "(John Doe, 23, New York)"

This can be useful if you need to print the contents of a tuple for debugging purposes.

Comparing Tuples

You can use the equals() method to compare two tuples:

Tuple myTuple1 = Tuple.of("John Doe", 23, "New York"); Tuple myTuple2 = Tuple.of("John Doe", 23, "New York"); myTuple1.equals(myTuple2); // returns true

You can also use the compareTo() method to compare two tuples. This method returns a positive integer if the first tuple is greater than the second tuple, a negative integer if the first tuple is less than the second tuple, or 0 if the two tuples are equal:

myTuple1.compareTo(myTuple2); // returns 0

When should you use a Java Tuple?

A tuple in Java is useful for data structures that store a set number of elements of different types.  By storing these elements in a tuple, you can easily access them without having to create separate variables for each element. In this article, you've learned what a tuple is and how to create and manipulate tuples in Java. Now that you know how to use tuples, you can start using them in your own Java programs.


Topics: Java

Related Articles

We're committed to your privacy. HubSpot uses the information you provide to us to contact you about our relevant content, products, and services. You may unsubscribe from these communications at any time. For more information, check out our Privacy Policy.

Learn more about the differences between and uses of these popular programming languages.