Boost Your Java Productivity: Essential Utility Classes You Should Know

This article introduces nine indispensable Java utility classes—including Collections, CollectionUtils, Lists, Objects, StringUtils, BeanUtils, ReflectionUtils, DigestUtils, and HttpStatus—showing how each can simplify common tasks, reduce boilerplate code, and improve development efficiency with clear examples and code snippets.

Su San Talks Tech
Su San Talks Tech
Su San Talks Tech
Boost Your Java Productivity: Essential Utility Classes You Should Know

1. Collections

The java.util.Collections class provides a rich set of static methods for operating on collections. Below are some frequently used features.

1.1 Sorting

List<Integer> list = new ArrayList<>();
list.add(2);
list.add(1);
list.add(3);
Collections.sort(list); // ascending
System.out.println(list);
Collections.reverse(list); // descending
System.out.println(list);

Output:

[1, 2, 3]
[3, 2, 1]

1.2 Max / Min

List<Integer> list = new ArrayList<>();
list.add(2);
list.add(1);
list.add(3);
Integer max = Collections.max(list);
Integer min = Collections.min(list);
System.out.println(max);
System.out.println(min);

Output:

3
1

1.3 Empty List

private List<Integer> fun(List<Integer> list) {
    if (list == null || list.size() == 0) {
        return Collections.emptyList();
    }
    return list;
}

1.4 Unmodifiable List

List<Integer> list = new ArrayList<>();
list.add(2);
list.add(1);
list.add(3);
List<Integer> integers = Collections.unmodifiableList(list);
integers.add(4); // throws UnsupportedOperationException

1.5 Synchronized List

List<Integer> list = new ArrayList<>();
list.add(2);
list.add(1);
list.add(3);
List<Integer> integers = Collections.synchronizedList(list);
System.out.println(integers);

The underlying implementation creates SynchronizedRandomAccessList or SynchronizedList, which lock each method.

2. CollectionUtils

The Apache Commons CollectionUtils class offers additional collection utilities. Add the dependency:

<dependency>
    <groupId>commons-collections</groupId>
    <artifactId>commons-collections</artifactId>
    <version>3.2.2</version>
</dependency>

2.1 Empty / Not Empty

if (CollectionUtils.isEmpty(list)) {
    System.out.println("Collection is empty.");
}
if (CollectionUtils.isNotEmpty(list)) {
    System.out.println("Collection is not empty");
}

2.2 Set Operations

List<Integer> list2 = new ArrayList<>();
list2.add(2);
list2.add(4);
Collection<Integer> unionList = CollectionUtils.union(list, list2);
System.out.println(unionList);
Collection<Integer> intersectionList = CollectionUtils.intersection(list, list2);
System.out.println(intersectionList);
Collection<Integer> disjunctionList = CollectionUtils.disjunction(list, list2);
System.out.println(disjunctionList);
Collection<Integer> subtractList = CollectionUtils.subtract(list, list2);
System.out.println(subtractList);

Result:

[1, 2, 3, 4]
[2]
[1, 3, 4]
[1, 3]

3. Lists (Guava)

Guava’s com.google.common.collect.Lists provides convenient list utilities.

3.1 Quick Initialization

List<Integer> list = Lists.newArrayList(1, 2, 3);
System.out.println(list);

Output:

[1, 2, 3]

3.2 Cartesian Product

List<Integer> list1 = Lists.newArrayList(1, 2, 3);
List<Integer> list2 = Lists.newArrayList(4, 5);
List<List<Integer>> productList = Lists.cartesianProduct(list1, list2);
System.out.println(productList);

Output:

[[1, 4], [1, 5], [2, 4], [2, 5], [3, 4], [3, 5]]

3.3 Partition

List<Integer> list = Lists.newArrayList(1, 2, 3, 4, 5);
List<List<Integer>> partitionList = Lists.partition(list, 2);
System.out.println(partitionList);

Output:

[[1, 2], [3, 4], [5]]

3.4 Transform

List<String> list = Lists.newArrayList("a", "b", "c");
List<String> transformList = Lists.transform(list, x -> x.toUpperCase());
System.out.println(transformList);

Output:

[A, B, C]

3.5 Reverse

List<Integer> list = Lists.newArrayList(3, 1, 2, 5, 4);
List<Integer> reverseList = Lists.reverse(list);
System.out.println(reverseList);

