What it is
FastUtil is a Java library that provides fast and memory-efficient collections for primitive types, including maps, sets, lists, and more. It offers specialized implementations that avoid the overhead of boxing/unboxing primitive types, improving performance in high-throughput applications.
FastUtil provides collections optimized for primitive types (int, long, double, etc.), including maps, sets, lists, and priority queues. It supports fast iteration, bulk operations, and interoperability with standard Java collections.
Installation
Add dependency in pom.xml:
<dependency>
<groupId>it.unimi.dsi</groupId>
<artifactId>fastutil</artifactId>
<version>8.6.1</version>
</dependency>Getting started
The smallest useful thing you can do with it, and what each part means.
import it.unimi.dsi.fastutil.ints.IntArrayList;
IntArrayList list = new IntArrayList();
list.add(10);
list.add(20);
System.out.println(list);import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
Int2ObjectOpenHashMap<String> map = new Int2ObjectOpenHashMap<>();
map.put(1, "one");
map.put(2, "two");
System.out.println(map.get(1));Advanced usage
Where the library earns its place over a simpler alternative.
IntArrayList list = new IntArrayList(new int[]{1,2,3,4});
for(int value : list) {
System.out.println(value);
}import it.unimi.dsi.fastutil.longs.Long2LongOpenHashMap;
Long2LongOpenHashMap map = new Long2LongOpenHashMap();
map.put(100L, 1000L);
map.put(200L, 2000L);
System.out.println(map.get(100L));import it.unimi.dsi.fastutil.ints.IntArrays;
int[] arr = {5,2,9,1};
IntArrays.quickSort(arr);
System.out.println(Arrays.toString(arr));import it.unimi.dsi.fastutil.ints.IntArrayList;
IntArrayList list = new IntArrayList(new int[]{1,2,3});
List<Integer> javaList = list; // auto-boxed
System.out.println(javaList);Errors and fixes
The failures you are most likely to hit, and what actually resolves them.
- NullPointerException
- FastUtil collections for primitives do not accept nulls. Ensure primitive arrays or default values are used.
- IndexOutOfBoundsException
- Occurs when accessing invalid indexes in lists or arrays. Check bounds carefully.
Best practices
- Use FastUtil collections for large datasets with primitive types to save memory and improve performance.
- Prefer primitive-specialized maps and sets to avoid boxing overhead.
- Use built-in iteration and bulk operations for high-performance tasks.
- Combine FastUtil with standard Java collections where interoperability is needed.
- Keep the library updated to leverage performance improvements and bug fixes.
Background
Why it exists, and what it was reacting to.
FastUtil was developed to address the performance limitations of standard Java collections when working with primitive types. By providing specialized collections, FastUtil reduces memory footprint and increases speed, making it popular in scenarios such as numerical computing, large datasets, and performance-critical systems.
