Common Java Utility Libraries and Their Usage Examples
This article introduces a collection of essential Java utility libraries—including core Java methods, Apache Commons, Commons BeanUtils, Commons IO, and Google Guava—explaining their key features, Maven dependencies, and practical code snippets for tasks such as string handling, collection operations, object mapping, file I/O, and advanced data structures.
1. Java Built‑in Utility Methods
Examples of common operations using the standard Java API, such as joining a List into a comma‑separated string, case‑insensitive string comparison, safe object equality checks with Objects.equals , and obtaining the intersection of two lists.
// How to join a list into a comma‑separated string
List
list = Arrays.asList("a", "b", "c");
String join = list.stream().collect(Collectors.joining(","));
System.out.println(join); // a,b,c
// Using String.join
default String join = String.join(",", list);
System.out.println(join); // a,b,c
// Case‑insensitive comparison
if (strA.equalsIgnoreCase(strB)) {
System.out.println("相等");
}
// Safe object equality
Objects.equals(strA, strB);
// List intersection
list1.retainAll(list2);
System.out.println(list1); // [a, b]2. Apache Commons Utility Libraries
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>Key utilities include:
StringUtils.isEmpty / isNotEmpty – null‑safe string emptiness checks.
StringUtils.isBlank / isNotBlank – also trims whitespace.
StringUtils.capitalize – capitalizes the first character.
StringUtils.repeat – repeats a string.
DateFormatUtils.format and DateUtils – simplify date formatting and arithmetic.
ImmutablePair and ImmutableTriple – lightweight tuple containers.
// String emptiness checks
boolean empty = StringUtils.isEmpty(cs);
boolean notEmpty = StringUtils.isNotEmpty(cs);
// Capitalize first letter
String capitalize = StringUtils.capitalize("yideng");
System.out.println(capitalize); // Yideng
// Repeat string
String repeated = StringUtils.repeat("ab", 2);
System.out.println(repeated); // abab
// Date formatting
String date = DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");
System.out.println(date);
// Tuple example
ImmutablePair
pair = ImmutablePair.of(1, "yideng");
System.out.println(pair.getLeft() + "," + pair.getRight()); // 1,yideng2.2 commons‑collections4 (Collection Utilities)
Maven dependency:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>Provides methods such as CollectionUtils.retainAll , union , and subtract for set operations, as well as CollectionUtils.isEmpty checks.
// Collection emptiness
boolean empty = CollectionUtils.isEmpty(coll);
// Intersection, union, subtraction
Collection
intersect = CollectionUtils.retainAll(listA, listB);
Collection
union = CollectionUtils.union(listA, listB);
Collection
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>Allows setting and getting bean properties via reflection, and converting between beans 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
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 Utilities)
Maven dependency:
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.8.0</version>
</dependency>Simplifies reading, writing, and copying files.
File file = new File("demo1.txt");
List
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 Collection Creation
List
list = Lists.newArrayList();
List
numbers = Lists.newArrayList(1, 2, 3);
List
reversed = Lists.reverse(numbers);
System.out.println(reversed); // [3, 2, 1]
List
> partitions = Lists.partition(numbers, 10);
Map
map = Maps.newHashMap();
Set
set = Sets.newHashSet();3.2 Advanced Collection Types
3.2.1 Multimap (multiple values per key)
Multimap
map = ArrayListMultimap.create();
map.put("key", 1);
map.put("key", 2);
System.out.println(map); // {"key":[1,2]}
Map
> asMap = map.asMap();3.2.2 BiMap (bijective map)
BiMap
biMap = HashBiMap.create();
biMap.put("key", "value");
System.out.println(biMap); // {"key":"value"}
BiMap
inverse = biMap.inverse();
System.out.println(inverse); // {"value":"key"}3.2.3 Table (two‑dimensional map)
Table
table = HashBasedTable.create();
table.put(18, "男", "yideng");
table.put(18, "女", "Lily");
System.out.println(table.get(18, "男")); // yideng
Map
row = table.row(18);
System.out.println(row); // {"男":"yideng","女":"Lily"}3.2.4 Multiset (set with element counts)
Multiset
multiset = HashMultiset.create();
multiset.add("apple");
multiset.add("apple");
multiset.add("orange");
System.out.println(multiset.count("apple")); // 2
Set
distinct = multiset.elementSet();
System.out.println(distinct); // ["orange","apple"]The article concludes with a brief invitation to share and join a community of architects for further learning.
Java Architect Essentials
Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.
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.