public class OrderedSet<T> extends ObjectSet<T>
ObjectSet that also stores keys in an Array using the insertion order. Null keys are not allowed. No
allocation is done except when growing the table size.
Iteration is ordered and faster than an unordered set. Keys can also be accessed and the order changed
using orderedItems(). There is some additional overhead for put and remove. When used for faster iteration versus
ObjectSet and the order does not actually matter, copying during remove can be greatly reduced by setting Array.ordered
to false for orderedItems().
This class performs fast contains (typically O(1), worst case O(n) but that is rare in practice). Remove is somewhat slower due
to orderedItems(). Add may be slightly slower, depending on hash collisions. Hashcodes are rehashed to reduce
collisions and the need to resize. Load factors greater than 0.91 greatly increase the chances to resize to the next higher POT
size.
Unordered sets and maps are not designed to provide especially fast iteration. Iteration is faster with OrderedSet and OrderedMap.
This implementation uses linear probing with the backward shift algorithm for removal. Hashcodes are rehashed using Fibonacci hashing, instead of the more common power-of-two mask, to better distribute poor hashCodes (see Malte Skarupke's blog post). Linear probing continues to work even when all hashCodes collide, just more slowly.
| Modifier and Type | Class and Description |
|---|---|
static class |
OrderedSet.OrderedSetIterator<K> |
ObjectSet.ObjectSetIterator<K>| Constructor and Description |
|---|
OrderedSet() |
OrderedSet(int initialCapacity) |
OrderedSet(int initialCapacity,
float loadFactor) |
OrderedSet(OrderedSet<? extends T> set) |
| Modifier and Type | Method and Description |
|---|---|
boolean |
add(T key)
Returns true if the key was not already in the set.
|
boolean |
add(T key,
int index)
Sets the key at the specfied index.
|
void |
addAll(OrderedSet<T> set) |
boolean |
alter(T before,
T after)
Changes the item
before to after without changing its position in the order. |
boolean |
alterIndex(int index,
T after)
Changes the item at the given
index in the order to after, without changing the ordering of other items. |
void |
clear()
Clears the set, leaving the backing arrays at the current capacity.
|
void |
clear(int maximumCapacity)
Clears the set and reduces the size of the backing arrays to be the specified capacity / loadFactor, if they are larger.
|
OrderedSet.OrderedSetIterator<T> |
iterator()
Returns an iterator for the keys in the set.
|
Array<T> |
orderedItems() |
boolean |
remove(T key)
Returns true if the key was removed.
|
T |
removeIndex(int index) |
String |
toString() |
String |
toString(String separator) |
static <T> OrderedSet<T> |
with(T... array) |
addAll, addAll, addAll, addAll, addAll, contains, ensureCapacity, equals, first, get, hashCode, isEmpty, notEmpty, shrinkforEach, spliteratorpublic OrderedSet()
public OrderedSet(int initialCapacity,
float loadFactor)
public OrderedSet(int initialCapacity)
public OrderedSet(OrderedSet<? extends T> set)
public boolean add(T key)
ObjectSetpublic boolean add(T key, int index)
public void addAll(OrderedSet<T> set)
public boolean remove(T key)
ObjectSetpublic T removeIndex(int index)
public boolean alter(T before, T after)
before to after without changing its position in the order. Returns true if after
has been added to the OrderedSet and before has been removed; returns false if after is already present or
before is not present. If you are iterating over an OrderedSet and have an index, you should prefer
alterIndex(int, Object), which doesn't need to search for an index like this does and so can be faster.before - an item that must be present for this to succeedafter - an item that must not be in this set for this to succeedbefore was removed and after was added, false otherwisepublic boolean alterIndex(int index,
T after)
index in the order to after, without changing the ordering of other items. If
after is already present, this returns false; it will also return false if index is invalid for the size of
this set. Otherwise, it returns true. Unlike alter(Object, Object), this operates in constant time.index - the index in the order of the item to change; must be non-negative and less than ObjectSet.sizeafter - the item that will replace the contents at index; this item must not be present for this to succeedafter successfully replaced the contents at index, false otherwisepublic void clear(int maximumCapacity)
ObjectSetpublic void clear()
ObjectSetObjectSet.clear(int) can be used to reduce the capacity.public OrderedSet.OrderedSetIterator<T> iterator()
ObjectSet
If Collections.allocateIterators is false, the same iterator instance is returned each time this method is called.
Use the ObjectSet.ObjectSetIterator constructor for nested or multithreaded iteration.
public static <T> OrderedSet<T> with(T... array)
Copyright © 2021. All rights reserved.