common
Interface IndexedSet

All Superinterfaces:
java.util.Collection, java.lang.Iterable, java.util.Set
All Known Subinterfaces:
IndexedSetDiff, IndexedSortedSet
All Known Implementing Classes:
IndexedHashMultiMap.IndexedValueSet, IndexedHashSet, IndexedHashSetDiff, IndexedMultiMapDiff.IndexedValueSet, IndexedSet.EmptyIndexedSet, IndexedTreeSet, IndexedTreeSetDiff

public interface IndexedSet
extends java.util.Set

Data structure that behaves like a set, except that its elements also have consecutive indices starting at zero. Different implementations may assign these indices in different ways: for example, according to the ordering on elements defined by a Comparator object, or according to the order in which objects were added. Indices are not guaranteed to remain stable when the IndexedSet is modified. In some implementations, adding or removing elements may change the indices of other elements.

IndexedSet extends the Set interface with additional methods get and indexOf, which behave like the corresponding methods in the List interface. Also, iterators over an IndexedSet go in the order defined by the indices. The only thing that invalidates an iterator is removing an element without going through the iterator's remove method. Notably, adding elements does not invalidate iterators.

One advantage of using an IndexedSet is that you can sample an element uniformly at random in constant time (by getting the element at a random index n). Another is that you can iterate over it just by incrementing an integer index.


Nested Class Summary
static class IndexedSet.EmptyIndexedSet
           
 
Field Summary
static IndexedSet EMPTY_INDEXED_SET
          An unmodifiable, empty indexed set.
 
Method Summary
 java.lang.Object get(int index)
          Returns the object with the specified index in this IndexedSet.
 int indexOf(java.lang.Object o)
          Returns the index of the given object in this IndexedSet, or -1 if this IndexedSet does not contain the given object.
 
Methods inherited from interface java.util.Set
add, addAll, clear, contains, containsAll, equals, hashCode, isEmpty, iterator, remove, removeAll, retainAll, size, toArray, toArray
 

Field Detail

EMPTY_INDEXED_SET

static final IndexedSet EMPTY_INDEXED_SET
An unmodifiable, empty indexed set.

Method Detail

get

java.lang.Object get(int index)
Returns the object with the specified index in this IndexedSet.

Throws:
java.lang.IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())

indexOf

int indexOf(java.lang.Object o)
Returns the index of the given object in this IndexedSet, or -1 if this IndexedSet does not contain the given object.