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.
MutableTriple
objectThere are several ways to create an object of MutableTriple
.
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");
of()
methodThe 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");
MutableTriple
objectleft
elementThe left
element can be set using the setLeft()
method.
mutableTriple.setLeft("leftValue");
right
elementThe right
element can be set using the setRight()
method.
mutableTriple.setRight("rightValue");
middle
elementThe middle
element can be set using the setMiddle()
method.
mutableTriple.setMiddle("middleValue");
MutableTriple
objectleft
elementThe left
element can be accessed as a property
or by the getLeft()
method.
String leftElement = mutableTriple.left;
// OR
String leftElement = mutableTriple.getLeft();
middle
elementThe middle
element can be accessed as a property
or by the getMiddle()
method.
String middleElement = mutableTriple.middle;
// OR
String middleElement = mutableTriple.getMiddle();
right
elementThe right
element can be accessed as a property
or by the getRight()
method.
String rightElement = mutableTriple.right;
// OR
String rightElement = mutableTriple.getRight();
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