Singleton vs Single-Element List Performance
You'll learn why singleton collections are more efficient than creating regular single-element collections.
The Core Difference
When you need a collection with just one element, you have two options:
List<String> regular = new ArrayList<>();
regular.add("value");
List<String> singleton = Collections.singletonList("value");
Both give you a list with one element, but they work very differently under the hood.
Memory Characteristics
A regular ArrayList with one element allocates:
- An internal array (default capacity 10 elements)
- Object metadata for the list itself
- Room to grow
A singleton collection from Collections.singletonList():
- Stores only a reference to your single element
- Uses a lightweight, fixed-size wrapper
- Cannot grow (it's immutable)
Think of it like packaging: A regular list is like a storage unit with 10 slots even if you only use one. A singleton is like a custom-fit box for exactly one item.
Performance Characteristics
Memory: Singleton collections use significantly less memory because they don't pre-allocate array space.
Speed: Singleton collections are slightly faster for creation and access because there's no array initialization or bounds checking overhead.
Iteration: Both perform similarly, but singletons skip unnecessary checks since the size is always exactly one.
When to Use Each
Use Collections.singletonList() when:
- You have exactly one element and it will never change
- You want to pass an immutable single-item collection to a method
- Memory efficiency matters (especially with many small collections)
Use regular lists when you might need to add more elements later.
Key Takeaway: Singleton collections are optimized for the one-element case with lower memory footprint and faster creation than regular single-element lists, but they're immutable.