Boost Java Productivity: Essential Utility Libraries and Code Snippets

This article introduces a curated collection of essential Java utility libraries—including built‑in methods, Apache Commons, Google Guava, and others—showing how to simplify common tasks such as string handling, collection operations, object mapping, file I/O, and more with concise code examples and Maven dependencies.

Code Ape Tech Column
Code Ape Tech Column
Code Ape Tech Column
Boost Java Productivity: Essential Utility Libraries and Code Snippets

Java provides built‑in utility methods for common tasks. The following examples show how to join a list, compare strings case‑insensitively, perform null‑safe equality checks, and compute the intersection of two lists.

1. Built‑in Java utilities

1.1 Join a List into a comma‑separated string

List<String> list = Arrays.asList("a", "b", "c");
// Stream API
String joined = list.stream().collect(Collectors.joining(","));
System.out.println(joined); // a,b,c

// String.join
String joined2 = String.join(",", list);
System.out.println(joined2); // a,b,c

1.2 Case‑insensitive string comparison

if (strA.equalsIgnoreCase(strB)) {
    System.out.println("equal");
}

1.3 Null‑safe object equality

boolean eq = Objects.equals(strA, strB);

Source implementation:

public static boolean equals(Object a, Object b) {
    return (a == b) || (a != null && a.equals(b));
}

1.4 Intersection of two lists

List<String> list1 = new ArrayList<>();
list1.add("a");
list1.add("b");
list1.add("c");

List<String> list2 = new ArrayList<>();
list2.add("a");
list2.add("b");
list2.add("d");

// retain common elements
list1.retainAll(list2);
System.out.println(list1); // [a, b]

2. Apache Commons utilities

2.1 commons‑lang3 (enhanced java.lang)

Maven dependency:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.12.0</version>
</dependency>

2.1.1 String emptiness checks

StringUtils.isEmpty(cs);      // true if cs is null or length 0
StringUtils.isNotEmpty(cs);   // opposite
StringUtils.isBlank(cs);      // true if cs is null, empty or whitespace only
StringUtils.isNotBlank(cs);   // opposite

2.1.2 Capitalize first letter

String s = "yideng";
String capitalized = StringUtils.capitalize(s);
System.out.println(capitalized); // Yideng

2.1.3 Repeat a string

String repeated = StringUtils.repeat("ab", 2);
System.out.println(repeated); // abab

2.1.4 Date formatting and parsing

// Date → String
String now = DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");
System.out.println(now);

// String → Date
Date parsed = DateUtils.parseDate("2021-05-01 01:01:01", "yyyy-MM-dd HH:mm:ss");

// Add one hour
Date later = DateUtils.addHours(new Date(), 1);

2.1.5 Temporary object wrappers

// Pair
ImmutablePair<Integer, String> pair = ImmutablePair.of(1, "yideng");
System.out.println(pair.getLeft() + "," + pair.getRight()); // 1,yideng

// Triple
ImmutableTriple<Integer, String, Date> triple = ImmutableTriple.of(1, "yideng", new Date());
System.out.println(triple.getLeft() + "," + triple.getMiddle() + "," + triple.getRight());

2.2 commons‑collections4 (collection utilities)

Maven dependency:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-collections4</artifactId>
    <version>4.4</version>
</dependency>

2.2.1 Collection emptiness and set operations

CollectionUtils.isEmpty(coll);      // true if null or empty
CollectionUtils.isNotEmpty(coll);   // opposite

Collection<String> intersect = CollectionUtils.retainAll(listA, listB);
Collection<String> union = CollectionUtils.union(listA, listB);
Collection<String> diff = CollectionUtils.subtract(listA, listB);

2.3 commons‑beanutils (bean manipulation)

Maven dependency:

<dependency>
    <groupId>commons-beanutils</groupId>
    <artifactId>commons-beanutils</artifactId>
    <version>1.9.4</version>
</dependency>

2.3.1 Set and get bean properties, map conversion

User user = new User();
BeanUtils.setProperty(user, "id", 1);
BeanUtils.setProperty(user, "name", "yideng");
System.out.println(BeanUtils.getProperty(user, "name")); // yideng

Map<String, String> map = BeanUtils.describe(user);
System.out.println(map); // {id=1, name=yideng}

User newUser = new User();
BeanUtils.populate(newUser, map);
System.out.println(newUser); // {id=1, name=yideng}

2.4 commons‑io (I/O utilities)

Maven dependency:

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.8.0</version>
</dependency>

2.4.1 File read/write/copy

File src = new File("demo1.txt");
List<String> lines = FileUtils.readLines(src, Charset.defaultCharset());

File dest = new File("demo2.txt");
FileUtils.writeLines(dest, lines);

FileUtils.copyFile(src, new File("demoCopy.txt"));

3. Google Guava utilities

Maven dependency:

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>30.1.1-jre</version>
</dependency>

3.1 Collection factories

List<String> list = Lists.newArrayList();
List<Integer> numbers = Lists.newArrayList(1, 2, 3);
List<Integer> reversed = Lists.reverse(numbers);
System.out.println(reversed); // [3, 2, 1]

List<List<Integer>> partitions = Lists.partition(numbers, 10);
Map<String, String> map = Maps.newHashMap();
Set<String> set = Sets.newHashSet();

3.2 Advanced collection types

3.2.1 Multimap (one key → multiple values)

Multimap<String, Integer> multimap = ArrayListMultimap.create();
multimap.put("key", 1);
multimap.put("key", 2);
System.out.println(multimap); // {key=[1, 2]}

Map<String, Collection<Integer>> asMap = multimap.asMap();

3.2.2 BiMap (bijective map)

BiMap<String, String> biMap = HashBiMap.create();
biMap.put("key", "value");
System.out.println(biMap); // {key=value}

BiMap<String, String> inverse = biMap.inverse();
System.out.println(inverse); // {value=key}

3.2.3 Table (two‑dimensional map)

Table<Integer, String, String> table = HashBasedTable.create();
table.put(18, "male", "yideng");
table.put(18, "female", "Lily");

System.out.println(table.get(18, "male")); // yideng
System.out.println(table.row(18)); // {male=yideng, female=Lily}
System.out.println(table.column("male")); // {18=yideng}

3.2.4 Multiset (bag for counting)

Multiset<String> multiset = HashMultiset.create();
multiset.add("apple");
multiset.add("apple");
multiset.add("orange");

System.out.println(multiset.count("apple")); // 2
System.out.println(multiset.elementSet()); // [orange, apple]

multiset.setCount("apple", 5);
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.

JavaCode ExamplesApache CommonsGoogle GuavaUtility Libraries
Code Ape Tech Column
Written by

Code Ape Tech Column

Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.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.