What is MutableTriple in Java?

Overview

A mutable triple consists of three object elements.

MutableTriple is defined in the Apache Commons Lang. The package has to be added to the classpath before MutableTriple 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 MutableTriple class, as follows.

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

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

The three object elements in the MutableTriple 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 a MutableTriple object

There are several ways to create an object of MutableTriple.

1. Use the constructors

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

MutableTriple<String, String, String> mutableTriple =  new MutableTriple<>("leftValue", "middleValue", "rightValue");

2. Use the of() method

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

MutableTriple<String, String> mutableTriple =  MutableTriple.of("leftValue", "middleValue", "rightValue");

Set the elements of MutableTriple object

Set the left element

The left element can be set using the setLeft() method.

mutableTriple.setLeft("leftValue");

Set the right element

The right element can be set using the setRight() method.

mutableTriple.setRight("rightValue");

Set the middle element

The middle element can be set using the setMiddle() method.

mutableTriple.setMiddle("middleValue");

Access the elements of MutableTriple object

Access the left element

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

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

Access the middle element

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

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

Access the right element

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

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

Example

In the example below, an empty MutableTriple object is created. The values of left, middle, and right are set using setters. Then, the elements of the object are printed.

import org.apache.commons.lang3.tuple.MutableTriple;
public class Main {
public static void main(String[] args){
MutableTriple<String, String, String> mutableTriple = new MutableTriple<>();
mutableTriple.setLeft("leftValue");
mutableTriple.setRight("rightValue");
mutableTriple.setMiddle("middleValue");
System.out.println("Left Element - " + mutableTriple.getLeft());
System.out.println("Middle Element - " + mutableTriple.getMiddle());
System.out.println("Right Element - " + mutableTriple.getRight());
}
}

Free Resources

Attributions:
  1. undefined by undefined