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 Indexer
.
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 Indexer
. The IndexedSet can be serialized
if all elements and the Indexer
are serializable.
Constructor and Description |
---|
IndexedSet()
Creates new empty set with default indexer
Indexer.DEFAULT and default initial capacity. |
IndexedSet(Collection<V> c)
Creates a new set containing the elements in the specified collection.
|
IndexedSet(Indexer<K,? super V> indexer)
Creates new empty set with specified indexer and default initial capacity.
|
IndexedSet(Indexer<K,? super V> indexer,
Collection<? extends V> c)
Creates a new set with specified indexer containing the elements in the specified collection.
|
IndexedSet(Indexer<K,? super V> indexer,
int initial_capacity)
Creates new empty set with specified indexer and specified initial capacity.
|
IndexedSet(int initial_capacity)
Creates new empty set with default indexer
Indexer.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.
|
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(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.
|
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(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()
Returns indexer 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.
|
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.
|
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.
|
addAll, containsAll, equals, hashCode, isEmpty, removeAll, retainAll, toString
public IndexedSet()
Indexer.DEFAULT
and default initial capacity.public IndexedSet(int initial_capacity)
Indexer.DEFAULT
and specified initial capacity.public IndexedSet(Indexer<K,? super V> indexer)
public IndexedSet(Indexer<K,? super V> indexer, int initial_capacity)
public IndexedSet(Collection<V> c)
IndexedSet
, then new indexed set uses same indexer,
otherwise it uses default indexer Indexer.DEFAULT
.public IndexedSet(Indexer<K,? super V> indexer, Collection<? extends V> c)
public static <V> IndexedSet<V,V> of(V... objs)
public IndexedSet<K,V> clone()
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>
public Indexer<K,? super V> getIndexer()
public int size()
public V getByValue(V value)
public V getByKey(K 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(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()
public Object[] toArray()
Collection.toArray()
method.toArray
in interface Collection<V>
toArray
in interface Set<V>
toArray
in class AbstractConcurrentSet<V>
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(long key)
Copyright © 2015 Devexperts. All Rights Reserved.