Lesser‑Known but Practical Features of Google Guava
This article explores several under‑used yet useful Google Guava utilities—including unsigned primitive types, 128‑bit MurmurHash, InternetDomainName parsing, class‑path reflection, and CharMatcher string handling—providing code examples and explanations of how they can improve Java development efficiency.
Google Guava is one of the most popular open‑source Java libraries, offering many core utilities that have been battle‑tested in production; beyond its well‑known collections and caching APIs, it provides a range of lesser‑known features that can boost developer productivity.
1. Unsigned Primitive Types: They Really Exist!
Guava supplies unsigned integer helpers that work on Java 6 and later, allowing you to treat primitive ints as unsigned values. Example code shows parsing a maximum unsigned int and converting it back to a string, as well as using the UnsignedInteger wrapper to perform arithmetic without the pitfalls of raw primitives.
int notReallyInt = UnsignedInts.parseUnsignedInt(4294967295); // Max unsigned int
String maxUnsigned = UnsignedInts.toString(notReallyInt); // We’re legit!
UnsignedInteger newType = UnsignedInteger.valueOf(maxUnsigned);
newType = newType.plus(UnsignedInteger.valueOf("1")); // IncrementBoth UnsignedInts and UnsignedLongs also provide comparison, division, min, max, and other arithmetic operations.
2. Hashing: 128‑bit MurmurHash
Guava implements the fast, non‑cryptographic MurmurHash algorithm (both 128‑bit and 32‑bit versions). The example demonstrates creating a HashFunction , feeding a long, a string, and a custom object via a Funnel , and obtaining a HashCode .
HashFunction hf = Hashing.murmur3_128();
HashCode hc = hf.newHasher()
.putLong(id)
.putString(name, Charsets.UTF_8)
.putObject(person, personFunnel)
.hash();A Funnel defines how to decompose an object into primitive components for hashing; the article shows a simple implementation for a Person object.
Funnel
personFunnel = new Funnel
() {
@Override
public void funnel(Person person, PrimitiveSink into) {
into.putInt(person.id)
.putString(person.firstName, Charsets.UTF_8)
.putString(person.lastName, Charsets.UTF_8)
.putInt(birthYear);
}
};3. InternetDomainName: Replace Your Own Domain Validator
Guava’s InternetDomainName parses and validates domain names according to the Public Suffix List. The example shows extracting the top‑private domain from blog.takipi.com and checking the validity of a custom domain.
InternetDomainName owner = InternetDomainName.from("blog.takipi.com").topPrivateDomain(); // returns takipi.com
boolean ok = InternetDomainName.isValid("takipi.monsters"); // returns falseThe article explains the difference between publicSuffix() and topPrivateDomain() and notes practical uses such as validating JIRA hostnames before integration.
4. ClassPath Reflection: Magic Mirror
Guava’s ClassPath utility can scan a given package and list all top‑level classes, simplifying class‑path inspection. The sample code iterates over classes in com.mycomp.mypackage and prints their names.
ClassPath classpath = ClassPath.from(classloader);
for (ClassPath.ClassInfo classInfo : classpath.getTopLevelClasses("com.mycomp.mypackage")) {
System.out.println(classInfo.getName());
}It warns that only classes physically present under the specified package are discovered; classes loaded from elsewhere will be omitted.
5. CharMatcher: A Simplified Regex?
Guava’s CharMatcher provides a fluent API for common string‑manipulation tasks such as trimming, collapsing whitespace, or retaining only specific characters. The examples illustrate collapsing whitespace and retaining characters that form the word “alex”.
String spaced = CharMatcher.WHITESPACE.trimAndCollapseFrom(string, ' ');
String keepAlex = CharMatcher.anyOf("alex").retainFrom(someOtherString);These utilities make string cleaning concise and readable compared to traditional regular‑expression approaches.
Conclusion
The article highlighted several interesting Guava features—unsigned primitives, MurmurHash, domain name handling, class‑path reflection, and CharMatcher—that are not as widely known as the library’s collections and caching APIs. These utilities are used extensively at Takipi and can benefit many Java projects by making code cleaner and more efficient.
Qunar Tech Salon
Qunar Tech Salon is a learning and exchange platform for Qunar engineers and industry peers. We share cutting-edge technology trends and topics, providing a free platform for mid-to-senior technical professionals to exchange and learn.
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.