Boost Java Development Efficiency with 17 Essential Utility Classes

This article introduces 17 practical Java utility classes—including Collections, CollectionUtils, Lists, Objects, BooleanUtils, StringUtils, Assert, IOUtils, MDC, ClassUtils, BeanUtils, ReflectionUtils, Base64Utils, StandardCharsets, DigestUtils, SerializationUtils, and HttpStatus—showing how to use them with concise code examples to streamline everyday development tasks.

Su San Talks Tech
Su San Talks Tech
Su San Talks Tech
Boost Java Development Efficiency with 17 Essential Utility Classes

Preface

Java offers many handy utilities, often referred to as "wheels," that can dramatically improve development efficiency when combined with IDE shortcuts.

1. Collections

The java.util.Collections class provides common collection operations.

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);

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);

1.3 Thread‑Safe Collections

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

1.4 Empty Collections

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

1.5 Binary Search

List<Integer> list = new ArrayList<>();
list.add(2);
list.add(1);
list.add(3);
int i = Collections.binarySearch(list, 3);
System.out.println(i);

1.6 Unmodifiable Collections

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

2. CollectionUtils

Beyond Collections, the CollectionUtils class from Apache Commons or Spring provides additional helpers.

2.1 Empty Check

List<Integer> list = new ArrayList<>();
list.add(2);
list.add(1);
list.add(3);
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> list = new ArrayList<>();
list.add(2);
list.add(1);
list.add(3);
List<Integer> list2 = new ArrayList<>();
list2.add(2);
list2.add(4);
Collection<Integer> union = CollectionUtils.union(list, list2);
Collection<Integer> intersection = CollectionUtils.intersection(list, list2);
Collection<Integer> disjunction = CollectionUtils.disjunction(list, list2);
Collection<Integer> subtract = CollectionUtils.subtract(list, list2);
System.out.println(union);
System.out.println(intersection);
System.out.println(disjunction);
System.out.println(subtract);

3. Lists (Guava)

Guava’s Lists class offers convenient list utilities.

3.1 Create Empty List

List<Integer> list = Lists.newArrayList();

3.2 Initialize List

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

3.3 Cartesian Product

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

3.4 Partition

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

3.5 Transform

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

3.6 Reverse

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

4. Objects (JDK 7+)

4.1 Null Checks

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

4.2 requireNonNull

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

4.3 Equality

Integer a = new Integer(1);
Long b = new Long(1);
System.out.println(Objects.equals(a, b)); // false

4.4 hashCode

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

5. BooleanUtils (Apache Commons)

5.1 isTrue / isFalse

Boolean b = new Boolean(true);
System.out.println(BooleanUtils.isTrue(b));
System.out.println(BooleanUtils.isFalse(b));

5.2 isNotTrue / isNotFalse

Boolean b = new Boolean(true);
Boolean bNull = null;
System.out.println(BooleanUtils.isNotTrue(b));
System.out.println(BooleanUtils.isNotTrue(bNull));
System.out.println(BooleanUtils.isNotFalse(b));
System.out.println(BooleanUtils.isNotFalse(bNull));

5.3 toInteger

Boolean b = new Boolean(true);
Boolean bFalse = new Boolean(false);
System.out.println(BooleanUtils.toInteger(b)); // 1
System.out.println(BooleanUtils.toInteger(bFalse)); // 0

5.4 toBoolean

Boolean b = new Boolean(true);
Boolean bNull = null;
System.out.println(BooleanUtils.toBoolean(b));
System.out.println(BooleanUtils.toBoolean(bNull));
System.out.println(BooleanUtils.toBooleanDefaultIfNull(bNull, false));

6. StringUtils (Apache Commons Lang)

6.1 Empty / Blank Checks

String s1 = null;
String s2 = "";
String s3 = " ";
String s4 = "abc";
System.out.println(StringUtils.isEmpty(s1));
System.out.println(StringUtils.isBlank(s3));
System.out.println(StringUtils.isNotBlank(s4));

6.2 Split

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

6.3 isNumeric

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

6.4 join

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

7. Assert (Spring)

String str = null;
Assert.isNull(str, "must be null");
Assert.notNull(str, "must not be null"); // throws IllegalArgumentException

8. IOUtils (Apache Commons IO)

8.1 Read File

String content = IOUtils.toString(new FileInputStream("/temp/a.txt"), StandardCharsets.UTF_8);
System.out.println(content);

8.2 Write File

String data = "abcde";
IOUtils.write(data, new FileOutputStream("/temp/b.txt"), StandardCharsets.UTF_8);

8.3 Copy File

IOUtils.copy(new FileInputStream("/temp/a.txt"), new FileOutputStream("/temp/b.txt"));

8.4 toByteArray

byte[] bytes = IOUtils.toByteArray(new FileInputStream("/temp/a.txt"));

9. MDC (SLF4J)

Using MDC to store a traceId per request and propagate it via a ClientHttpRequestInterceptor for logging and tracing.

public class LogFilter implements Filter {
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
        MdcUtil.add(UUID.randomUUID().toString());
        chain.doFilter(request, response);
    }
}

public class RestTemplateInterceptor implements ClientHttpRequestInterceptor {
    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) {
        request.getHeaders().set("traceId", MdcUtil.get());
        return execution.execute(request, body);
    }
}

10. ClassUtils (Spring)

Class<?>[] interfaces = ClassUtils.getAllInterfaces(new User());
String pkg = ClassUtils.getPackageName(User.class);
System.out.println(ClassUtils.isInnerClass(User.class));
System.out.println(ClassUtils.isCglibProxy(new User()));

11. BeanUtils (Spring)

User u1 = new User();
u1.setId(1L);
u1.setName("SuSan");
User u2 = new User();
BeanUtils.copyProperties(u1, u2);
System.out.println(u2);

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

12. ReflectionUtils (Spring)

Method m = ReflectionUtils.findMethod(User.class, "getId");
Field f = ReflectionUtils.findField(User.class, "id");
ReflectionUtils.invokeMethod(m, userInstance);
System.out.println(ReflectionUtils.isPublicStaticFinal(f));

13. Base64Utils (Spring)

String str = "abc";
String encoded = new String(Base64Utils.encode(str.getBytes()));
String decoded = new String(Base64Utils.decode(encoded.getBytes()), StandardCharsets.UTF_8);
System.out.println(encoded);
System.out.println(decoded);

14. StandardCharsets (Java)

Use StandardCharsets.UTF_8 instead of string literals for charset specifications.

15. DigestUtils (Apache Commons Codec)

String md5 = DigestUtils.md5Hex("SuSan says tech");
String sha256 = DigestUtils.sha256Hex("SuSan says tech");
System.out.println(md5);
System.out.println(sha256);

16. SerializationUtils (Spring)

Map<String, String> map = new HashMap<>();
map.put("a", "1");
byte[] data = SerializationUtils.serialize(map);
Object obj = SerializationUtils.deserialize(data);
System.out.println(obj);

17. HttpStatus (Spring / Apache)

Use the predefined HttpStatus enum constants (e.g., HttpStatus.OK, HttpStatus.NOT_FOUND) instead of hard‑coding numeric status codes.

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.

backend-developmentutilitiescode-examplesApache Commons
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.