public class OrdinalMap<T>
extends java.lang.Object
implements java.lang.Iterable<T>
OrdinalMap will generate and maintain a mapping between objects added and an integer value between
0 and n, where n is the number of objects in the map.The values mapped to the objects will be the order in which the objects are inserted.
The OrdinalMap is memory-efficient and can retrieve an object given an ordinal, or an ordinal given an object, both in O(1) time.
If, for example, some application refers to graph nodes as Strings, the OrdinalMap can be used as follows:
OrdinalMap<String> ordinalMap = new OrdinalMap<String>();
int ord0 = ordinalMap.add("node0"); // returns 0
int ord1 = ordinalMap.add("node1"); // returns 1
int ord2 = ordinalMap.add("node2"); // returns 2
int ord3 = ordinalMap.add("node1"); // returns 1
ordinalMap.get("node2"); // returns 2
ordinalMap.get(ord2); // returns "node2"
| Constructor and Description |
|---|
OrdinalMap() |
OrdinalMap(int expectedSize) |
| Modifier and Type | Method and Description |
|---|---|
int |
add(T obj)
Add an object into this
OrdinalMap. |
T |
get(int ordinal) |
int |
get(T obj) |
java.util.Iterator<T> |
iterator() |
int |
size() |
public int add(T obj)
OrdinalMap. If the same object (or an Object.equals(Object) object) is
already in the map, then no changes will be made.objpublic int get(T obj)
public T get(int ordinal)
public int size()