Package com.badlogic.gdx.utils
Class ArrayMap<K,V>
- java.lang.Object
-
- com.badlogic.gdx.utils.ArrayMap<K,V>
-
- All Implemented Interfaces:
java.lang.Iterable<ObjectMap.Entry<K,V>>
public class ArrayMap<K,V> extends java.lang.Object implements java.lang.Iterable<ObjectMap.Entry<K,V>>
An ordered or unordered map of objects. This implementation uses arrays to store the keys and values, which meansgetsdo 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. LikeArray, 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
Nested Classes Modifier and Type Class Description static classArrayMap.Entries<K,V>static classArrayMap.Keys<K>static classArrayMap.Values<V>
-
Constructor Summary
Constructors Constructor Description ArrayMap()Creates an ordered map with a capacity of 16.ArrayMap(boolean ordered, int capacity)ArrayMap(boolean ordered, int capacity, java.lang.Class keyArrayType, java.lang.Class valueArrayType)ArrayMap(int capacity)Creates an ordered map with the specified capacity.ArrayMap(ArrayMap array)Creates a new map containing the elements in the specified map.ArrayMap(java.lang.Class keyArrayType, java.lang.Class valueArrayType)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()voidclear(int maximumCapacity)Clears the map and reduces the size of the backing arrays to be the specified capacity if they are larger.booleancontainsKey(K key)booleancontainsValue(V value, boolean identity)voidensureCapacity(int additionalCapacity)Increases the size of the backing arrays to accommodate the specified number of additional entries.ArrayMap.Entries<K,V>entries()Returns an iterator for the entries in the map.booleanequals(java.lang.Object obj)booleanequalsIdentity(java.lang.Object obj)Uses == for comparison of each value.KfirstKey()VfirstValue()Vget(K key)Returns the value (which may be null) for the specified key, or null if the key is not in the map.Vget(K key, V defaultValue)Returns the value (which may be null) for the specified key, or the default value if the key is not in the map.KgetKey(V value, boolean identity)Returns the key for the specified value.KgetKeyAt(int index)VgetValueAt(int index)inthashCode()intindexOfKey(K key)intindexOfValue(V value, boolean identity)voidinsert(int index, K key, V value)booleanisEmpty()Returns true if the map is empty.java.util.Iterator<ObjectMap.Entry<K,V>>iterator()ArrayMap.Keys<K>keys()Returns an iterator for the keys in the map.booleannotEmpty()Returns true if the map has one or more items.KpeekKey()Returns the last key.VpeekValue()Returns the last value.intput(K key, V value)intput(K key, V value, int index)voidputAll(ArrayMap<? extends K,? extends V> map)voidputAll(ArrayMap<? extends K,? extends V> map, int offset, int length)voidremoveIndex(int index)Removes and returns the key/values pair at the specified index.VremoveKey(K key)booleanremoveValue(V value, boolean identity)protected voidresize(int newSize)voidreverse()voidsetKey(int index, K key)voidsetValue(int index, V value)voidshrink()Reduces the size of the backing arrays to the size of the actual number of entries.voidshuffle()java.lang.StringtoString()voidtruncate(int newSize)Reduces the size of the arrays to the specified size.ArrayMap.Values<V>values()Returns an iterator for the values in the map.
-
-
-
Constructor Detail
-
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
public ArrayMap(boolean ordered, int capacity, java.lang.Class keyArrayType, java.lang.Class valueArrayType)- 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
public ArrayMap(java.lang.Class keyArrayType, java.lang.Class valueArrayType)
-
ArrayMap
public ArrayMap(ArrayMap array)
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 Detail
-
get
@Null public V get(K key)
Returns the value (which may be null) for the specified key, or null if the key is not in the map. Note this does a .equals() comparison of each key in reverse order until the specified key is found.
-
get
@Null public V get(K key, @Null V defaultValue)
Returns the value (which may be null) for the specified key, or the default value if the key is not in the map. Note this does a .equals() comparison of each key in reverse order until the specified key is found.
-
getKey
@Null public K getKey(V value, boolean identity)
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
public K getKeyAt(int index)
-
getValueAt
public V getValueAt(int index)
-
firstKey
public K firstKey()
-
firstValue
public V firstValue()
-
setKey
public void setKey(int index, K key)
-
setValue
public void setValue(int index, V value)
-
containsKey
public boolean containsKey(K key)
-
containsValue
public boolean containsValue(V value, boolean identity)
- Parameters:
identity- If true, == comparison will be used. If false, .equals() comparison will be used.
-
indexOfKey
public int indexOfKey(K key)
-
indexOfValue
public int indexOfValue(V value, boolean identity)
-
removeValue
public boolean removeValue(V value, boolean identity)
-
removeIndex
public void removeIndex(int index)
Removes and returns the key/values pair at the specified index.
-
notEmpty
public boolean notEmpty()
Returns true if the map has one or more items.
-
isEmpty
public boolean isEmpty()
Returns true if the map is empty.
-
peekKey
public K peekKey()
Returns the last key.
-
peekValue
public V 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()
- Overrides:
hashCodein classjava.lang.Object
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equalsin classjava.lang.Object
-
equalsIdentity
public boolean equalsIdentity(java.lang.Object obj)
Uses == for comparison of each value.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
iterator
public java.util.Iterator<ObjectMap.Entry<K,V>> iterator()
- Specified by:
iteratorin interfacejava.lang.Iterable<K>
-
entries
public ArrayMap.Entries<K,V> entries()
Returns an iterator for the entries in the map. Remove is supported.If
Collections.allocateIteratorsis false, the same iterator instance is returned each time this method is called. Use theArrayMap.Entriesconstructor for nested or multithreaded iteration.- See Also:
Collections.allocateIterators
-
values
public ArrayMap.Values<V> values()
Returns an iterator for the values in the map. Remove is supported.If
Collections.allocateIteratorsis false, the same iterator instance is returned each time this method is called. Use theArrayMap.Entriesconstructor for nested or multithreaded iteration.- See Also:
Collections.allocateIterators
-
keys
public ArrayMap.Keys<K> keys()
Returns an iterator for the keys in the map. Remove is supported.If
Collections.allocateIteratorsis false, the same iterator instance is returned each time this method is called. Use theArrayMap.Entriesconstructor for nested or multithreaded iteration.- See Also:
Collections.allocateIterators
-
-