Java's standard Collections Framework provides a robust set of tools for managing data structures like Lists, Sets, and Maps. However, the ecosystem has expanded with various third-party libraries that provide enhanced functionality, specialized data structures, and optimized performance. This article introduces some of the most popular Java collections libraries, including Google Guava, Apache Commons Collections, Eclipse Collections, and others, each offering unique advantages and use cases. We’ll explore their key features, licensing, and "Hello World" examples to help you choose the right tool for your Java project.
1. Google Guava
Google Guava is one of the most widely used libraries in the Java ecosystem. It offers extensions to the JDK's collections framework, as well as additional utilities such as caching, concurrency utilities, string processing, and more.
- Key Features: Collections, caching, primitives support, concurrency libraries, common annotations, string processing, and I/O utilities.
- License: Apache 2
- JDK Requirement: Requires JDK 1.6 or higher
- Business-Friendly License: Yes
Example: Hello World using ImmutableList
in Guava
import com.google.common.collect.ImmutableList;
public class GuavaExample {
public static void main(String[] args) {
ImmutableList<String> fruits = ImmutableList.of("Apple", "Banana", "Orange");
System.out.println(fruits);
}
}
2. Apache Commons Collections
Apache Commons Collections enhances Java’s standard collections by adding new interfaces, implementations, and utilities. It aims to improve developer productivity by providing highly reusable collection types.
- Key Features: Extensions to JDK collections, additional utility classes, and new collection types.
- License: Apache 2
- Business-Friendly License: Yes
Example: Hello World with Bag
in Apache Commons Collections
import org.apache.commons.collections4.Bag;
import org.apache.commons.collections4.bag.HashBag;
public class CommonsCollectionsExample {
public static void main(String[] args) {
Bag<String> bag = new HashBag<>();
bag.add("Apple", 3);
bag.add("Banana", 2);
System.out.println(bag);
}
}
3. Eclipse Collections
Eclipse Collections (formerly GS Collections) is a comprehensive collections framework for Java, offering JDK-compatible collection types such as Lists, Sets, and Maps. It also introduces additional types like Bags and Multimaps, with a rich API for functional-style programming.
- Key Features: Rich API, additional types (e.g., Bags, Multimaps), JDK-compatible Lists, Sets, and Maps.
- License: Eclipse Public License 1.0
- Business-Friendly License: Yes
Example: Hello World using MutableList
in Eclipse Collections
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.impl.factory.Lists;
public class EclipseCollectionsExample {
public static void main(String[] args) {
MutableList<String> fruits = Lists.mutable.of("Apple", "Banana", "Cherry");
System.out.println(fruits);
}
}
4. Javatuples
Javatuples is a simple yet powerful library that provides classes for working with tuples, which are sequences of objects of different types. It supports pairs, triplets, and other tuple types.
- Key Features: Simple tuple classes for storing heterogeneous elements.
- License: Apache 2
- Business-Friendly License: Yes
Example: Hello World using Triplet
in Javatuples
import org.javatuples.Triplet;
public class JavatuplesExample {
public static void main(String[] args) {
Triplet<Integer, String, Double> triplet = new Triplet<>(1, "Apple", 2.5);
System.out.println(triplet);
}
}
5. Immutables
Immutables provides annotation processors that automatically generate immutable value objects. It emphasizes simplicity, safety, and consistency by eliminating boilerplate code.
- Key Features: Generates immutable classes from annotations, reduces boilerplate code.
- License: Apache 2
- Business-Friendly License: Yes
Example: Hello World using Immutables
import org.immutables.value.Value;
public class ImmutablesExample {
@Value.Immutable
public interface Fruit {
String name();
int quantity();
}
public static void main(String[] args) {
Fruit fruit = ImmutableFruit.builder().name("Apple").quantity(5).build();
System.out.println(fruit);
}
}
6. OpenHFT Chronicle Queue
OpenHFT Chronicle Queue is a high-performance, microsecond-latency messaging system that persists everything to disk, designed for high-throughput use cases.
- Key Features: High-speed messaging with disk persistence.
- License: GNU Lesser 3.0
- Business-Friendly License: Yes
7. Fastutil
Fastutil extends the Java Collections Framework by providing type-specific maps, sets, and lists, with additional support for big (64-bit) data structures.
- Key Features: Type-specific collections, big data support, optimized performance.
- License: Apache 2
- Business-Friendly License: Yes
8. HPPC (High-Performance Primitive Collections)
HPPC focuses on providing optimized collections for primitive types, aiming for the highest performance and memory efficiency.
- Key Features: Collections optimized for Java primitive types, memory-efficient structures.
- License: Apache 2
- Business-Friendly License: Yes
9. Koloboke
Koloboke is designed for efficiency, providing Java collections that maximize memory and performance. It offers primitive-specialized collections with a focus on speed.
- Key Features: Primitive-specialized collections, high performance.
- License: Apache 2
- Business-Friendly License: Yes
10. Trove
Trove provides high-performance collections for both regular and primitive data types, focusing on reducing memory footprint and enhancing processing speed.
- Key Features: High-speed collections for primitive and regular types.
- License: GNU Lesser 2.1
- Business-Friendly License: Yes
11. Underscore-java
Underscore-java is a Java port of the popular Underscore.js library, offering utility functions for functional programming and data manipulation.
- Key Features: Utility functions similar to Underscore.js, functional-style operations.
- License: MIT
- Business-Friendly License: Yes
GoodBye!