High-performance immutable collection implementations providing List, Set, and Map variants with efficient copy-on-write semantics and builder patterns. These collections are designed for zero-copy operations and thread safety while maintaining familiar Java Collections Framework interfaces.
Factory methods and implementations for immutable List collections.
/**
* Factory for immutable List implementations
*/
public class Lists {
/**
* Create empty immutable list
* @return Empty immutable list
*/
public static <E> List<E> of();
/**
* Create immutable list with single element
* @param e1 First element
* @return Immutable list containing one element
*/
public static <E> List<E> of(E e1);
/**
* Create immutable list with two elements
* @param e1 First element
* @param e2 Second element
* @return Immutable list containing two elements
*/
public static <E> List<E> of(E e1, E e2);
/**
* Create immutable list with three elements
* @param e1 First element
* @param e2 Second element
* @param e3 Third element
* @return Immutable list containing three elements
*/
public static <E> List<E> of(E e1, E e2, E e3);
/**
* Create immutable list with four elements
* @param e1 First element
* @param e2 Second element
* @param e3 Third element
* @param e4 Fourth element
* @return Immutable list containing four elements
*/
public static <E> List<E> of(E e1, E e2, E e3, E e4);
/**
* Create immutable list with five elements
* @param e1 First element
* @param e2 Second element
* @param e3 Third element
* @param e4 Fourth element
* @param e5 Fifth element
* @return Immutable list containing five elements
*/
public static <E> List<E> of(E e1, E e2, E e3, E e4, E e5);
/**
* Create immutable list with six elements
* @param e1 First element
* @param e2 Second element
* @param e3 Third element
* @param e4 Fourth element
* @param e5 Fifth element
* @param e6 Sixth element
* @return Immutable list containing six elements
*/
public static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6);
/**
* Create immutable list with seven elements
* @param e1 First element
* @param e2 Second element
* @param e3 Third element
* @param e4 Fourth element
* @param e5 Fifth element
* @param e6 Sixth element
* @param e7 Seventh element
* @return Immutable list containing seven elements
*/
public static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7);
/**
* Create immutable list with eight elements
* @param e1 First element
* @param e2 Second element
* @param e3 Third element
* @param e4 Fourth element
* @param e5 Fifth element
* @param e6 Sixth element
* @param e7 Seventh element
* @param e8 Eighth element
* @return Immutable list containing eight elements
*/
public static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8);
/**
* Create immutable list with nine elements
* @param e1 First element
* @param e2 Second element
* @param e3 Third element
* @param e4 Fourth element
* @param e5 Fifth element
* @param e6 Sixth element
* @param e7 Seventh element
* @param e8 Eighth element
* @param e9 Ninth element
* @return Immutable list containing nine elements
*/
public static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9);
/**
* Create immutable list with ten elements
* @param e1 First element
* @param e2 Second element
* @param e3 Third element
* @param e4 Fourth element
* @param e5 Fifth element
* @param e6 Sixth element
* @param e7 Seventh element
* @param e8 Eighth element
* @param e9 Ninth element
* @param e10 Tenth element
* @return Immutable list containing ten elements
*/
public static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10);
/**
* Create immutable list from varargs
* @param elements Elements to include in list
* @return Immutable list containing all elements
*/
public static <E> List<E> of(E... elements);
/**
* Create immutable list by copying from collection
* @param collection Source collection to copy
* @return Immutable list containing all elements from collection
*/
public static <E> List<E> copyOf(Collection<? extends E> collection);
/**
* Create collector for immutable lists
* @return Collector that produces immutable lists
*/
public static <E> Collector<E, ?, List<E>> toList();
}Usage Examples:
import aQute.bnd.unmodifiable.Lists;
import java.util.stream.Stream;
// Create with factory methods
List<String> empty = Lists.of();
List<String> single = Lists.of("hello");
List<String> multiple = Lists.of("a", "b", "c", "d");
// Copy from existing collection
List<String> original = Arrays.asList("x", "y", "z");
List<String> immutable = Lists.copyOf(original);
// Use with streams
List<Integer> squares = Stream.of(1, 2, 3, 4, 5)
.map(x -> x * x)
.collect(Lists.toList());
// Immutable lists throw UnsupportedOperationException on modification
try {
immutable.add("new"); // Throws exception
} catch (UnsupportedOperationException e) {
System.out.println("Cannot modify immutable list");
}Factory methods and implementations for immutable Set collections.
/**
* Factory for immutable Set implementations
*/
public class Sets {
/**
* Create empty immutable set
* @return Empty immutable set
*/
public static <E> Set<E> of();
/**
* Create immutable set with single element
* @param e1 First element
* @return Immutable set containing one element
*/
public static <E> Set<E> of(E e1);
/**
* Create immutable set with two elements
* @param e1 First element
* @param e2 Second element
* @return Immutable set containing two elements
*/
public static <E> Set<E> of(E e1, E e2);
/**
* Create immutable set with three elements
* @param e1 First element
* @param e2 Second element
* @param e3 Third element
* @return Immutable set containing three elements
*/
public static <E> Set<E> of(E e1, E e2, E e3);
/**
* Create immutable set with four elements
* @param e1 First element
* @param e2 Second element
* @param e3 Third element
* @param e4 Fourth element
* @return Immutable set containing four elements
*/
public static <E> Set<E> of(E e1, E e2, E e3, E e4);
/**
* Create immutable set with five elements
* @param e1 First element
* @param e2 Second element
* @param e3 Third element
* @param e4 Fourth element
* @param e5 Fifth element
* @return Immutable set containing five elements
*/
public static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5);
/**
* Create immutable set with six elements
* @param e1 First element
* @param e2 Second element
* @param e3 Third element
* @param e4 Fourth element
* @param e5 Fifth element
* @param e6 Sixth element
* @return Immutable set containing six elements
*/
public static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5, E e6);
/**
* Create immutable set with seven elements
* @param e1 First element
* @param e2 Second element
* @param e3 Third element
* @param e4 Fourth element
* @param e5 Fifth element
* @param e6 Sixth element
* @param e7 Seventh element
* @return Immutable set containing seven elements
*/
public static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7);
/**
* Create immutable set with eight elements
* @param e1 First element
* @param e2 Second element
* @param e3 Third element
* @param e4 Fourth element
* @param e5 Fifth element
* @param e6 Sixth element
* @param e7 Seventh element
* @param e8 Eighth element
* @return Immutable set containing eight elements
*/
public static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8);
/**
* Create immutable set with nine elements
* @param e1 First element
* @param e2 Second element
* @param e3 Third element
* @param e4 Fourth element
* @param e5 Fifth element
* @param e6 Sixth element
* @param e7 Seventh element
* @param e8 Eighth element
* @param e9 Ninth element
* @return Immutable set containing nine elements
*/
public static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9);
/**
* Create immutable set with ten elements
* @param e1 First element
* @param e2 Second element
* @param e3 Third element
* @param e4 Fourth element
* @param e5 Fifth element
* @param e6 Sixth element
* @param e7 Seventh element
* @param e8 Eighth element
* @param e9 Ninth element
* @param e10 Tenth element
* @return Immutable set containing ten elements
*/
public static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10);
/**
* Create immutable set from varargs
* @param elements Elements to include in set
* @return Immutable set containing all unique elements
*/
public static <E> Set<E> of(E... elements);
/**
* Create immutable set by copying from collection
* @param collection Source collection to copy
* @return Immutable set containing all unique elements from collection
*/
public static <E> Set<E> copyOf(Collection<? extends E> collection);
/**
* Create collector for immutable sets
* @return Collector that produces immutable sets
*/
public static <E> Collector<E, ?, Set<E>> toSet();
}Factory methods and implementations for immutable Map collections.
/**
* Factory for immutable Map implementations
*/
public class Maps {
/**
* Create empty immutable map
* @return Empty immutable map
*/
public static <K, V> Map<K, V> of();
/**
* Create immutable map with single key-value pair
* @param k1 First key
* @param v1 First value
* @return Immutable map containing one entry
*/
public static <K, V> Map<K, V> of(K k1, V v1);
/**
* Create immutable map with two key-value pairs
* @param k1 First key
* @param v1 First value
* @param k2 Second key
* @param v2 Second value
* @return Immutable map containing two entries
*/
public static <K, V> Map<K, V> of(K k1, V v1, K k2, V v2);
/**
* Create immutable map with three key-value pairs
* @param k1 First key
* @param v1 First value
* @param k2 Second key
* @param v2 Second value
* @param k3 Third key
* @param v3 Third value
* @return Immutable map containing three entries
*/
public static <K, V> Map<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3);
/**
* Create immutable map with four key-value pairs
* @param k1 First key
* @param v1 First value
* @param k2 Second key
* @param v2 Second value
* @param k3 Third key
* @param v3 Third value
* @param k4 Fourth key
* @param v4 Fourth value
* @return Immutable map containing four entries
*/
public static <K, V> Map<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4);
/**
* Create immutable map with five key-value pairs
* @param k1 First key
* @param v1 First value
* @param k2 Second key
* @param v2 Second value
* @param k3 Third key
* @param v3 Third value
* @param k4 Fourth key
* @param v4 Fourth value
* @param k5 Fifth key
* @param v5 Fifth value
* @return Immutable map containing five entries
*/
public static <K, V> Map<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5);
/**
* Create immutable map with six key-value pairs
* @param k1 First key
* @param v1 First value
* @param k2 Second key
* @param v2 Second value
* @param k3 Third key
* @param v3 Third value
* @param k4 Fourth key
* @param v4 Fourth value
* @param k5 Fifth key
* @param v5 Fifth value
* @param k6 Sixth key
* @param v6 Sixth value
* @return Immutable map containing six entries
*/
public static <K, V> Map<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6);
/**
* Create immutable map with seven key-value pairs
* @param k1 First key
* @param v1 First value
* @param k2 Second key
* @param v2 Second value
* @param k3 Third key
* @param v3 Third value
* @param k4 Fourth key
* @param v4 Fourth value
* @param k5 Fifth key
* @param v5 Fifth value
* @param k6 Sixth key
* @param v6 Sixth value
* @param k7 Seventh key
* @param v7 Seventh value
* @return Immutable map containing seven entries
*/
public static <K, V> Map<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7);
/**
* Create immutable map with eight key-value pairs
* @param k1 First key
* @param v1 First value
* @param k2 Second key
* @param v2 Second value
* @param k3 Third key
* @param v3 Third value
* @param k4 Fourth key
* @param v4 Fourth value
* @param k5 Fifth key
* @param v5 Fifth value
* @param k6 Sixth key
* @param v6 Sixth value
* @param k7 Seventh key
* @param v7 Seventh value
* @param k8 Eighth key
* @param v8 Eighth value
* @return Immutable map containing eight entries
*/
public static <K, V> Map<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8);
/**
* Create immutable map with nine key-value pairs
* @param k1 First key
* @param v1 First value
* @param k2 Second key
* @param v2 Second value
* @param k3 Third key
* @param v3 Third value
* @param k4 Fourth key
* @param v4 Fourth value
* @param k5 Fifth key
* @param v5 Fifth value
* @param k6 Sixth key
* @param v6 Sixth value
* @param k7 Seventh key
* @param v7 Seventh value
* @param k8 Eighth key
* @param v8 Eighth value
* @param k9 Ninth key
* @param v9 Ninth value
* @return Immutable map containing nine entries
*/
public static <K, V> Map<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9);
/**
* Create immutable map with ten key-value pairs
* @param k1 First key
* @param v1 First value
* @param k2 Second key
* @param v2 Second value
* @param k3 Third key
* @param v3 Third value
* @param k4 Fourth key
* @param v4 Fourth value
* @param k5 Fifth key
* @param v5 Fifth value
* @param k6 Sixth key
* @param v6 Sixth value
* @param k7 Seventh key
* @param v7 Seventh value
* @param k8 Eighth key
* @param v8 Eighth value
* @param k9 Ninth key
* @param v9 Ninth value
* @param k10 Tenth key
* @param v10 Tenth value
* @return Immutable map containing ten entries
*/
public static <K, V> Map<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9, K k10, V v10);
/**
* Create immutable map from Entry varargs
* @param entries Entries to include in map
* @return Immutable map containing all entries
*/
public static <K, V> Map<K, V> ofEntries(Entry<? extends K, ? extends V>... entries);
/**
* Create immutable map by copying from existing map
* @param map Source map to copy
* @return Immutable map containing all entries from source map
*/
public static <K, V> Map<K, V> copyOf(Map<? extends K, ? extends V> map);
/**
* Create Entry instance for use with ofEntries
* @param key Entry key
* @param value Entry value
* @return Map Entry instance
*/
public static <K, V> Entry<K, V> entry(K key, V value);
/**
* Create collector for immutable maps
* @return Collector that produces immutable maps
*/
public static <K, V> Collector<Entry<? extends K, ? extends V>, ?, Map<K, V>> toMap();
}Usage Examples:
import aQute.bnd.unmodifiable.Maps;
import aQute.bnd.unmodifiable.Sets;
import java.util.stream.Stream;
// Create with factory methods
Map<String, Integer> empty = Maps.of();
Map<String, Integer> single = Maps.of("one", 1);
Map<String, Integer> multiple = Maps.of(
"one", 1,
"two", 2,
"three", 3
);
// Create with entries
Map<String, Integer> fromEntries = Maps.ofEntries(
Maps.entry("apple", 5),
Maps.entry("banana", 3),
Maps.entry("orange", 8)
);
// Copy from existing map
Map<String, Integer> original = new HashMap<>();
original.put("a", 1);
original.put("b", 2);
Map<String, Integer> immutable = Maps.copyOf(original);
// Use with streams
Map<String, Integer> lengths = Stream.of("hello", "world", "java")
.collect(Collectors.toMap(
s -> s,
String::length,
(a, b) -> a,
() -> new HashMap<>()
));
// Create immutable sets
Set<String> colors = Sets.of("red", "green", "blue");
Set<Integer> numbers = Sets.copyOf(Arrays.asList(1, 2, 3, 2, 1)); // Duplicates removed
// All collections are truly immutable
try {
immutable.put("c", 3); // Throws UnsupportedOperationException
} catch (UnsupportedOperationException e) {
System.out.println("Cannot modify immutable map");
}All immutable collections provide the following guarantees:
UnsupportedOperationExceptionNullPointerExceptionConcurrentModificationExceptionPerformance Characteristics: