Class MapsP
CollectionsPlume.-
Method Summary
Modifier and TypeMethodDescriptionstatic <K,V, M extends @Nullable Map<K, V>>
@PolyNull McloneElements(@PolyNull M orig) Returns a copy oforig, where each key and value in the result is a clone of the corresponding element oforig.static <K,V, M extends @Nullable Map<K, V>>
@PolyNull McloneValues(@PolyNull M orig) Returns a copy oforig, where each value of the result is a clone of the corresponding value oforig, but the keys are the same objects.static <K,V> Map<K, V> createLruCache(@org.checkerframework.checker.index.qual.Positive int size) Creates a LRU cache.static <K extends @Nullable DeepCopyable<K>,V extends @Nullable DeepCopyable<V>, M extends @Nullable Map<K, V>>
@PolyNull MdeepCopy(@PolyNull M orig) Returns a copy oforig, where each key and value in the result is a deep copy (according to theDeepCopyableinterface) of the corresponding element oforig.static <K,V extends @Nullable DeepCopyable<V>, M extends @Nullable Map<K, V>>
@PolyNull MdeepCopyValues(@PolyNull M orig) Returns a copy oforig, where each value of the result is a deep copy (according to theDeepCopyableinterface) of the corresponding value oforig, but the keys are the same objects.incrementMap(Map<K, Integer> m, K key) Increments the Integer which is indexed by key in the Map.incrementMap(Map<K, Integer> m, K key, int count) Increments the Integer which is indexed by key in the Map.static intmapCapacity(int numElements) Given an expected number of elements, returns the capacity that should be passed to a HashMap or HashSet constructor, so that the set or map will not resize.static intmapCapacity(Collection<?> c) Given a collection, returns the capacity that should be passed to a HashMap or HashSet constructor, so that the set or map will not resize.static intmapCapacity(Map<?, ?> m) Given a map, returns the capacity that should be passed to a HashMap or HashSet constructor, so that the set or map will not resize.static <T> intmapCapacity(T[] a) Given an array, returns the capacity that should be passed to a HashMap or HashSet constructor, so that the set or map will not resize.mapToString(Appendable sb, Map<K, V> m, String linePrefix) Deprecated.mapToString(Map<K, V> m) Deprecated.mapToStringAndClassMultiLine(Map<K, V> m) Convert a map to a multi-line string representation, which includes the runtime class of keys and values.mapToStringAndClassMultiLine(Map<K, V> m, String linePrefix) Convert a map to a multi-line string representation, which includes the runtime class of keys and values.mapToStringMultiLine(Appendable sb, Map<K, V> m, String linePrefix) Write a multi-line representation of the map into the given Appendable (e.g., a StringBuilder), including a final line separator (unless the map is empty).mapToStringMultiLine(Map<K, V> m) Returns a multi-line string representation of a map.mapToStringMultiLine(Map<K, V> m, String linePrefix) Returns a multi-line string representation of a map.static <K extends Comparable<? super K>,V>
Collection<@KeyFor("#1") K>sortedKeySet(Map<K, V> m) Returns a sorted version of m.keySet().static <K,V> Collection<@KeyFor("#1") K> sortedKeySet(Map<K, V> m, Comparator<K> comparator) Returns a sorted version of m.keySet().
-
Method Details
-
incrementMap
Increments the Integer which is indexed by key in the Map. Sets the value to 1 if not currently mapped.- Type Parameters:
K- type of keys in the map- Parameters:
m- map from K to Integerkey- the key whose value will be incremented- Returns:
- the old value, before it was incremented; this might be null
- Throws:
Error- if the key is in the Map but maps to a non-Integer
-
incrementMap
public static <K extends @NonNull Object> @Nullable Integer incrementMap(Map<K, Integer> m, K key, int count) Increments the Integer which is indexed by key in the Map. Sets the value tocountif not currently mapped.- Type Parameters:
K- type of keys in the map- Parameters:
m- map from K to Integerkey- the key whose value will be incrementedcount- how much to increment the value by- Returns:
- the old value, before it was incremented; this might be null
- Throws:
Error- if the key is in the Map but maps to a non-Integer
-
sortedKeySet
public static <K extends Comparable<? super K>,V> Collection<@KeyFor("#1") K> sortedKeySet(Map<K, V> m) Returns a sorted version of m.keySet().- Type Parameters:
K- type of the map keysV- type of the map values- Parameters:
m- a map whose keyset will be sorted- Returns:
- a sorted version of m.keySet()
-
sortedKeySet
Returns a sorted version of m.keySet().- Type Parameters:
K- type of the map keysV- type of the map values- Parameters:
m- a map whose keyset will be sortedcomparator- the Comparator to use for sorting- Returns:
- a sorted version of m.keySet()
-
mapCapacity
public static int mapCapacity(int numElements) Given an expected number of elements, returns the capacity that should be passed to a HashMap or HashSet constructor, so that the set or map will not resize.- Parameters:
numElements- the maximum expected number of elements in the map or set- Returns:
- the initial capacity to pass to a HashMap or HashSet constructor
-
mapCapacity
public static <T> int mapCapacity(T[] a) Given an array, returns the capacity that should be passed to a HashMap or HashSet constructor, so that the set or map will not resize.- Type Parameters:
T- the type of elements of the array- Parameters:
a- an array whose length is the maximum expected number of elements in the map or set- Returns:
- the initial capacity to pass to a HashMap or HashSet constructor
-
mapCapacity
Given a collection, returns the capacity that should be passed to a HashMap or HashSet constructor, so that the set or map will not resize.- Parameters:
c- a collection whose size is the maximum expected number of elements in the map or set- Returns:
- the initial capacity to pass to a HashMap or HashSet constructor
-
mapCapacity
Given a map, returns the capacity that should be passed to a HashMap or HashSet constructor, so that the set or map will not resize.- Parameters:
m- a map whose size is the maximum expected number of elements in the map or set- Returns:
- the initial capacity to pass to a HashMap or HashSet constructor
-
deepCopy
public static <K extends @Nullable DeepCopyable<K>,V extends @Nullable DeepCopyable<V>, @PolyNull M deepCopyM extends @Nullable Map<K, V>> (@PolyNull M orig) Returns a copy oforig, where each key and value in the result is a deep copy (according to theDeepCopyableinterface) of the corresponding element oforig.- Type Parameters:
K- the type of keys of the mapV- the type of values of the mapM- the type of the map- Parameters:
orig- a map- Returns:
- a copy of
orig, as described above
-
deepCopyValues
public static <K,V extends @Nullable DeepCopyable<V>, @PolyNull M deepCopyValuesM extends @Nullable Map<K, V>> (@PolyNull M orig) Returns a copy oforig, where each value of the result is a deep copy (according to theDeepCopyableinterface) of the corresponding value oforig, but the keys are the same objects.- Type Parameters:
K- the type of keys of the mapV- the type of values of the mapM- the type of the map- Parameters:
orig- a map- Returns:
- a copy of
orig, as described above
-
createLruCache
public static <K,V> Map<K,V> createLruCache(@org.checkerframework.checker.index.qual.Positive int size) Creates a LRU cache.You might want to consider using a
WeakHashMaporWeakIdentityHashMapinstead- Type Parameters:
K- the type of keysV- the type of values- Parameters:
size- size of the cache- Returns:
- a new cache with the provided size
-
cloneElements
Returns a copy oforig, where each key and value in the result is a clone of the corresponding element oforig.- Type Parameters:
K- the type of keys of the mapV- the type of values of the mapM- the type of the map- Parameters:
orig- a map- Returns:
- a copy of
orig, as described above
-
cloneValues
Returns a copy oforig, where each value of the result is a clone of the corresponding value oforig, but the keys are the same objects.- Type Parameters:
K- the type of keys of the mapV- the type of values of the mapM- the type of the map- Parameters:
orig- a map- Returns:
- a copy of
orig, as described above
-
mapToString
@Deprecated public static <K extends @Signed @Nullable Object,V extends @Signed @Nullable Object> void mapToString(Appendable sb, Map<K, V> m, String linePrefix) Deprecated.Write a multi-line representation of the map into the given Appendable (e.g., a StringBuilder), including a final line separator (unless the map is empty).Each line has the form "{linePrefix}{key} => {value}"
This is less expensive than
sb.append(mapToStringMultiLine(m)).- Type Parameters:
K- type of map keysV- type of map values- Parameters:
sb- an Appendable (such as StringBuilder) to which to write a multi-line string representation of mm- map to be converted to a stringlinePrefix- a prefix to put at the beginning of each line
-
mapToStringMultiLine
public static <K extends @Signed @Nullable Object,V extends @Signed @Nullable Object> void mapToStringMultiLine(Appendable sb, Map<K, V> m, String linePrefix) Write a multi-line representation of the map into the given Appendable (e.g., a StringBuilder), including a final line separator (unless the map is empty).Each line has the form "{linePrefix}{key} => {value}".
This is less expensive than
sb.append(mapToStringMultiLine(m)).- Type Parameters:
K- type of map keysV- type of map values- Parameters:
sb- an Appendable (such as StringBuilder) to which to write a multi-line string representation of mm- map to be converted to a stringlinePrefix- a prefix to put at the beginning of each line
-
mapToString
@Deprecated @SideEffectFree public static <K extends @Signed @Nullable Object,V extends @Signed @Nullable Object> String mapToString(Map<K, V> m) Deprecated.Returns a multi-line string representation of a map. Each key-value pair appears on its own line, with no indentation. The last line does not end with a line separator.Each line has the form "{linePrefix}{key} => {value}".
- Type Parameters:
K- type of map keysV- type of map values- Parameters:
m- map to be converted to a string- Returns:
- a multi-line string representation of m
-
mapToStringMultiLine
@SideEffectFree public static <K extends @Signed @Nullable Object,V extends @Signed @Nullable Object> String mapToStringMultiLine(Map<K, V> m) Returns a multi-line string representation of a map. Each key-value pair appears on its own line, with no indentation. The last line does not end with a line separator.Each line has the form "{key} => {value}".
- Type Parameters:
K- type of map keysV- type of map values- Parameters:
m- map to be converted to a string- Returns:
- a multi-line string representation of the map
-
mapToStringMultiLine
@SideEffectFree public static <K extends @Signed @Nullable Object,V extends @Signed @Nullable Object> String mapToStringMultiLine(Map<K, V> m, String linePrefix) Returns a multi-line string representation of a map. Each key-value pair appears on its own line, with no indentation. The last line does not end with a line separator.Each line has the form "{linePrefix}{key} => {value}".
- Type Parameters:
K- type of map keysV- type of map values- Parameters:
m- map to be converted to a stringlinePrefix- a prefix to put at the beginning of each line- Returns:
- a multi-line string representation of the map
-
mapToStringAndClassMultiLine
@SideEffectFree public static <K extends @Signed @Nullable Object,V extends @Signed @Nullable Object> String mapToStringAndClassMultiLine(Map<K, V> m) Convert a map to a multi-line string representation, which includes the runtime class of keys and values. The last line does not end with a line separator.Each line has the form "{key} [{key.getClass()}] => {value} [{value.getClass()}]", where the "{}" characters indicate interpolation and the "[]" characters are literally present.
- Type Parameters:
K- type of map keysV- type of map values- Parameters:
m- a map- Returns:
- a string representation of the map
-
mapToStringAndClassMultiLine
@SideEffectFree public static <K extends @Signed @Nullable Object,V extends @Signed @Nullable Object> String mapToStringAndClassMultiLine(Map<K, V> m, String linePrefix) Convert a map to a multi-line string representation, which includes the runtime class of keys and values. The last line does not end with a line separator.Each line has the form "{linePrefix}{key} [{key.getClass()}] => {value} [{value.getClass()}]", where the "{}" characters indicate interpolation and the "[]" characters are literally present.
- Type Parameters:
K- type of map keysV- type of map values- Parameters:
m- a maplinePrefix- a prefix to put at the beginning of each line- Returns:
- a string representation of the map
-
mapToStringMultiLine(Appendable, Map, String)