Boost Java Productivity with Essential Utility Libraries
This article introduces essential Java utility libraries—including built‑in methods, Apache Commons, and Google Guava—showing practical code snippets and Maven dependencies that help developers reduce boilerplate, improve readability, and boost productivity across backend projects.
After years of work, I realized many utility libraries can greatly simplify code and improve development efficiency, yet junior developers often miss them. These libraries have become industry standards and are used by large companies.
1. Java Built-in Utility Methods
1.1 Join List into Comma‑Separated String
// How to join a list into a comma‑separated string a,b,c
List<String> list = Arrays.asList("a", "b", "c");
// First method using streams
String join = list.stream().collect(Collectors.joining(","));
System.out.println(join); // a,b,c
// Second method using String.join
String join2 = String.join(",", list);
System.out.println(join2); // a,b,c1.2 Case‑Insensitive String Equality
if (strA.equalsIgnoreCase(strB)) {
System.out.println("相等");
}1.3 Object Equality with Null‑Safety
When using equals to compare objects, you need to check for null to avoid a NullPointerException. java.util.Objects provides a safe method. Objects.equals(strA, strB); Source implementation:
public static boolean equals(Object a, Object b) {
return (a == b) || (a != null && a.equals(b));
}1.4 List Intersection
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");
list1.retainAll(list2);
System.out.println(list1); // [a, b]2. Apache Commons Utility Libraries
Apache Commons is a powerful and widely used set of utility libraries. Below are some of the most common sub‑libraries.
2.1 commons‑lang3 (enhanced java.lang)
Recommended to use commons‑lang3, which optimizes several APIs; the older commons‑lang is no longer updated.
Maven dependency:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>String Empty Checks
Methods accept CharSequence (String, StringBuilder, StringBuffer) and check for null or length 0.
public static boolean isEmpty(final CharSequence cs) {
return cs == null || cs.length() == 0;
}
public static boolean isNotEmpty(final CharSequence cs) {
return !isEmpty(cs);
}
public static boolean isBlank(final CharSequence cs) {
final int strLen = length(cs);
if (strLen == 0) {
return true;
}
for (int i = 0; i < strLen; i++) {
if (!Character.isWhitespace(cs.charAt(i))) {
return false;
}
}
return true;
}
public static boolean isNotBlank(final CharSequence cs) {
return !isBlank(cs);
}Capitalize First Letter
String str = "yideng";
String capitalize = StringUtils.capitalize(str);
System.out.println(capitalize); // YidengRepeat String
String str = StringUtils.repeat("ab", 2);
System.out.println(str); // ababDate Formatting
No more manual SimpleDateFormat formatting.
// Date to String
String date = DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");
System.out.println(date); // 2021-05-01 01:01:01
// String to Date
Date date2 = DateUtils.parseDate("2021-05-01 01:01:01", "yyyy-MM-dd HH:mm:ss");
// Add one hour
Date date3 = DateUtils.addHours(new Date(), 1);Temporary Object Wrappers (Pair, Triple)
When a method needs to return multiple fields, Pair and Triple can be used instead of custom objects.
ImmutablePair<Integer, String> pair = ImmutablePair.of(1, "yideng");
System.out.println(pair.getLeft() + "," + pair.getRight()); // 1,yideng
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>Collection Empty Checks
public static boolean isEmpty(final Collection<?> coll) {
return coll == null || coll.isEmpty();
}
public static boolean isNotEmpty(final Collection<?> coll) {
return !isEmpty(coll);
}Other operations: CollectionUtils.retainAll, CollectionUtils.union, CollectionUtils.subtract.
2.3 commons‑beanutils (Object Manipulation)
Maven dependency:
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.4</version>
</dependency>Set and get properties, convert between objects and maps.
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);
User newUser = new User();
BeanUtils.populate(newUser, map);
System.out.println(newUser); // {"id":1,"name":"yideng"}2.4 commons‑io (File I/O)
Maven dependency:
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.8.0</version>
</dependency>Read, write, and copy files:
File file = new File("demo1.txt");
List<String> lines = FileUtils.readLines(file, Charset.defaultCharset());
FileUtils.writeLines(new File("demo2.txt"), lines);
FileUtils.copyFile(srcFile, destFile);3. Google Guava Utility Library
Maven dependency:
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>30.1.1-jre</version>
</dependency>3.1 Creating Collections
List<String> list = Lists.newArrayList();
List<Integer> list2 = Lists.newArrayList(1, 2, 3);
List<Integer> reverse = Lists.reverse(list2);
System.out.println(reverse); // [3, 2, 1]
List<List<Integer>> partition = Lists.partition(list2, 10);
Map<String, String> map = Maps.newHashMap();
Set<String> set = Sets.newHashSet();3.2 Advanced Collections
Multimap (multiple values per key)
Multimap<String, Integer> map = ArrayListMultimap.create();
map.put("key", 1);
map.put("key", 2);
System.out.println(map); // {"key":[1,2]}
Map<String, Collection<Integer>> collectionMap = map.asMap();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"}Table (two‑dimensional map)
Table<Integer, String, String> table = HashBasedTable.create();
table.put(18, "男", "yideng");
table.put(18, "女", "Lily");
System.out.println(table.get(18, "男")); // yideng
Map<String, String> row = table.row(18);
System.out.println(row); // {"男":"yideng","女":"Lily"}
Map<Integer, String> column = table.column("男");
System.out.println(column); // {18:"yideng"}Multiset (set with counting)
Multiset<String> multiset = HashMultiset.create();
multiset.add("apple");
multiset.add("apple");
multiset.add("orange");
System.out.println(multiset.count("apple")); // 2
Set<String> set = multiset.elementSet();
System.out.println(set); // ["orange","apple"]
Iterator<String> iterator = multiset.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
multiset.setCount("apple", 5);These personal experiences are shared for reference; any omissions or errors are welcome for correction.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
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.
