Class IndexedCollection<K,C>
- java.lang.Object
 - 
- org.apache.commons.collections4.collection.AbstractCollectionDecorator<C>
 - 
- org.apache.commons.collections4.collection.IndexedCollection<K,C>
 
 
 
- 
- Type Parameters:
 K- the type of object in the index.C- the type of object in the collection.
- All Implemented Interfaces:
 java.io.Serializable,java.lang.Iterable<C>,java.util.Collection<C>
public class IndexedCollection<K,C> extends AbstractCollectionDecorator<C>
An IndexedCollection is a Map-like view onto a Collection. It accepts a keyTransformer to define how the keys are converted from the values.Modifications made to this decorator modify the index as well as the decorated
Collection. However, modifications to the underlyingCollectionwill not update the index and it will get out of sync.If modification of the decorated
Collectionis unavoidable, then a call toreindex()will update the index to the current contents of theCollection.- Since:
 - 4.0
 - See Also:
 - Serialized Form
 
 
- 
- 
Constructor Summary
Constructors Constructor Description IndexedCollection(java.util.Collection<C> coll, Transformer<C,K> keyTransformer, MultiMap<K,C> map, boolean uniqueIndex)Create aIndexedCollection. 
- 
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanadd(C object)booleanaddAll(java.util.Collection<? extends C> coll)voidclear()booleancontains(java.lang.Object object)booleancontainsAll(java.util.Collection<?> coll)Cget(K key)Get the element associated with the given key.static <K,C>
IndexedCollection<K,C>nonUniqueIndexedCollection(java.util.Collection<C> coll, Transformer<C,K> keyTransformer)Create anIndexedCollectionfor a non-unique index.voidreindex()Clears the index and re-indexes the entire decoratedCollection.booleanremove(java.lang.Object object)booleanremoveAll(java.util.Collection<?> coll)booleanremoveIf(java.util.function.Predicate<? super C> filter)booleanretainAll(java.util.Collection<?> coll)static <K,C>
IndexedCollection<K,C>uniqueIndexedCollection(java.util.Collection<C> coll, Transformer<C,K> keyTransformer)Create anIndexedCollectionfor a unique index.java.util.Collection<C>values(K key)Get all elements associated with the given key.- 
Methods inherited from class org.apache.commons.collections4.collection.AbstractCollectionDecorator
isEmpty, iterator, size, toArray, toArray, toString 
- 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait 
 - 
 
 - 
 
- 
- 
Constructor Detail
- 
IndexedCollection
public IndexedCollection(java.util.Collection<C> coll, Transformer<C,K> keyTransformer, MultiMap<K,C> map, boolean uniqueIndex)
Create aIndexedCollection.- Parameters:
 coll- decoratedCollectionkeyTransformer-Transformerfor generating index keysmap- map to use as indexuniqueIndex- if the index shall enforce uniqueness of index keys
 
 - 
 
- 
Method Detail
- 
uniqueIndexedCollection
public static <K,C> IndexedCollection<K,C> uniqueIndexedCollection(java.util.Collection<C> coll, Transformer<C,K> keyTransformer)
Create anIndexedCollectionfor a unique index.If an element is added, which maps to an existing key, an
IllegalArgumentExceptionwill be thrown.- Type Parameters:
 K- the index object type.C- the collection type.- Parameters:
 coll- the decoratedCollection.keyTransformer- theTransformerfor generating index keys.- Returns:
 - the created 
IndexedCollection. 
 
- 
nonUniqueIndexedCollection
public static <K,C> IndexedCollection<K,C> nonUniqueIndexedCollection(java.util.Collection<C> coll, Transformer<C,K> keyTransformer)
Create anIndexedCollectionfor a non-unique index.- Type Parameters:
 K- the index object type.C- the collection type.- Parameters:
 coll- the decoratedCollection.keyTransformer- theTransformerfor generating index keys.- Returns:
 - the created 
IndexedCollection. 
 
- 
add
public boolean add(C object)
- Specified by:
 addin interfacejava.util.Collection<K>- Overrides:
 addin classAbstractCollectionDecorator<C>- Throws:
 java.lang.IllegalArgumentException- if the object maps to an existing key and the index enforces a uniqueness constraint
 
- 
addAll
public boolean addAll(java.util.Collection<? extends C> coll)
- Specified by:
 addAllin interfacejava.util.Collection<K>- Overrides:
 addAllin classAbstractCollectionDecorator<C>
 
- 
clear
public void clear()
- Specified by:
 clearin interfacejava.util.Collection<K>- Overrides:
 clearin classAbstractCollectionDecorator<C>
 
- 
contains
public boolean contains(java.lang.Object object)
Note: uses the index for fast lookup
- Specified by:
 containsin interfacejava.util.Collection<K>- Overrides:
 containsin classAbstractCollectionDecorator<C>
 
- 
containsAll
public boolean containsAll(java.util.Collection<?> coll)
Note: uses the index for fast lookup
- Specified by:
 containsAllin interfacejava.util.Collection<K>- Overrides:
 containsAllin classAbstractCollectionDecorator<C>
 
- 
get
public C get(K key)
Get the element associated with the given key.In case of a non-unique index, this method will return the first value associated with the given key. To retrieve all elements associated with a key, use
values(Object).- Parameters:
 key- key to look up- Returns:
 - element found
 - See Also:
 values(Object)
 
- 
values
public java.util.Collection<C> values(K key)
Get all elements associated with the given key.- Parameters:
 key- key to look up- Returns:
 - a collection of elements found, or null if 
contains(key) == false 
 
- 
reindex
public void reindex()
Clears the index and re-indexes the entire decoratedCollection. 
- 
remove
public boolean remove(java.lang.Object object)
- Specified by:
 removein interfacejava.util.Collection<K>- Overrides:
 removein classAbstractCollectionDecorator<C>
 
- 
removeIf
public boolean removeIf(java.util.function.Predicate<? super C> filter)
- Specified by:
 removeIfin interfacejava.util.Collection<K>- Overrides:
 removeIfin classAbstractCollectionDecorator<C>- Since:
 - 4.4
 
 
- 
removeAll
public boolean removeAll(java.util.Collection<?> coll)
- Specified by:
 removeAllin interfacejava.util.Collection<K>- Overrides:
 removeAllin classAbstractCollectionDecorator<C>
 
- 
retainAll
public boolean retainAll(java.util.Collection<?> coll)
- Specified by:
 retainAllin interfacejava.util.Collection<K>- Overrides:
 retainAllin classAbstractCollectionDecorator<C>
 
 - 
 
 -