Apache Commons Lang provides essential Java utility classes for string manipulation, object operations, array handling, date/time processing, reflection utilities, and more.
npx @tessl/cli install tessl/maven-org-apache-commons--commons-lang3@3.18.0Apache Commons Lang is a comprehensive utility library that provides a rich set of helpers for Java development. This library contains 223+ public classes across 16 packages with over 2000 public static utility methods, making it one of the most essential libraries for Java developers.
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.18.0</version>
</dependency>Maven Coordinates: org.apache.commons/commons-lang3@3.18.0
Java Version: Java 8+
License: Apache License 2.0
The most commonly used classes can be imported as follows:
// String utilities
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.CharUtils;
// Array utilities
import org.apache.commons.lang3.ArrayUtils;
// Object utilities
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.ClassUtils;
import org.apache.commons.lang3.BooleanUtils;
// Math utilities
import org.apache.commons.lang3.math.NumberUtils;
// Builder pattern utilities
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
// Validation
import org.apache.commons.lang3.Validate;
// Date/Time utilities
import org.apache.commons.lang3.time.DateUtils;
import org.apache.commons.lang3.time.StopWatch;
// Exception handling
import org.apache.commons.lang3.exception.ExceptionUtils;StringUtils provides 233+ null-safe string operations:
// Null-safe string operations
String result = StringUtils.defaultString(null, "default"); // "default"
boolean empty = StringUtils.isEmpty(null); // true
boolean blank = StringUtils.isBlank(" "); // true
String clean = StringUtils.trimToNull(" text "); // "text"
// String formatting and manipulation
String abbrev = StringUtils.abbreviate("Long text here", 10); // "Long te..."
String padded = StringUtils.leftPad("123", 5, '0'); // "00123"
String joined = StringUtils.join(Arrays.asList("a", "b"), ","); // "a,b"ArrayUtils provides 368+ methods for array manipulation:
// Array creation and manipulation
int[] array = ArrayUtils.add(new int[]{1, 2}, 3); // [1, 2, 3]
boolean contains = ArrayUtils.contains(array, 2); // true
int[] reversed = ArrayUtils.reverse(array); // [3, 2, 1]
String[] subarray = ArrayUtils.subarray(new String[]{"a", "b", "c"}, 1, 3);
// Null-safe operations
boolean isEmpty = ArrayUtils.isEmpty(null); // true
int length = ArrayUtils.getLength(array); // 3ObjectUtils provides null-safe object operations:
// Null-safe operations
String result = ObjectUtils.defaultIfNull(null, "default"); // "default"
String first = ObjectUtils.firstNonNull(null, null, "value"); // "value"
boolean allNull = ObjectUtils.allNull(null, null); // true
// Object comparison and cloning
int comparison = ObjectUtils.compare("a", "b"); // -1
Object cloned = ObjectUtils.clone(originalObject);The Validate class provides argument validation with clear error messages:
// Argument validation
Validate.notNull(object, "Object cannot be null");
Validate.notEmpty(collection, "Collection cannot be empty");
Validate.inclusiveBetween(1, 10, value, "Value must be between 1 and 10");
Validate.isTrue(condition, "Condition must be true");Apache Commons Lang is organized into focused packages:
Here's a quick example showing common patterns:
import org.apache.commons.lang3.*;
import org.apache.commons.lang3.builder.*;
import org.apache.commons.lang3.math.*;
public class CommonsLangExample {
public void demonstrateUsage() {
// String operations
String input = null;
String safe = StringUtils.defaultString(input, "N/A");
// Array operations
String[] array = {"apple", "banana", "cherry"};
boolean hasApple = ArrayUtils.contains(array, "apple");
// Number parsing
int number = NumberUtils.toInt("123", 0);
// Validation
Validate.notEmpty(array, "Array cannot be empty");
// Builder pattern
String description = new ToStringBuilder(this)
.append("safe", safe)
.append("hasApple", hasApple)
.append("number", number)
.toString();
}
}This library is organized into several functional areas, each with comprehensive documentation:
Apache Commons Lang is designed for high performance with minimal overhead:
// Defensive programming with null checks
String result = StringUtils.trimToNull(input);
if (StringUtils.isNotBlank(result)) {
// Process non-blank string
}
// Fluent validation
Validate.notNull(user, "User cannot be null")
.notEmpty(user.getName(), "Name cannot be empty");
// Builder pattern for complex objects
String description = ToStringBuilder.reflectionToString(object,
ToStringStyle.SHORT_PREFIX_STYLE);@Component
public class UserService {
public User createUser(String name, String email) {
Validate.notBlank(name, "Name is required");
Validate.notBlank(email, "Email is required");
String cleanName = StringUtils.trim(name);
String normalizedEmail = StringUtils.lowerCase(email);
return new User(cleanName, normalizedEmail);
}
}@Test
public void testUserValidation() {
// Using ArrayUtils for test data
String[] validNames = {"John", "Jane", "Bob"};
String[] invalidNames = {null, "", " "};
for (String name : validNames) {
assertDoesNotThrow(() -> userService.createUser(name, "test@example.com"));
}
for (String name : invalidNames) {
assertThrows(IllegalArgumentException.class,
() -> userService.createUser(name, "test@example.com"));
}
}Most methods are backward compatible, with key improvements:
org.apache.commons.lang to org.apache.commons.lang3This library serves as the foundation for robust, maintainable Java applications by providing well-tested, performant utilities for common programming tasks.