Package arc.struct
Class ArrayMap<K,V>
java.lang.Object
arc.struct.ArrayMap<K,V>
- All Implemented Interfaces:
Iterable<ObjectMap.Entry<K,
V>>
- Direct Known Subclasses:
Jval.JsonMap
An ordered or unordered map of objects. This implementation uses arrays to store the keys and values, which means
gets
do a comparison for each key in the map. This is slower than a typical hash map
implementation, but may be acceptable for small maps and has the benefits that keys and values can be accessed by index, which
makes iteration fast. Like Seq
, if ordered is false, this class avoids a memory copy when removing elements (the last
element is moved to the removed element's position).-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
static class
static class
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
void
clear
(int maximumCapacity) Clears the map and reduces the size of the backing arrays to be the specified capacity if they are larger.boolean
containsKey
(K key) boolean
containsValue
(V value, boolean identity) void
ensureCapacity
(int additionalCapacity) Increases the size of the backing arrays to accommodate the specified number of additional entries.entries()
Returns an iterator for the entries in the map.boolean
firstKey()
Returns the value for the specified key.Returns the key for the specified value.getKeyAt
(int index) getValueAt
(int index) int
hashCode()
int
indexOfKey
(K key) int
indexOfValue
(V value, boolean identity) void
boolean
isEmpty()
Returns true if the map is empty.iterator()
keys()
Returns an iterator for the keys in the map.peekKey()
Returns the last key.Returns the last value.int
int
void
void
void
removeIndex
(int index) Removes and returns the key/values pair at the specified index.boolean
removeValue
(V value, boolean identity) protected void
resize
(int newSize) void
reverse()
void
void
void
shrink()
Reduces the size of the backing arrays to the size of the actual number of entries.void
shuffle()
toString()
void
truncate
(int newSize) Reduces the size of the arrays to the specified size.values()
Returns an iterator for the values in the map.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
keys
-
values
-
size
public int size -
ordered
public boolean ordered
-
-
Constructor Details
-
ArrayMap
public ArrayMap()Creates an ordered map with a capacity of 16. -
ArrayMap
public ArrayMap(int capacity) Creates an ordered map with the specified capacity. -
ArrayMap
public ArrayMap(boolean ordered, int capacity) - Parameters:
ordered
- If false, methods that remove elements may change the order of other elements in the arrays, which avoids a memory copy.capacity
- Any elements added beyond this will cause the backing arrays to be grown.
-
ArrayMap
- Parameters:
ordered
- If false, methods that remove elements may change the order of other elements in the arrays, which avoids a memory copy.capacity
- Any elements added beyond this will cause the backing arrays to be grown.
-
ArrayMap
-
ArrayMap
Creates a new map containing the elements in the specified map. The new map will have the same type of backing arrays and will be ordered if the specified map is ordered. The capacity is set to the number of elements, so any subsequent elements added will cause the backing arrays to be grown.
-
-
Method Details
-
put
-
put
-
putAll
-
putAll
-
get
Returns the value for the specified key. Note this does a .equals() comparison of each key in reverse order until the specified key is found. -
getKey
Returns the key for the specified value. Note this does a comparison of each value in reverse order until the specified value is found.- Parameters:
identity
- If true, == comparison will be used. If false, .equals() comparison will be used.
-
getKeyAt
-
getValueAt
-
firstKey
-
firstValue
-
setKey
-
setValue
-
insert
-
containsKey
-
containsValue
- Parameters:
identity
- If true, == comparison will be used. If false, .equals() comparison will be used.
-
indexOfKey
-
indexOfValue
-
removeKey
-
removeValue
-
removeIndex
public void removeIndex(int index) Removes and returns the key/values pair at the specified index. -
isEmpty
public boolean isEmpty()Returns true if the map is empty. -
peekKey
Returns the last key. -
peekValue
Returns the last value. -
clear
public void clear(int maximumCapacity) Clears the map and reduces the size of the backing arrays to be the specified capacity if they are larger. -
clear
public void clear() -
shrink
public void shrink()Reduces the size of the backing arrays to the size of the actual number of entries. This is useful to release memory when many items have been removed, or if it is known that more entries will not be added. -
ensureCapacity
public void ensureCapacity(int additionalCapacity) Increases the size of the backing arrays to accommodate the specified number of additional entries. Useful before adding many entries to avoid multiple backing array resizes. -
resize
protected void resize(int newSize) -
reverse
public void reverse() -
shuffle
public void shuffle() -
truncate
public void truncate(int newSize) Reduces the size of the arrays to the specified size. If the arrays are already smaller than the specified size, no action is taken. -
hashCode
public int hashCode() -
equals
-
toString
-
iterator
-
entries
Returns an iterator for the entries in the map. Remove is supported. Note that the same iterator instance is returned each time this method is called. Use theArrayMap.Entries
constructor for nested or multithreaded iteration. -
values
Returns an iterator for the values in the map. Remove is supported. Note that the same iterator instance is returned each time this method is called. Use theArrayMap.Entries
constructor for nested or multithreaded iteration. -
keys
Returns an iterator for the keys in the map. Remove is supported. Note that the same iterator instance is returned each time this method is called. Use theArrayMap.Entries
constructor for nested or multithreaded iteration.
-