Open links in new tab
  1. A tuple is a data structure that can hold a fixed number of elements of different types. Tuples are immutable, meaning once they are created, their values cannot be changed. They are useful for grouping multiple values together without creating a separate class for each combination of values.

    Creating Tuples

    In Java, tuples are not part of the standard library, but you can use the Javatuples library to work with tuples. This library provides classes for tuples of different sizes, from a single element (Unit) to ten elements (Decade). To use Javatuples, you need to add the following Maven dependency to your pom.xml file:

    <dependency>
    <groupId>org.javatuples</groupId>
    <artifactId>javatuples</artifactId>
    <version>1.2</version>
    </dependency>

    Example of Creating Tuples

    You can create tuples using the with() method or constructors provided by the Javatuples library. Here are some examples:

    import org.javatuples.Pair;
    import org.javatuples.Triplet;
    import org.javatuples.Quartet;

    public class TupleExample {
    public static void main(String[] args) {
    // Creating a Pair
    Pair<String, Integer> pair = Pair.with("A pair", 55);

    // Creating a Triplet
    Triplet<String, Integer, Double> triplet = Triplet.with("hello", 23, 1.2);

    // Creating a Quartet
    Quartet<String, Double, Integer, String> quartet = Quartet.with("john", 72.5, 32, "1051 SW");

    // Printing the tuples
    System.out.println(pair);
    System.out.println(triplet);
    System.out.println(quartet);
    }
    }
    Feedback
  1. Java Tuple (with Examples) - HowToDoInJava

    Technically speaking, a tuple is like a small and immutablewrapper object that holds different pieces of information. The pieces of contained information do not necessarily relate to each other in any way, but collectively these will have some meaning. For example, [“Sajal Chakraborty,” “IT Professional,” 32]can be a tupl…
    See more on howtodoinjava.com
  2. Introduction to Javatuples - Baeldung

    May 11, 2024 · In this tutorial, we’ll take a quick look at a really simple library, javatuples, that allows us to work with the tuple based data structures. 2. Built …

    • Estimated Reading Time: 5 mins
    • Java Tuple - Tpoint Tech

      A tuple is a fixed-size and ordered list of items. Unlike arrays or lists, tuples can contain elements of any type as well as them being immutable, this impl...

    • JavaTuples | Introduction - GeeksforGeeks

      Sep 16, 2021 · JavaTuples is a Java library that offers classes, functions and data structures to work with tuples. It is one of the simplest java library ever made. JavaTuples offers following …

    • Java Tuple Example - Java Code Geeks

      Apr 24, 2020 · Let us understand the different tuples and various methods in the tuple library of Java through the sample code. Tuples.java. // Create a tuple. // Creating a tuple through "with" …

    • JavaTuples fromArray () method - GeeksforGeeks

      Aug 30, 2018 · The fromArray () method in org.javatuples is used to instantiate a tuple in a semantically elegant way, with the values of the array, given as parameters. This method can …

    • Java Tuple - CodeGym

      Feb 17, 2025 · JavaTuples is a powerful library that provides a wide range of tuple types for Java developers. JavaTuples simplifies working with tuples by offering pre-defined classes for tuples of various arities (number of components).

    • Some results have been removed
    Refresh