Using Guava, Apache Commons, and Other Libraries for Enhanced Collections

Illustration for Using Guava, Apache Commons, and Other Libraries for Enhanced Collections
By Last updated:

While the Java Collections Framework provides a solid foundation for data structures like lists, maps, and sets, it often lacks advanced utilities that developers encounter in real-world applications. Libraries like Guava and Apache Commons Collections extend Java's core collections with powerful abstractions such as Multimap, BiMap, ListUtils, and more.

This tutorial dives into how third-party libraries enhance the collections ecosystem and boost developer productivity with cleaner, safer, and more expressive code.


Core Concepts

Third-party libraries supplement the limitations of standard collections. These enhancements include:

  • Simplified collection creation and transformations
  • Advanced data structures (e.g., Multimap, BiMap, Trie)
  • Utility functions for sorting, partitioning, and filtering
  • Improved immutability and performance

Guava Collections Overview

Google Guava is a powerful library that enhances collections with:

  • Multimap: Map that allows multiple values per key
  • BiMap: Map with unique values and reverse lookup
  • ImmutableList, ImmutableMap: Safer alternatives to standard collections
  • Lists.partition, Sets.cartesianProduct, etc.
Multimap<String, String> multimap = ArrayListMultimap.create();
multimap.put("fruit", "apple");
multimap.put("fruit", "banana");

Apache Commons Collections Overview

Apache Commons Collections is a mature library offering:

  • MultiValuedMap
  • ListValuedMap
  • Bag and BidiMap
  • Transformers, Predicates, and functors (pre-Java 8 functional utils)
MultiValuedMap<String, String> map = new ArrayListValuedHashMap<>();
map.put("city", "Paris");
map.put("city", "London");

Code Examples and Use Cases

Guava: Partitioning a List

List<List<Integer>> partitions = Lists.partition(Arrays.asList(1, 2, 3, 4, 5), 2);

Apache Commons: Bidirectional Map

BidiMap<Integer, String> bidi = new DualHashBidiMap<>();
bidi.put(1, "One");
String value = bidi.get(1);
int key = bidi.getKey("One");

Comparison with Java Standard Collections

Feature Java Collections Guava Apache Commons
Multimap
Bidirectional Map ✅ (BiMap) ✅ (BidiMap)
Immutable Collections Java 9+ only Limited
List Partitioning Manual

Performance Insights

  • Guava collections often offer better memory efficiency due to internal optimizations.
  • Apache Commons provides compatibility and support for older Java versions.

Functional Programming Support

While Apache Commons used pre-Java 8 functional interfaces, Guava integrates cleanly with lambdas, predicates, and streams.

Predicate<String> isNotEmpty = s -> !s.isEmpty();

Version Differences in Java

  • Java 8 brought functional programming, reducing the need for Commons Functor.
  • Java 9+ introduced immutable collections via List.of, competing with Guava’s immutables.
  • Java 21 provides better runtime performance for generics-heavy structures.

Pros and Cons

Guava

✅ Feature-rich
✅ Fluent API
❌ Slightly larger dependency footprint

Apache Commons

✅ Legacy support
✅ Intuitive API
❌ Verbose for some tasks


Anti-Patterns and Fixes

  • ❌ Using HashMap with lists of values instead of Multimap
  • ✅ Replace with Multimap for cleaner code

Legacy Refactoring Tips

Before (verbose):

Map<String, List<String>> map = new HashMap<>();
map.computeIfAbsent("key", k -> new ArrayList<>()).add("value");

After (cleaner):

Multimap<String, String> map = ArrayListMultimap.create();
map.put("key", "value");

Best Practices

  • Use Guava for modern, immutable, and functional designs
  • Use Apache Commons for backwards compatibility in legacy projects
  • Don’t reinvent collection transformations already provided by libraries

📌 What's New in Java Versions?

  • Java 8
    • Lambdas, Streams, Collectors, Optional
  • Java 9
    • List.of(), Map.of(), compact immutable collection factories
  • Java 10
    • var keyword for type inference
  • Java 21
    • Performance improvements for HashMap, better memory layout for collections

Conclusion and Key Takeaways

Third-party libraries like Guava and Apache Commons Collections empower Java developers to write more expressive, cleaner, and efficient code. They bridge gaps in the standard library and help teams maintain concise and robust codebases.


FAQ

1. What is a Multimap?

A data structure that maps keys to multiple values.

2. How is BiMap different from a regular Map?

It allows reverse lookup of keys by values.

3. Can I mix Guava with standard Java Collections?

Yes, Guava is fully interoperable with Java collections.

4. Which library is more modern?

Guava is more aligned with Java 8+ features.

5. Are these libraries production-ready?

Yes, both are battle-tested and used in enterprise projects.

6. Is Guava supported in Android?

Yes, but use the Android-compatible subset (guava-android).

7. How do I choose between Apache Commons and Guava?

Use Guava for new projects; Apache Commons for legacy or compatibility needs.

8. What about performance?

Guava is optimized, but always benchmark for your use case.

9. Do these libraries support serialization?

Yes, but read documentation for caveats.

10. Are these libraries maintained?

Yes, both have active communities and GitHub repositories.