What is ImmutableTriple in Java?

Overview

An immutable triple consists of three Object elements.

Although the implementation is immutable, there is no restriction on the objects that may be stored. If mutable objects are stored in the triple, then the triple effectively becomes mutable.

ImmutableTriple is defined in the Apache Commons Lang. The package has to be added to the classpath before ImmutableTriple can be used.

If you use Maven, add the following in the dependencies section of pom.xml.

<dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.12.0</version>
</dependency>

After you import the dependency above, import the ImmutableTriple class as follows.

import org.apache.commons.lang3.tuple.ImmutableTriple;

Note: There are no setters in this class, as the data structure is immutable in nature.

The three Object elements in the ImmutableTriple are named left, middle, and right. So, the objects will be referred to as left, middle, and right for any operations on the data structure.

Create an ImmutableTriple object

There are several ways to create an object of ImmutableTriple.

  1. Use the constructors

The constructor of the ImmutableTriple class accepts the left, middle, and right elements as parameters.

ImmutableTriple<String, String, String> immutableTriple =  new ImmutableTriple<>("leftValue", "middleValue", "rightValue");
  1. Use of() method:

The of(left, middle, right) can be used to create an object of the ImmutableTriple class. It accepts the left, middle, and right elements as parameters.

ImmutableTriple<String, String> immutableTriple =  ImmutableTriple.of("leftValue", "middleValue", "rightValue");

Access the elements of ImmutableTriple object

Access the left element

The left element can be accessed as a property or using the getLeft() method.

String leftElement = immutableTriple.left;
// OR
String leftElement = immutableTriple.getLeft();

Access the middle element

The middle element can be accessed as a property or using the getMiddle() method.

String middleElement = immutableTriple.middle;
// OR
String middleElement = immutableTriple.getMiddle();

Access the right element

The right element can be accessed as a property or using the getRight() method.

String rightElement = immutableTriple.right;
// OR
String rightElement = immutableTriple.getRight();

Example

In the example below, an ImmutableTriple object is created with the left, middle, and right values. Then, the elements of the object are printed.

import org.apache.commons.lang3.tuple.ImmutableTriple;

public class Main {

    public static void main(String args[])
    {
        ImmutableTriple<String, String, String> immutableTriple =  new ImmutableTriple<>("leftValue", "middleValue", "rightValue");
        System.out.println("Left Element - " + immutableTriple.getLeft());
        System.out.println("Middle Element - " + immutableTriple.getLeft());
        System.out.println("Right Element - " + immutableTriple.getRight());
        
    }
}

Free Resources