Package org.plumelib.util
Class ArrayMap<K extends @UnknownSignedness Object,V extends @UnknownSignedness Object>
java.lang.Object
java.util.AbstractMap<K,V>
org.plumelib.util.ArrayMap<K,V>
- Type Parameters:
K- the type of keys maintained by this mapV- the type of mapped values
- All Implemented Interfaces:
Map<K,V>
public class ArrayMap<K extends @UnknownSignedness Object,V extends @UnknownSignedness Object>
extends AbstractMap<K,V>
A map backed by two arrays. It permits null keys and values, and its iterator has deterministic
ordering.
Compared to a HashMap or LinkedHashMap: For very small maps, this uses much less space, has comparable performance, and (like a LinkedHashMap) is deterministic, with elements returned in the order their keys were inserted. For large maps, this is significantly less performant than other map implementations.
Compared to a TreeMap: This uses somewhat less space, and it does not require defining a comparator. This isn't sorted but does have deterministic ordering. For large maps, this is significantly less performant than other map implementations.
A number of other ArrayMap implementations exist, including
- android.util.ArrayMap
- com.google.api.client.util.ArrayMap
- it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap
- oracle.dss.util.ArrayMap
- org.apache.myfaces.trinidad.util.ArrayMap
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) classAn iterator over the ArrayMap.(package private) final classAn entrySet() entry.(package private) final classAn iterator over the entries.(package private) final classRepresents a view of the entries.(package private) final classAn iterator over the keys.(package private) final classRepresents a view of the keys.(package private) final classAn iterator over the values.(package private) final classRepresents a view of the values.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 TypeFieldDescriptionThe view of the entries.private @Nullable K @SameLen("values") []The keys.A view of the keys.private @org.checkerframework.checker.index.qual.NonNegative,@org.checkerframework.checker.index.qual.LessThan({"keys.length + 1"}),@org.checkerframework.checker.index.qual.IndexOrHigh({"keys", "values"}) intThe number of used mappings in the representation of this.(package private) intThe number of times this map's size has been modified by adding or removing an element (changing the value associated with a key does not count as a change).private @Nullable V @SameLen("keys") []The values.(package private) @MonotonicNonNull Collection<V>The view of the values. -
Constructor Summary
ConstructorsModifierConstructorDescriptionArrayMap()Constructs an emptyArrayMapwith the default initial capacity.ArrayMap(int initialCapacity) Constructs an emptyArrayMapwith the specified initial capacity.Constructs a newArrayMapwith the same mappings as the givenMap.privateArrayMap(K @SameLen("values") [] keys, V @SameLen("keys") [] values, @org.checkerframework.checker.index.qual.LTEqLengthOf({"keys", "values"}) int size) Private constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()clone()Returns a copy of this.@PolyNull V@PolyNull VcomputeIfAbsent(K key, Function<? super K, ? extends @PolyNull V> mappingFunction) @PolyNull VcomputeIfPresent(K key, BiFunction<? super K, ? super V, ? extends @PolyNull V> remappingFunction) private booleancontainsEntry(@GuardSatisfied @Nullable @UnknownSignedness Object key, @GuardSatisfied @Nullable @UnknownSignedness Object value) Returns true if this map contains the given mapping.booleancontainsKey(@GuardSatisfied @Nullable @UnknownSignedness Object key) booleancontainsValue(@GuardSatisfied @Nullable @UnknownSignedness Object value) entrySet()voidforEach(BiConsumer<? super K, ? super V> action) @Nullable VgetOrDefault(@GuardSatisfied @Nullable @UnknownSignedness Object key, V defaultValue) private @Nullable VgetOrNull(@org.checkerframework.checker.index.qual.GTENegativeOne int index) Returns the value at the given index, or null if the index is -1.private voidgrow()Increases the capacity of the arrays.private intindexOfKey(@GuardSatisfied @Nullable @UnknownSignedness Object key) Returns the index of the given key, or -1 if it does not appear.private intindexOfValue(@GuardSatisfied @Nullable @UnknownSignedness Object value) Returns the index of the given value, or -1 if it does not appear.booleanisEmpty()keySet()@PolyNull Vmerge(K key, @NonNull V value, BiFunction<? super V, ? super V, ? extends @PolyNull V> remappingFunction) static <K,V> Map<K, V> newArrayMapOrHashMap(int capacity) Returns a new ArrayMap or HashMap with the given capacity.static <K,V> Map<K, V> newArrayMapOrHashMap(Map<K, V> m) Returns a new ArrayMap or HashMap with the given elements.static <K,V> Map<K, V> newArrayMapOrLinkedHashMap(int capacity) Returns a new ArrayMap or LinkedHashMap with the given capacity.static <K,V> Map<K, V> newArrayMapOrLinkedHashMap(Map<K, V> m) Returns a new ArrayMap or LinkedHashMap with the given elements.private voidAdds the (key, value) mapping to this.@Nullable Vvoid@Nullable VputIfAbsent(K key, V value) @Nullable Vbooleanremove(@GuardSatisfied @Nullable @UnknownSignedness Object key, @GuardSatisfied @Nullable @UnknownSignedness Object value) private booleanremoveIndex(@org.checkerframework.checker.index.qual.GTENegativeOne int index) Remove the mapping at the given index.@Nullable VbooleanvoidreplaceAll(BiFunction<? super K, ? super V, ? extends V> function) (package private) Stringrepr()Returns the internal representation, printed.@org.checkerframework.checker.index.qual.NonNegative intsize()values()Methods inherited from class java.util.AbstractMap
equals, hashCode, toString
-
Field Details
-
keys
The keys. Null if capacity=0. -
values
The values. Null if capacity=0. -
size
private @org.checkerframework.checker.index.qual.NonNegative,@org.checkerframework.checker.index.qual.LessThan({"keys.length + 1"}),@org.checkerframework.checker.index.qual.IndexOrHigh({"keys", "values"}) int sizeThe number of used mappings in the representation of this. -
sizeModificationCount
transient int sizeModificationCountThe number of times this map's size has been modified by adding or removing an element (changing the value associated with a key does not count as a change). This field is used to make view iterators fail-fast. -
keySet
A view of the keys. -
valuesCollection
@MonotonicNonNull Collection<V extends @UnknownSignedness Object> valuesCollectionThe view of the values. -
entrySet
@MonotonicNonNull Set<Map.Entry<@KeyFor("this") K extends @UnknownSignedness Object,V extends @UnknownSignedness Object>> entrySetThe view of the entries.
-
-
Constructor Details
-
ArrayMap
@SideEffectFree public ArrayMap(int initialCapacity) Constructs an emptyArrayMapwith the specified initial capacity.- Parameters:
initialCapacity- the initial capacity- Throws:
IllegalArgumentException- if the initial capacity is negative
-
ArrayMap
@SideEffectFree public ArrayMap()Constructs an emptyArrayMapwith the default initial capacity. -
ArrayMap
@SideEffectFree private ArrayMap(K @SameLen("values") [] keys, V @SameLen("keys") [] values, @org.checkerframework.checker.index.qual.LTEqLengthOf({"keys", "values"}) int size) Private constructor. Installs the given objects in this as its representation, without making defensive copies.- Parameters:
keys- the keysvalues- the valuessize- the number of used items in the arrays; may be less than their lengths
-
ArrayMap
Constructs a newArrayMapwith the same mappings as the givenMap.- Parameters:
m- the map whose mappings are to be placed in this map- Throws:
NullPointerException- if the given map is null
-
-
Method Details
-
newArrayMapOrHashMap
Returns a new ArrayMap or HashMap with the given capacity. Uses an ArrayMap if the capacity is small, and a HashMap otherwise.- Type Parameters:
K- the type of the keysV- the type of the values- Parameters:
capacity- the expected maximum number of elements in the set- Returns:
- a new ArrayMap or HashMap with the given capacity
-
newArrayMapOrHashMap
Returns a new ArrayMap or HashMap with the given elements. Uses an ArrayMap if the capacity is small, and a HashMap otherwise.- Type Parameters:
K- the type of the keysV- the type of the values- Parameters:
m- the elements to put in the returned set- Returns:
- a new ArrayMap or HashMap with the given elements
-
newArrayMapOrLinkedHashMap
Returns a new ArrayMap or LinkedHashMap with the given capacity. Uses an ArrayMap if the capacity is small, and a LinkedHashMap otherwise.- Type Parameters:
K- the type of the keysV- the type of the values- Parameters:
capacity- the expected maximum number of elements in the set- Returns:
- a new ArrayMap or LinkedHashMap with the given capacity
-
newArrayMapOrLinkedHashMap
Returns a new ArrayMap or LinkedHashMap with the given elements. Uses an ArrayMap if the capacity is small, and a LinkedHashMap otherwise.- Type Parameters:
K- the type of the keysV- the type of the values- Parameters:
m- the elements to put in the returned set- Returns:
- a new ArrayMap or LinkedHashMap with the given elements
-
put
@EnsuresKeyFor(value="#2", map="this") private void put(@org.checkerframework.checker.index.qual.GTENegativeOne int index, K key, V value) Adds the (key, value) mapping to this.- Parameters:
index- the index ofkeyinkeys. If -1, add a new mapping. Otherwise, replace the mapping atindex.key- the keyvalue- the value
-
grow
private void grow()Increases the capacity of the arrays. -
removeIndex
private boolean removeIndex(@org.checkerframework.checker.index.qual.GTENegativeOne int index) Remove the mapping at the given index. Does nothing if index is -1.- Parameters:
index- the index of the mapping to remove- Returns:
- true if this map was modified
-
size
@Pure public @org.checkerframework.checker.index.qual.NonNegative int size() -
isEmpty
@Pure public boolean isEmpty() -
indexOfKey
Returns the index of the given key, or -1 if it does not appear. UsesObjects.equalsfor comparison.- Parameters:
key- a key to find- Returns:
- the index of the given key, or -1 if it does not appear
-
indexOfValue
Returns the index of the given value, or -1 if it does not appear. UsesObjects.equalsfor comparison.- Parameters:
value- a value to find- Returns:
- the index of the given value, or -1 if it does not appear
-
containsKey
- Specified by:
containsKeyin interfaceMap<K extends @UnknownSignedness Object,V extends @UnknownSignedness Object> - Overrides:
containsKeyin classAbstractMap<K extends @UnknownSignedness Object,V extends @UnknownSignedness Object>
-
containsValue
- Specified by:
containsValuein interfaceMap<K extends @UnknownSignedness Object,V extends @UnknownSignedness Object> - Overrides:
containsValuein classAbstractMap<K extends @UnknownSignedness Object,V extends @UnknownSignedness Object>
-
containsEntry
@Pure private boolean containsEntry(@GuardSatisfied @Nullable @UnknownSignedness Object key, @GuardSatisfied @Nullable @UnknownSignedness Object value) Returns true if this map contains the given mapping.- Parameters:
key- the keyvalue- the value- Returns:
- true if this map contains the given mapping
-
get
-
getOrNull
@Pure private @Nullable V getOrNull(@org.checkerframework.checker.index.qual.GTENegativeOne int index) Returns the value at the given index, or null if the index is -1.- Parameters:
index- the index- Returns:
- the value at the given index, or null if the index is -1
-
put
-
remove
-
putAll
-
clear
public void clear() -
keySet
-
values
-
entrySet
-
getOrDefault
-
forEach
-
replaceAll
-
putIfAbsent
-
remove
-
replace
-
replace
-
computeIfAbsent
-
computeIfPresent
-
compute
-
merge
-
clone
Returns a copy of this. -
repr
Returns the internal representation, printed.- Returns:
- the internal representation, printed
-