Class IntShortMap
- All Implemented Interfaces:
Cloneable,Map<Integer,,Short> IntShortOrderedIterable
This implementation is optimized for small sizes and appending keys in
ascending order. Random mutations trigger array copies that can slow
performance down considerably. Accessing values run in O(log n) time.
Keys are maintained in ascending order, so iteration occurs also in ascending
order.
- Since:
- 1.58
- Version:
- 1.0
- Author:
- matt
-
Nested Class Summary
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K extends Object,V extends Object>, AbstractMap.SimpleImmutableEntry<K extends Object, V extends Object> -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe default initial capacity.static final shortThe default value that causesNoSuchElementExceptionto be thrown ingetValue(int). -
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor.IntShortMap(int initialCapacity) Constructor.IntShortMap(int initialCapacity, short notFoundValue) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()clone()booleancompact()Free up excess capacity.booleancontainsKey(int k) Test if a primitive key value exists in this map.booleancontainsKey(Object key) booleancontainsValue(Object value) entrySet()voidforEachOrdered(int min, int max, IntShortBiConsumer action) Iterate over a range of key/value pairs in this map.voidforEachOrdered(IntShortBiConsumer action) Iterate over all key/value pairs in this map.get(int k) Get the value for a given key.intGet the current capacity.shortgetValue(int k) Get the value for a given key.keySet()putValue(int k, int value) Get primitive values.putValue(int k, short value) Put primitive values.intsize()toString()Get a view of this map with unsigned integer values.values()Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, isEmpty, merge, putAll, putIfAbsent, remove, remove, replace, replace, replaceAll
-
Field Details
-
DEFAULT_INITIAL_CAPACITY
public static final int DEFAULT_INITIAL_CAPACITYThe default initial capacity.- See Also:
-
VALUE_NO_SUCH_ELEMENT
public static final short VALUE_NO_SUCH_ELEMENTThe default value that causesNoSuchElementExceptionto be thrown ingetValue(int).- See Also:
-
-
Constructor Details
-
IntShortMap
public IntShortMap()Default constructor.Defaults to returning 0 for nonexistent keys in
getValue(int). -
IntShortMap
public IntShortMap(int initialCapacity) Constructor.Defaults to returning 0 for nonexistent keys in
getValue(int).- Parameters:
initialCapacity- the initial capacity- Throws:
IllegalArgumentException- ifinitialCapacityis less than 1
-
IntShortMap
public IntShortMap(int initialCapacity, short notFoundValue) Constructor.- Parameters:
initialCapacity- the initial capacitynotFoundValue- the value to return ingetValue(int)if a key is not found, orVALUE_NO_SUCH_ELEMENTto throw aNoSuchElementException- Throws:
IllegalArgumentException- ifinitialCapacityis less than 1
-
-
Method Details
-
toString
- Overrides:
toStringin classAbstractMap<Integer,Short>
-
clone
- Overrides:
clonein classAbstractMap<Integer,Short>
-
entrySet
-
size
public int size() -
forEachOrdered
Iterate over all key/value pairs in this map.This method of iteration can be more efficient than iterating via
entrySet()because no intermediateSet,Iterator, orEntryobjects are created, and not primitive boxing occurs.- Specified by:
forEachOrderedin interfaceIntShortOrderedIterable- Parameters:
action- the consumer to handle the key/value pairs
-
forEachOrdered
Iterate over a range of key/value pairs in this map.This method of iteration can be more efficient than iterating via
entrySet()because no intermediateSet,Iterator, orEntryobjects are created, and not primitive boxing occurs.- Specified by:
forEachOrderedin interfaceIntShortOrderedIterable- Parameters:
min- the minimum key value (inclusive)max- the maximum key value (exclusive)action- the consumer to handle the key/value pairs
-
keySet
-
values
-
containsValue
- Specified by:
containsValuein interfaceMap<Integer,Short> - Overrides:
containsValuein classAbstractMap<Integer,Short>
-
containsKey
- Specified by:
containsKeyin interfaceMap<Integer,Short> - Overrides:
containsKeyin classAbstractMap<Integer,Short>
-
containsKey
public boolean containsKey(int k) Test if a primitive key value exists in this map.- Parameters:
k- the key to test- Returns:
- true if the key exists in this map
-
clear
public void clear() -
getCapacity
public int getCapacity()Get the current capacity.- Returns:
- the capacity
-
compact
public boolean compact()Free up excess capacity.- Returns:
- true if any capacity was freed
-
get
-
get
Get the value for a given key.- Parameters:
k- the key of the value to get- Returns:
- the associated value, or null if
kis not present
-
getValue
public short getValue(int k) Get the value for a given key.- Parameters:
k- the key of the value to get- Returns:
- the associated value
- Throws:
NoSuchElementException- ifkis not present
-
putValue
Get primitive values.- Parameters:
k- the keyvalue- the value, which will be down-cast to a short- Returns:
- the previous value associated with
k, or null if none
-
putValue
Put primitive values.- Parameters:
k- the keyvalue- the value- Returns:
- the previous value associated with
k, or null if none
-
put
-
unsignedMap
Get a view of this map with unsigned integer values.- Returns:
- a new map, backed by this map's data, where the values are returned as unsigned integers
-