public class IndexedSet<K,V> extends AbstractConcurrentSet<V> implements Cloneable, Serializable
Set
interface
and provides additional benefits over standard implementations:
The IndexedSet assumes that identity of an element can be represented by a variable number
of attributes, therefore it delegates identification to an external strategy — the IndexerFunction
.
In order to fulfil contracts of Map
interface and for convenience, the IndexedSet
supports concept of explicit key object and also numeric key representation, but these
identification means are optional and need not be supported by all strategies.
Note that the IndexedSet is not synchronized! Concurrent modifications of IndexedSet from multiple threads must be synchronized externally to preserve data integrity. On the other side, the IndexedSet fully supports concurrent asynchronous read operations, which works during concurrent modification by other thread. In case of concurrent modification each atomic read sees IndexedSet either before or after each atomic write operation.
The IndexedSet does not support null values, but it supports null keys
if they are supported by corresponding IndexerFunction
. The IndexedSet is serializable.
Modifier | Constructor and Description |
---|---|
|
IndexedSet()
Creates new empty set with default indexer
IndexerFunction.DEFAULT . |
|
IndexedSet(Collection<V> c)
Creates a new set containing the elements in the specified collection.
|
|
IndexedSet(Indexer<K,? super V> indexer)
Deprecated.
Use
create(indexer) |
|
IndexedSet(Indexer<K,? super V> indexer,
Collection<? extends V> c)
Deprecated.
|
|
IndexedSet(Indexer<K,? super V> indexer,
int initialCapacity)
Deprecated.
|
protected |
IndexedSet(IndexerFunction<K,? super V> indexer)
Creates new empty set with specified indexer.
|
protected |
IndexedSet(IndexerFunction<K,? super V> indexer,
Collection<? extends V> c)
Creates a new set with specified indexer containing the elements in the specified collection.
|
protected |
IndexedSet(IndexerFunction<K,? super V> indexer,
int initialCapacity)
Creates new empty set with specified indexer and specified initial capacity.
|
|
IndexedSet(int initialCapacity)
Creates new empty set with default indexer
IndexerFunction.DEFAULT and specified initial capacity. |
Modifier and Type | Method and Description |
---|---|
boolean |
add(V value)
Adds specified element into this set and returns true
if this operation has increased the size of this set.
|
void |
clear()
Removes all elements from this set.
|
IndexedSet<K,V> |
clone()
Returns a shallow copy of this set - the values themselves are not cloned.
|
static <V> Collector<V,?,? extends IndexedSet<V,V>> |
collector()
Returns a
Collector that accumulates the input elements into a new IndexedSet with default indexer. |
static <V> Collector<V,?,? extends IndexedSet<Integer,V>> |
collector(IndexerFunction.IntKey<? super V> indexer)
Deprecated.
|
static <V> Collector<V,?,? extends IndexedSet<Long,V>> |
collector(IndexerFunction.LongKey<? super V> indexer)
Deprecated.
|
static <K,V> Collector<V,?,? extends IndexedSet<K,V>> |
collector(IndexerFunction<K,? super V> indexer)
Returns a
Collector that accumulates the input elements into a new IndexedSet with specified indexer. |
static <V> Collector<V,?,? extends IndexedSet<V,V>> |
collectorIdentity()
Returns a
Collector that accumulates the input elements into a new IndexedSet with default identity indexer. |
static <K,V> Collector<V,?,? extends IndexedSet<K,V>> |
collectorIdentity(IndexerFunction.IdentityKey<K,? super V> indexer)
Returns a
Collector that accumulates the input elements into a new IndexedSet with specified identity indexer. |
static <V> Collector<V,?,? extends IndexedSet<Integer,V>> |
collectorInt(IndexerFunction.IntKey<? super V> indexer)
Returns a
Collector that accumulates the input elements into a new IndexedSet with specified int indexer. |
static <V> Collector<V,?,? extends IndexedSet<Long,V>> |
collectorLong(IndexerFunction.LongKey<? super V> indexer)
Returns a
Collector that accumulates the input elements into a new IndexedSet with specified long indexer. |
Iterator<V> |
concurrentIterator()
Returns concurrent iterator over the elements in this set.
|
boolean |
contains(Object value)
Deprecated.
Use
containsValue(V) to be explicit about type and intent. |
boolean |
containsKey(int key)
Returns true if this set contains element which matches specified key.
|
boolean |
containsKey(K key)
Returns true if this set contains element which matches specified key.
|
boolean |
containsKey(long key)
Returns true if this set contains element which matches specified key.
|
boolean |
containsValue(V value)
Returns true if this set contains element which matches specified value.
|
static <V> IndexedSet<V,V> |
create()
Creates new empty set with default indexer
IndexerFunction.DEFAULT . |
static <V> IndexedSet<Integer,V> |
create(IndexerFunction.IntKey<? super V> indexer)
Deprecated.
|
static <V> IndexedSet<Integer,V> |
create(IndexerFunction.IntKey<? super V> indexer,
Collection<? extends V> c)
Deprecated.
|
static <V> IndexedSet<Integer,V> |
create(IndexerFunction.IntKey<? super V> indexer,
int initialCapacity)
Deprecated.
|
static <V> IndexedSet<Long,V> |
create(IndexerFunction.LongKey<? super V> indexer)
Deprecated.
|
static <V> IndexedSet<Long,V> |
create(IndexerFunction.LongKey<? super V> indexer,
Collection<? extends V> c)
Deprecated.
|
static <V> IndexedSet<Long,V> |
create(IndexerFunction.LongKey<? super V> indexer,
int initialCapacity)
Deprecated.
|
static <K,V> IndexedSet<K,V> |
create(IndexerFunction<K,? super V> indexer)
Creates new empty set with specified indexer.
|
static <K,V> IndexedSet<K,V> |
create(IndexerFunction<K,? super V> indexer,
Collection<? extends V> c)
Deprecated.
|
static <K,V> IndexedSet<K,V> |
create(IndexerFunction<K,? super V> indexer,
int initialCapacity)
Deprecated.
|
static <V> IndexedSet<V,V> |
createIdentity()
Creates new empty set with default identity indexer.
|
static <K,V> IndexedSet<K,V> |
createIdentity(IndexerFunction.IdentityKey<K,? super V> indexer)
Creates new empty set with specified identity indexer.
|
static <V> IndexedSet<Integer,V> |
createInt(IndexerFunction.IntKey<? super V> indexer)
Creates new empty set with specified int indexer.
|
static <V> IndexedSet<Long,V> |
createLong(IndexerFunction.LongKey<? super V> indexer)
Creates new empty set with specified long indexer.
|
void |
ensureCapacity(int capacity)
Increases the capacity of this set instance, if necessary, to ensure that it
can hold at least the number of elements specified by the capacity argument.
|
Iterator<Map.Entry<K,V>> |
entryIterator()
Returns an iterator over the entries in this set.
|
V |
getByKey(int key)
Returns the element from this set which matches specified key or null if none were found.
|
V |
getByKey(K key)
Returns the element from this set which matches specified key or null if none were found.
|
V |
getByKey(long key)
Returns the element from this set which matches specified key or null if none were found.
|
V |
getByValue(V value)
Returns the element from this set which matches specified value or null if none were found.
|
Indexer<K,? super V> |
getIndexer()
Deprecated.
|
IndexerFunction<K,? super V> |
getIndexerFunction()
Returns indexer function used to distinguish and identify elements in this set.
|
IndexedSetStats |
getStats()
Returns static structure statistics of this set.
|
Iterator<V> |
iterator()
Returns an iterator over the elements in this set.
|
Iterator<K> |
keyIterator()
Returns an iterator over the keys of elements in this set.
|
static <V> IndexedSet<V,V> |
of(V... objs)
Creates a new set with default indexer containing specified elements.
|
V |
put(V value)
Puts specified element into this set and returns previous element that matches specified one.
|
V |
putIfAbsentAndGet(V value)
Puts specified element into this set if it is absent and
returns current element in the set that matches specified one.
|
boolean |
remove(Object value)
Removes specified element from this set if it is present and returns
true if this operation has decreased the size of this set.
|
boolean |
removeAll(Collection<?> c)
Removes all of the elements in the specified collection from this set and
returns true if this operation has decreased the size of this set.
|
V |
removeKey(int key)
Removes the element from this set which matches specified key if it is present
and returns removed element or null if none were found.
|
V |
removeKey(K key)
Removes the element from this set which matches specified key if it is present
and returns removed element or null if none were found.
|
V |
removeKey(long key)
Removes the element from this set which matches specified key if it is present
and returns removed element or null if none were found.
|
V |
removeValue(V value)
Removes the element from this set which matches specified value if it is present
and returns removed element or null if none were found.
|
boolean |
retainAll(Collection<?> c)
Retains only the elements in this set that are contained in the specified collection.
|
int |
size()
Returns the number of elements in this set.
|
Object[] |
toArray()
Returns an array containing all of the elements in this set.
|
<T> T[] |
toArray(T[] a)
Returns an array containing all of the elements in this set whose runtime type is that of the specified array.
|
void |
trimToSize()
Trims the capacity of this set instance to be the set's current size.
|
IndexedSet<K,V> |
withCapacity(int capacity)
Increases the capacity of this set instance, if necessary, to ensure that it
can hold at least the number of elements specified by the capacity argument.
|
IndexedSet<K,V> |
withElements(Collection<? extends V> c)
Adds all of the elements in the specified collection into this set.
|
addAll, containsAll, equals, hashCode, isEmpty, toString
finalize, getClass, notify, notifyAll, wait, wait, wait
spliterator
parallelStream, removeIf, stream
public IndexedSet()
IndexerFunction.DEFAULT
.public IndexedSet(int initialCapacity)
IndexerFunction.DEFAULT
and specified initial capacity.protected IndexedSet(IndexerFunction<K,? super V> indexer)
@Deprecated public IndexedSet(Indexer<K,? super V> indexer)
create(indexer)
protected IndexedSet(IndexerFunction<K,? super V> indexer, int initialCapacity)
@Deprecated public IndexedSet(Indexer<K,? super V> indexer, int initialCapacity)
create(indexer)
.withCapacity(initialCapacity)
public IndexedSet(Collection<V> c)
IndexedSet
, then new indexed set uses same indexer,
otherwise it uses default indexer IndexerFunction.DEFAULT
.protected IndexedSet(IndexerFunction<K,? super V> indexer, Collection<? extends V> c)
@Deprecated public IndexedSet(Indexer<K,? super V> indexer, Collection<? extends V> c)
create(indexer)
.withElements(c)
public static <V> IndexedSet<V,V> create()
IndexerFunction.DEFAULT
.public static <V> IndexedSet<V,V> createIdentity()
public static <K,V> IndexedSet<K,V> create(IndexerFunction<K,? super V> indexer)
public static <K,V> IndexedSet<K,V> createIdentity(IndexerFunction.IdentityKey<K,? super V> indexer)
public static <V> IndexedSet<Integer,V> createInt(IndexerFunction.IntKey<? super V> indexer)
public static <V> IndexedSet<Long,V> createLong(IndexerFunction.LongKey<? super V> indexer)
@Deprecated public static <V> IndexedSet<Integer,V> create(IndexerFunction.IntKey<? super V> indexer)
createInt(indexer)
@Deprecated public static <V> IndexedSet<Long,V> create(IndexerFunction.LongKey<? super V> indexer)
createLong(indexer)
@Deprecated public static <K,V> IndexedSet<K,V> create(IndexerFunction<K,? super V> indexer, int initialCapacity)
create(indexer)
.withCapacity(initialCapacity)
@Deprecated public static <V> IndexedSet<Integer,V> create(IndexerFunction.IntKey<? super V> indexer, int initialCapacity)
createInt(indexer)
.withCapacity(initialCapacity)
@Deprecated public static <V> IndexedSet<Long,V> create(IndexerFunction.LongKey<? super V> indexer, int initialCapacity)
createLong(indexer)
.withCapacity(initialCapacity)
@Deprecated public static <K,V> IndexedSet<K,V> create(IndexerFunction<K,? super V> indexer, Collection<? extends V> c)
create(indexer)
.withElements(c)
@Deprecated public static <V> IndexedSet<Integer,V> create(IndexerFunction.IntKey<? super V> indexer, Collection<? extends V> c)
createInt(indexer)
.withElements(c)
@Deprecated public static <V> IndexedSet<Long,V> create(IndexerFunction.LongKey<? super V> indexer, Collection<? extends V> c)
createLong(indexer)
.withElements(c)
@SafeVarargs public static <V> IndexedSet<V,V> of(V... objs)
public static <V> Collector<V,?,? extends IndexedSet<V,V>> collector()
Collector
that accumulates the input elements into a new IndexedSet
with default indexer.
This is an unordered
Collector.public static <V> Collector<V,?,? extends IndexedSet<V,V>> collectorIdentity()
Collector
that accumulates the input elements into a new IndexedSet
with default identity indexer.
This is an unordered
Collector.public static <K,V> Collector<V,?,? extends IndexedSet<K,V>> collector(IndexerFunction<K,? super V> indexer)
Collector
that accumulates the input elements into a new IndexedSet
with specified indexer.
This is an unordered
Collector.public static <K,V> Collector<V,?,? extends IndexedSet<K,V>> collectorIdentity(IndexerFunction.IdentityKey<K,? super V> indexer)
Collector
that accumulates the input elements into a new IndexedSet
with specified identity indexer.
This is an unordered
Collector.public static <V> Collector<V,?,? extends IndexedSet<Integer,V>> collectorInt(IndexerFunction.IntKey<? super V> indexer)
Collector
that accumulates the input elements into a new IndexedSet
with specified int indexer.
This is an unordered
Collector.public static <V> Collector<V,?,? extends IndexedSet<Long,V>> collectorLong(IndexerFunction.LongKey<? super V> indexer)
Collector
that accumulates the input elements into a new IndexedSet
with specified long indexer.
This is an unordered
Collector.@Deprecated public static <V> Collector<V,?,? extends IndexedSet<Integer,V>> collector(IndexerFunction.IntKey<? super V> indexer)
collectorInt(indexer)
Collector
that accumulates the input elements into a new IndexedSet
with specified indexer.
This is an unordered
Collector.@Deprecated public static <V> Collector<V,?,? extends IndexedSet<Long,V>> collector(IndexerFunction.LongKey<? super V> indexer)
collectorLong(indexer)
Collector
that accumulates the input elements into a new IndexedSet
with specified indexer.
This is an unordered
Collector.public IndexedSet<K,V> clone()
public IndexedSet<K,V> withCapacity(int capacity)
Returns this set instance for convenience.
public IndexedSet<K,V> withElements(Collection<? extends V> c)
Returns this set instance for convenience.
public void ensureCapacity(int capacity)
public void trimToSize()
public void clear()
clear
in interface Collection<V>
clear
in interface Set<V>
clear
in class AbstractConcurrentSet<V>
@Deprecated public Indexer<K,? super V> getIndexer()
getIndexerFunction()
public IndexerFunction<K,? super V> getIndexerFunction()
public int size()
public V getByValue(V value)
public V getByKey(K key)
public V getByKey(int key)
public V getByKey(long key)
public boolean contains(Object value)
containsValue(V)
to be explicit about type and intent.
This implementation delegates to (getByValue(value)
!= null) expression.
Note, that unlike HashSet.contains(java.lang.Object)
,
this method might throw ClassCastException
if value is of the wrong class.
public boolean containsValue(V value)
This implementation delegates to (getByValue(value)
!= null) expression.
public boolean containsKey(K key)
This implementation delegates to (getByKey(key)
!= null) expression.
public boolean containsKey(int key)
This implementation delegates to (getByKey(key)
!= null) expression.
public boolean containsKey(long key)
This implementation delegates to (getByKey(key)
!= null) expression.
public Iterator<K> keyIterator()
public Iterator<Map.Entry<K,V>> entryIterator()
public Iterator<V> concurrentIterator()
@Nonnull public Object[] toArray()
Collection.toArray()
method.toArray
in interface Collection<V>
toArray
in interface Set<V>
toArray
in class AbstractConcurrentSet<V>
@Nonnull public <T> T[] toArray(T[] a)
Collection.toArray(Object[])
method.toArray
in interface Collection<V>
toArray
in interface Set<V>
toArray
in class AbstractConcurrentSet<V>
public IndexedSetStats getStats()
public V put(V value)
public V putIfAbsentAndGet(V value)
if (set.containsValue(value)) { return set.getByValue(value); } else { set.put(value); return value; }except that the action is performed atomically if it is properly synchronized.
Note, that unlike ConcurrentMap.putIfAbsent(K, V)
,
this method returns specified value (not null) if the value was absent.
public boolean add(V value)
This implementation adds value using put(value)
method.
public boolean remove(Object value)
This implementation removes value using removeValue(value)
method.
Note, that unlike HashSet.remove(java.lang.Object)
,
this method might throw ClassCastException
if value is of the wrong class.
public V removeValue(V value)
public V removeKey(K key)
public V removeKey(int key)
public V removeKey(long key)
public boolean removeAll(Collection<?> c)
Elements compared using the set's IndexerFunction
.
removeAll
in interface Collection<V>
removeAll
in interface Set<V>
removeAll
in class AbstractConcurrentSet<V>
c
- collection containing elements to be removed from this setpublic boolean retainAll(Collection<?> c)
Elements compared using the set's IndexerFunction
.
NOTE: if provided collection is not an IndexedSet
with the same IndexerFunction
this
implementation will create a temporary IndexedSet with provided elements.
retainAll
in interface Collection<V>
retainAll
in interface Set<V>
retainAll
in class AbstractConcurrentSet<V>
c
- collection containing elements to be retained in this setCopyright © 2002–2025 Devexperts LLC. All rights reserved.