null, are permitted.
contains(x), add(x) and remove(x), are all (expected and amortized)
constant time operations. Expected in the hashtable sense (depends on the hash function doing a
good job of distributing the elements to the buckets to a distribution not far from uniform), and
amortized since some operations can trigger a hash table resize.
This implementation consumes significantly less memory than java.util.LinkedHashSet or
even java.util.HashSet, and places considerably less load on the garbage collector. Like
java.util.LinkedHashSet, it offers insertion-order iteration, with identical behavior.
This class should not be assumed to be universally superior to java.util.LinkedHashSet. Generally speaking, this class reduces object allocation and memory
consumption at the price of moderately increased constant factors of CPU. Only use this class
when there is a specific reason to prioritize memory over CPU.
| Modifier and Type | Field and Description |
|---|---|
| private static final int | |
| private transient int | firstEntry
Pointer to the first node in the linked list, or |
| private transient int | lastEntry
Pointer to the last node in the linked list, or |
| private transient int[] | predecessor
Pointer to the predecessor of an entry in insertion order. |
| private transient int[] | successor
Pointer to the successor of an entry in insertion order. |
| Access | Constructor and Description |
|---|---|
| pack-priv | |
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv int | adjustAfterRemove(int indexBeforeRemove, int indexRemoved)
Overrides com. |
| pack-priv int | allocArrays()
Overrides com. |
| public void | clear()
Overrides com. Implements java. |
| pack-priv Set | |
| public static <E extends @Nullable Object> CompactLinkedHashSet | create()
Hides com. CompactLinkedHashSet instance.
|
| public static <E extends @Nullable Object> CompactLinkedHashSet | Returns: a newCompactLinkedHashSet containing those elements (minus duplicates)the elements that the set should contain collection)Hides com. CompactLinkedHashSet instance containing the elements of the
given collection in the order returned by the collection's iterator.
|
| public static <E extends @Nullable Object> CompactLinkedHashSet | Returns: a newCompactLinkedHashSet containing those elements (minus duplicates)the elements that the set should contain elements)Hides com. CompactLinkedHashSet instance containing the given elements in unspecified
order.
|
| public static <E extends @Nullable Object> CompactLinkedHashSet | Returns: a new, emptyCompactLinkedHashSet with enough capacity to hold expectedSize elements without resizingthe number of elements you expect to add to the returned set expectedSize)Hides com. CompactLinkedHashSet instance, with a high enough "initial capacity" that it
should hold expectedSize elements without rebuilding internal data structures.
|
| pack-priv int | |
| private int | |
| pack-priv int | |
| pack-priv void | init(int expectedSize)
Pseudoconstructor for serialization support.
|
| pack-priv void | insertEntry(int entryIndex, E object, int hash, int mask)
Overrides com. |
| pack-priv void | moveLastEntry(int dstIndex, int mask)
Overrides com. dstIndex, and nulls out its old position.
|
| private int[] | |
| private int[] | |
| pack-priv void | resizeEntries(int newCapacity)
Overrides com. |
| private void | |
| private void | |
| private void | |
| public Spliterator | spliterator()
Overrides com. Overrides default java. Spliterator over the elements in this set.
|
| public @Nullable Object[] | toArray()
Overrides com. Implements java. |
| public <T extends @Nullable Object> T[] | toArray(T[]
the array into which the elements of this set are to be
stored, if it is big enough; otherwise, a new array of the same
runtime type is allocated for this purpose. a)Overrides com. Implements java. |
| ENDPOINT | back to summary |
|---|---|
| private static final int ENDPOINT | |
| firstEntry | back to summary |
|---|---|
| private transient int firstEntry Pointer to the first node in the linked list, or | |
| lastEntry | back to summary |
|---|---|
| private transient int lastEntry Pointer to the last node in the linked list, or | |
| predecessor | back to summary |
|---|---|
| private transient int[] predecessor Pointer to the predecessor of an entry in insertion order. ENDPOINT indicates a node is the
first node in insertion order; all values at indices ≥ | |
| successor | back to summary |
|---|---|
| private transient int[] successor Pointer to the successor of an entry in insertion order. ENDPOINT indicates a node is the last
node in insertion order; all values at indices ≥ | |
| CompactLinkedHashSet | back to summary |
|---|---|
| pack-priv CompactLinkedHashSet() | |
| CompactLinkedHashSet | back to summary |
|---|---|
| pack-priv CompactLinkedHashSet(int expectedSize) | |
| adjustAfterRemove | back to summary |
|---|---|
| pack-priv int adjustAfterRemove(int indexBeforeRemove, int indexRemoved) Overrides com. Doc from com. Updates the index an iterator is pointing to after a call to remove: returns the index of the entry that should be looked at after a removal on indexRemoved, with indexBeforeRemove as the index that *was* the next entry that would be looked at.
| |
| allocArrays | back to summary |
|---|---|
| pack-priv int allocArrays() Overrides com. Doc from com. Handle lazy allocation of arrays.
| |
| clear | back to summary |
|---|---|
| public void clear() Overrides com. Implements java. Doc from java. Removes all of the elements from this set (optional operation). The set will be empty after this call returns.
| |
| convertToHashFloodingResistantImplementation | back to summary |
|---|---|
| pack-priv Set Overrides com.
| |
| create | back to summary |
|---|---|
| public static <E extends @Nullable Object> CompactLinkedHashSet Hides com. Creates an empty | |
| create | back to summary |
|---|---|
| public static <E extends @Nullable Object> CompactLinkedHashSet Hides com. Creates a mutable
| |
| create | back to summary |
|---|---|
| public static <E extends @Nullable Object> CompactLinkedHashSet Hides com. Creates a
| |
| createWithExpectedSize | back to summary |
|---|---|
| public static <E extends @Nullable Object> CompactLinkedHashSet Hides com. Creates a
| |
| firstEntryIndex | back to summary |
|---|---|
| pack-priv int firstEntryIndex() Overrides com.
| |
| getPredecessor | back to summary |
|---|---|
| private int getPredecessor(int entry) | |
| getSuccessor | back to summary |
|---|---|
| pack-priv int getSuccessor(int entry) Overrides com.
| |
| init | back to summary |
|---|---|
| pack-priv void init(int expectedSize) Overrides com. Doc from com. Pseudoconstructor for serialization support.
| |
| insertEntry | back to summary |
|---|---|
| pack-priv void insertEntry(int entryIndex, E object, int hash, int mask) Overrides com. Doc from com. Creates a fresh entry with the specified object at the specified position in the entry arrays.
| |
| moveLastEntry | back to summary |
|---|---|
| pack-priv void moveLastEntry(int dstIndex, int mask) Overrides com. Doc from com. Moves the last entry in the entry array into
| |
| requirePredecessors | back to summary |
|---|---|
| private int[] requirePredecessors() | |
| requireSuccessors | back to summary |
|---|---|
| private int[] requireSuccessors() | |
| resizeEntries | back to summary |
|---|---|
| pack-priv void resizeEntries(int newCapacity) Overrides com. Doc from com. Resizes the internal entries array to the specified capacity, which may be greater or less than the current capacity.
| |
| setPredecessor | back to summary |
|---|---|
| private void setPredecessor(int entry, int pred) | |
| setSucceeds | back to summary |
|---|---|
| private void setSucceeds(int pred, int succ) | |
| setSuccessor | back to summary |
|---|---|
| private void setSuccessor(int entry, int succ) | |
| spliterator | back to summary |
|---|---|
| public Spliterator Overrides com. Overrides default java. Doc from java. Creates a The
| |
| toArray | back to summary |
|---|---|
| public @Nullable Object[] toArray() Overrides com. Implements java. Doc from java. Returns an array containing all of the elements in this set. If this set makes any guarantees as to what order its elements are returned by its iterator, this method must return the elements in the same order. The returned array will be "safe" in that no references to it are maintained by this set. (In other words, this method must allocate a new array even if this set is backed by an array). The caller is thus free to modify the returned array. This method acts as bridge between array-based and collection-based APIs. | |
| toArray | back to summary |
|---|---|
| public <T extends @Nullable Object> T[] toArray(T[] a) Overrides com. Implements java. Doc from java. Returns an array containing all of the elements in this set; the runtime type of the returned array is that of the specified array. If the set fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this set. If this set fits in the specified array with room to spare
(i.e., the array has more elements than this set), the element in
the array immediately following the end of the set is set to
If this set makes any guarantees as to what order its elements are returned by its iterator, this method must return the elements in the same order. Like the Suppose
String[] y = x.toArray(new String[0]);
Note that toArray(new Object[0]) is identical in function to
toArray().
| |