public class HashIndexer<T> extends Object implements Indexer<T>, Serializable
0.
This implementation provides faster index(Object) performance
than ObjectIndexer but at the expensive of having amortized constant
time lookup(int) and #find(int) performance. This class
will have superior performance in use cases where all objects are indexed and
then lookup or find is called repeatedly whithout having
indexed any new objects.
ObjectIndexer,
Serialized Form| Constructor and Description |
|---|
HashIndexer()
Creates an empty
HashIndexer with no mappings. |
HashIndexer(Collection<? extends T> items)
Creates an
HashIndexer with indices for all of the provided items. |
HashIndexer(Indexer<? extends T> indexed)
Creates an
HashIndexer with indices for all of the provided items. |
| Modifier and Type | Method and Description |
|---|---|
void |
clear()
Removes all of the item-index mappings.
|
boolean |
contains(T item)
Returns
true if the item has a corresponding index |
int |
find(T item)
Returns the index for the item or a negative value corresponding to the
index the object would have been assigned if it had been added.
|
int |
highestIndex()
Returns the highest index to which any element is mapped.
|
int |
index(T item)
Adds the item and creates an index for it, returning that index.
|
boolean |
indexAll(Collection<T> items)
Adds an index for each item in the collection if it did not already
exist, returning
true if at least one new index was added. |
Set<T> |
items()
Returns an unmodifiable view of the items currently mapped to indices
|
Iterator<Map.Entry<T,Integer>> |
iterator()
Returns an iterator over all the objects and their corresponding indices.
|
T |
lookup(int index)
Returns the element to which this index is mapped or
null if the
index has not been mapped |
Map<Integer,T> |
mapping()
Returns an unmodifiable view from each index to the object mapped to that
index.
|
int |
size()
Returns the number of items that are mapped to indices.
|
String |
toString() |
public HashIndexer()
HashIndexer with no mappings.public HashIndexer(Collection<? extends T> items)
HashIndexer with indices for all of the provided items.public void clear()
public boolean contains(T item)
true if the item has a corresponding indexpublic int find(T item)
public int highestIndex()
highestIndex in interface Indexer<T>public int index(T item)
public boolean indexAll(Collection<T> items)
true if at least one new index was added.public Iterator<Map.Entry<T,Integer>> iterator()
remove. The returned iterator does not support remove.public T lookup(int index)
null if the
index has not been mappedpublic Map<Integer,T> mapping()
public int size()
Copyright © 2012. All Rights Reserved.