Master Java’s Pair and Triple: Simplify Multi‑Value Returns with Apache Commons Lang3
This article explains why returning multiple values in Java can be cumbersome, introduces Apache Commons Lang3’s Pair and Triple utilities—including mutable and immutable variants—shows how to add the Maven dependency, and provides clear code examples that demonstrate their creation, modification, and safe usage in real‑world scenarios.
Why Multiple Return Values Matter
In everyday Java development you often need to return more than one value from a method, such as a result together with auxiliary information. Traditional approaches—creating a custom wrapper class or using a Map —tend to bloat code and make the caller’s intent unclear.
Apache Commons Lang3 Pair and Triple
The org.apache.commons.lang3.tuple package offers Pair and Triple abstract classes that represent two‑ and three‑element tuples. They implement Map.Entry (for Pair) and provide Comparable and Serializable support, enabling concise, type‑safe value grouping without extra boilerplate.
Adding the Dependency
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>Include the snippet above in your pom.xml to gain access to the tuple classes.
Pair Class Overview
Pair<L,R>is an abstract representation of a key‑value pair. It implements Map.Entry<L,R> and Comparable<Pair<L,R>>, and supports serialization.
Key Subclasses
ImmutablePair : Once created, its left and right fields are final, making the instance thread‑safe. Attempting to call setValue throws UnsupportedOperationException.
MutablePair : Provides public mutable fields and setter methods, allowing the pair’s values to be changed after construction. It is not thread‑safe.
Usage Example
class UserDO {
private String userId;
private Integer age;
public String getUserId() { return userId; }
public void setUserId(String userId) { this.userId = userId; }
public Integer getAge() { return age; }
public void setAge(Integer age) { this.age = age; }
}
// Creating an immutable pair
Pair<String, Integer> idAge = ImmutablePair.of("U123", 30);
// Mutable pair example
MutablePair<String, Integer> mutable = new MutablePair<>();
mutable.setLeft("U456");
mutable.setRight(25);Attempting to modify ImmutablePair results in an exception, demonstrating its immutability.
Triple Class Overview
Triple<L,M,R>extends the tuple concept to three elements—left, middle, and right. Like Pair, it is abstract and provides static factory methods for creating immutable or mutable instances.
Key Subclasses
ImmutableTriple : All three fields are final, guaranteeing thread safety when the contained objects are themselves immutable.
MutableTriple : Exposes public fields left, middle, right and setter methods ( setLeft, setMiddle, setRight). It is not thread‑safe.
Usage Example
class UserDO {
private String userId;
private String userName;
private Integer sex;
// getters and setters omitted for brevity
}
// Immutable triple
Triple<String, String, Integer> userInfo = ImmutableTriple.of("U789", "Alice", 1);
// Mutable triple
MutableTriple<String, String, Integer> mutableTriple = new MutableTriple<>();
mutableTriple.setLeft("U101");
mutableTriple.setMiddle("Bob");
mutableTriple.setRight(0);Conclusion
By leveraging Pair and Triple from Apache Commons Lang3, Java developers can write cleaner, more readable code when a method needs to return multiple related values. The immutable variants provide thread safety out of the box, while mutable versions give flexibility when mutability is required. Using these utilities reduces boilerplate, preserves type safety, and improves overall code maintainability.
Java Architect Handbook
Focused on Java interview questions and practical article sharing, covering algorithms, databases, Spring Boot, microservices, high concurrency, JVM, Docker containers, and ELK-related knowledge. Looking forward to progressing together with you.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
