-
Notifications
You must be signed in to change notification settings - Fork 4
Old Collections Overview
JImmutable Collections contains a variety of fully immutable collections designed to suit different needs in an application. This page describes the various collections (as of version 1.9) and their general performance characteristics. For each collection the summary lists the JImmutables factory method used to create an instance, the general algorithmic complexity of the collection, and a description of the collection's uses and characteristics.
The complexity is expressed in big-oh notation which means "on the order of" or "within some constant multiple of" the value in parentheses. logX(n) means log to the base X of the number of elements in the collection. To give an idea of scale log32(100000) is 3.3 while log2(100000) is 16.6. "Within some constant factor" means that there will be fixed costs associated with the implementation so one algorithm with a given big-oh complexity might be 2-3 times faster than another with the same complexity.
Generally speaking ArrayList will be faster than JImmutableList unless insertion or deletion in mid-list is required since it simply updates a mutable array but for most algorithms the difference will be negligible to overall run time. JImmutableMaps are generally comparable in performance (i.e. within acceptable limits for most algorithms) to java.util.Maps. See the [Old Comparative Performance] page for an idea of how the immutable maps and arrays compare to java.util.Maps.
Factory | Complexity | Description |
---|---|---|
JImmutables.array() | O(log32(n)) | A sparse array allowing any integer (positive or negative) as index. Indexes do not need to be consecutive and the array allocates memory only for indexes actually inserted into the array. Implemented internally as an integer trie with 32-way branching. Iterators and Streams visit elements in order by index with negative indexes visited before positive indexes. No direct java.util analog but similar to a TreeMap but with better performance. |
JImmutables.map() | O(log32(n)) | A hash map using hashCode() and equals() methods of keys. Keys and values are stored in hash mapped array tries using linked lists or binary trees for collision handling (two keys having same hash code). Performance scales roughly linearly with size of the collection since depth of the trie never exceeds 7 levels. Intended as a replacement for java.util.HashMap. Iterators and Streams visit elements in an unspecified order. |
JImmutables.sortedMap() | O(log2(n)) | A tree map using a Comparator object to sort and compare keys. Keys and values are stored in binary trees. Intended as a replacement for java.util.TreeMap. Iterators and Streams visit elements in sort order of keys as specified by the Comparator. |
JImmutables.insertOrderMap() | O(2 * log32(n)) | A hash map using hashCode() and equals() methods of keys. Keys and values are stored in hash mapped array tries using linked lists or binary trees for collision handling (two keys having same hash code). A second integer trie is also used to govern iterator order. Performance scales roughly linearly with size of the collection since depth of the trie never exceeds 7 levels. Perhaps as much as twice the overhead of a map() since it maintains two data structures internally. Intended as a replacement for java.util.LinkedHashMap. Iterators and Streams visit elements in the order they were originally inserted into the map. |
JImmutables.stack() | O(1) inserts/deletes at head, O(n) searches | A singly linked linear list of elements maintained in reverse insertion order. Extremely fast inserts and deletes from the head but searches require looping through all elements to find a match. Does not support insertion or deletion inside the list. Useful as a stack or a fast temporary list of items where LIFO order is acceptable. Iterators and Streams visit elements in LIFO (last in first out) order. |
JImmutables.list() | O(log32(n)) | A "list" implemented internally as a balanced binary tree with leaf nodes containing up to 64 values each. Allows elements to be inserted or removed anywhere in the list (prior to 3.0.0 only at either end of the list). Allows elements to be replaced anywhere within the list. Intended as a replacement for java.util.List. Iterators and Streams visit elements in order by index. |
JImmutables.ralist() | O(log2(n)) | (Only in version prior to 3.0.0) A "list" implemented internally as a B-tree. Allows elements to be inserted, removed, and updated anywhere within the list. Intended as a replacement for java.util.List in algorithms that require insertion or removal from the middle of the list. Otherwise use list(). Iterators and Streams visit elements in order by index. |
JImmutables.set() | O(log32(n)) | A set implemented internally using a hash map. Keys are stored in hash mapped array tries using hashCode() and equals() methods to compare keys. Intended as a replacement for HashSet. Iterators and Streams visit elements in an unspecified order. |
JImmutables.sortedSet() | O(log2(n)) | A set implemented internally using a binary tree. Keys are compared using a Comparator. Intended as a replacement for TreeSet. Iterators and Streams visit elements in sort order of keys as specified by the Comparator. |
JImmutables.insertOrderSet() | O(2 * log32(n)) | A set implemented internally using an integer trie for sort order and a hash mapped trie for searching. Performance scales roughly linearly with size of the collection since depth of the trie never exceeds 7 levels. Perhaps as much as twice the overhead of a set() since it maintains two data structures internally. Intended as a replacement for java.util.LinkedHashSet. Iterators and Streams visit elements in the order they were originally inserted into the map. |
JImmutables.listMap() | O(log32(n)) | A hash map mapping keys to JImmutableLists. Performance similar to JImmutables.map(). Iterators and Streams visit elements in an unspecified order. |
JImmutables.sortedListMap() | O(log2(n)) | A sorted map mapping keys to JImmutableLists. Performance similar to JImmutables.sortedMap(). Iterators and Streams visit elements in sort order of keys as specified by the Comparator. |
JImmutables.insertOrderListMap() | O(2 * log32(n)) | A sorted map mapping keys to JImmutableLists. Performance similar to JImmutables.insertOrderMap(). Iterators and Streams visit elements in the order they were originally inserted into the map. |
JImmutables.setMap() | O(log32(n)) | A hash map mapping keys to JImmutableSets. Performance similar to JImmutables.map(). Iterators and Streams visit elements in an unspecified order. Sets are all equivalent to one created by JImmutables.set(). |
JImmutables.sortedSetMap() | O(log2(n)) | A sorted map mapping keys to JImmutableSets. Performance similar to JImmutables.sortedMap(). Iterators and Streams visit elements in sort order of keys as specified by the Comparator. Sets are all equivalent to one created by JImmutables.set(). |
JImmutables.insertOrderSetMap() | O(2 * log32(n)) | A sorted map mapping keys to JImmutableSets. Performance similar to JImmutables.insertOrderMap(). Iterators and Streams visit elements in the order they were originally inserted into the map. Sets are all equivalent to one created by JImmutables.set(). |
JImmutables.multiset() | O(log32(n)) | A multiset implemented internally using a hash map. Keys are stored in hash mapped array tries using hashCode() and equals() methods to compare keys. Iterators and Streams visit elements in an unspecified order. |
JImmutables.sortedMultiset() | O(log2(n)) | A multiset implemented internally using a binary tree. Keys are compared using a Comparator. Iterators and Streams visit elements in sort order of keys as specified by the Comparator. |
JImmutables.insertOrderMultiset() | O(2 * log32(n)) | A multiset implemented internally using an integer trie for sort order and a hash mapped trie for searching. Performance scales roughly linearly with size of the collection since depth of the trie never exceeds 7 levels. Perhaps as much as twice the overhead of a multiset() since it maintains two data structures internally. Iterators and Streams visit elements in the order they were originally inserted into the map. |