Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

The standard Java library does not provide enough methods to manipulate its core classes, and the Apache Commons Lang provides these additional methods.

Apache Commons Lang provides a number of help tools for Java. The Lang API, specifically String manipulation methods, basic numeric methods, object reflection, concurrency, creation and serialization, and the System property. In addition, it includes basic enhancements to java.util.date, as well as a series of utilities specifically designed to help build methods, such as hashCode, toString, and equals.

— from official introduction commons.apache.org/proper/comm…

What is a tuple?

A tuple is a basic concept in relational database. A relationship is a table in which each row (i.e. each record in the database) is a tuple and each column is an attribute. In two-dimensional tables, tuples are also called rows. — from Baidu Baike

What if, from everyday development, we often encounter a situation where we write a method and the distribution wants to return both a maximum and a minimum value? Encapsulate a return object? Or return a JSON object or list and parse it? It’s kind of ugly!

The Commons Lang provides us with an implementation of a tuple related data structure to help us solve this problem.

Commons Lang Tuple

Tuple related classes at org.apache.com mons. Lang3. A tuple, the tuples from whether variable dimension is divided into variable tuples and immutable tuple; From the dimension of the number of tuple elements, it can be divided into tuples and triples. Then add two and two, so there are a total of 2×2 tuples: mutable tuples, immutable tuples, mutable triples, and immutable triples.

Let’s take a look at the tuple class diagram:

From the class diagram, we can roughly see the relationships between the entire classes. The base class of all tuples is Pair, which inherits from Map.Entry. And the base class for triples is Triple. Let’s move on to actual combat.

Mutable binary group MutablePair

@Test
public void testMutablePair(a) {
    MutablePair<String, Boolean> mutablePair = MutablePair.of("test1".true);
    System.out.println("Lvalue:" + mutablePair.getLeft() + "Right value:" + mutablePair.getRight());

    mutablePair.setLeft("test2");
    mutablePair.setRight(false);
    System.out.println("Lvalue:" + mutablePair.getLeft() + "Right value:"+ mutablePair.getRight()); Output: lvalue: test1 Rvalue:trueLvalue: test2 Rvalue:false
}
Copy the code

Immutable binary ImmutablePair

In immutable tuples, the properties of stored values are all modified by final and therefore immutable.

@Test
public void testImmutablePair(a) {
    ImmutablePair<String, Boolean> immutablePair = ImmutablePair.of("test1".true);
    System.out.println("Lvalue:" + immutablePair.getLeft() + "Right value:"+ immutablePair.getRight()); } output: lvalue: test1 rvalue:true
Copy the code

Mutable triad

@Test
public void testMutableTriple(a) {
    MutableTriple<String, Boolean, Integer> mutableTriple = MutableTriple.of("test1".true.1);
    System.out.println(
        "Lvalue:" + mutableTriple.getLeft() + "Intermediate value:" + mutableTriple.getMiddle() + "Right value:" + mutableTriple.getRight());

    mutableTriple.setLeft("test2");
    mutableTriple.setMiddle(false);
    mutableTriple.setRight(2);
    System.out.println(
        "Lvalue:" + mutableTriple.getLeft() + "Intermediate value:" + mutableTriple.getMiddle() + "Right value:"+ mutableTriple.getRight()); } Output: lvalue: test1 Intermediate value:trueRight value:1Lvalue: test2 Intermediate value:falseRight value:2
Copy the code

Immutable triplet

Immutable works the same way as tuples, with properties modified by final.

@Test
public void testImmutableTriple(a) {
    ImmutableTriple<String, Boolean, Integer> immutableTriple = ImmutableTriple.of("test1".true.1);
    System.out.println(
        "Lvalue:" + immutableTriple.getLeft() + "Intermediate value:" + immutableTriple.getMiddle() + "Right value:"+ immutableTriple.getRight()); } Output: lvalue: test1 Intermediate value:trueRight value:1
Copy the code