Output:

[4, 5, 2, 1, 3]

4. Objects (JDK 7+)

4.1 Null Checks

Integer i = new Integer(10);
if (Objects.isNull(i)) {
    System.out.println("Object is null");
}
if (Objects.nonNull(i)) {
    System.out.println("Object is not null");
}

4.2 requireNonNull

Integer i = new Integer(128);
Objects.requireNonNull(i);
Objects.requireNonNull(i, "Parameters cannot be null");
Objects.requireNonNull(i, () -> "Parameters cannot be null");

4.3 Equality

Integer i1 = new Integer(1);
Integer i2 = new Integer(1);
System.out.println(Objects.equals(i1, i2)); // true
Long l = new Long(1);
System.out.println(Objects.equals(i1, l)); // false

4.4 Hash Code

String str = new String("abc");
System.out.println(Objects.hashCode(str));

5. StringUtils (Apache Commons Lang)

5.1 Empty / Blank Checks

String str1 = null;
String str2 = "";
String str3 = " ";
String str4 = "abc";
System.out.println(StringUtils.isEmpty(str1));
System.out.println(StringUtils.isEmpty(str2));
System.out.println(StringUtils.isEmpty(str3));
System.out.println(StringUtils.isEmpty(str4));
System.out.println(StringUtils.isBlank(str1));
System.out.println(StringUtils.isBlank(str2));
System.out.println(StringUtils.isBlank(str3));
System.out.println(StringUtils.isBlank(str4));

5.2 Split

String str1 = null;
System.out.println(StringUtils.split(str1, ",")); // returns null, no NPE

5.3 Numeric Check

System.out.println(StringUtils.isNumeric("123")); // true
System.out.println(StringUtils.isNumeric("123abc")); // false
System.out.println(StringUtils.isNumeric("0.33")); // false

5.4 Join

List<String> list = Lists.newArrayList("a", "b", "c");
List<Integer> list2 = Lists.newArrayList(1, 2, 3);
System.out.println(StringUtils.join(list, ","));
System.out.println(StringUtils.join(list2, " "));

6. BeanUtils (Spring)

6.1 Copy Properties

User user1 = new User();
user1.setId(1L);
user1.setName("Dylan");
user1.setAddress("Hong Kong");
User user2 = new User();
BeanUtils.copyProperties(user1, user2);
System.out.println(user2);

6.2 Find Declared Method

Method declaredMethod = BeanUtils.findDeclaredMethod(User.class, "getId");
System.out.println(declaredMethod.getName());

6.3 Find Property for Method

PropertyDescriptor pd = BeanUtils.findPropertyForMethod(declaredMethod);
System.out.println(pd.getName());

7. ReflectionUtils (Spring)

7.1 Find Method

Method method = ReflectionUtils.findMethod(User.class, "getId");

7.2 Find Field

Field field = ReflectionUtils.findField(User.class, "id");

7.3 Invoke Method

ReflectionUtils.invokeMethod(method, beanInstance, args);

7.4 Check Constant

System.out.println(ReflectionUtils.isPublicStaticFinal(field));

7.5 Check Equals Method

System.out.println(ReflectionUtils.isEqualsMethod(method));

8. DigestUtils (Apache Commons Codec)

8.1 MD5

String md5Hex = DigestUtils.md5Hex("Dylan");
System.out.println(md5Hex);

8.2 SHA‑256

String sha256Hex = DigestUtils.sha256Hex("Dylan");
System.out.println(sha256Hex);

9. HttpStatus

Instead of defining custom status codes, use the standard enums from org.springframework.http.HttpStatus or org.apache.http.HttpStatus which already provide constants such as 200 (OK), 404 (NOT_FOUND), and 500 (INTERNAL_SERVER_ERROR).

Conclusion

The nine utility classes presented—Collections, CollectionUtils, Lists, Objects, StringUtils, BeanUtils, ReflectionUtils, DigestUtils, and HttpStatus—cover a wide range of common development scenarios. Leveraging them can dramatically reduce boilerplate code, improve readability, and let developers focus on business logic rather than low‑level implementation details.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaspringCollectionsApache CommonsUtility Classes
Su San Talks Tech
Written by

Su San Talks Tech

Su San, former staff at several leading tech companies, is a top creator on Juejin and a premium creator on CSDN, and runs the free coding practice site www.susan.net.cn.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.