Package com.google.common.collect
Class RegularImmutableMap<K,V>
java.lang.Object
com.google.common.collect.ImmutableMap<K,V>
com.google.common.collect.RegularImmutableMap<K,V>
- All Implemented Interfaces:
Serializable
,Map<K,
V>
Implementation of
ImmutableMap
used for 0 entries and for 2+ entries. Additional
implementations exist for particular cases, like ImmutableTable
views and hash flooding.
(This doc discusses ImmutableMap
subclasses only for the JRE flavor; the Android flavor
differs.)-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static class
private static final class
private static final class
Nested classes/interfaces inherited from class com.google.common.collect.ImmutableMap
ImmutableMap.Builder<K,
V>, ImmutableMap.IteratorBasedImmutableMap<K, V>, ImmutableMap.SerializedForm<K, V> -
Field Summary
FieldsModifier and TypeFieldDescription(package private) static final ImmutableMap
<Object, Object> (package private) static final double
Maximum allowed false positive probability of detecting a hash flooding attack given random input.private final int
(package private) static final int
Maximum allowed length of a hash table bucket before falling back to a j.u.HashMap based implementation.(package private) static final double
Closed addressing tends to perform well even with high load factors.private static final long
private final ImmutableMapEntry<K,
V>[] Fields inherited from class com.google.common.collect.ImmutableMap
EMPTY_ENTRY_ARRAY
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
RegularImmutableMap
(Map.Entry<K, V>[] entries, ImmutableMapEntry<K, V>[] table, int mask) -
Method Summary
Modifier and TypeMethodDescription(package private) static <K,
V> ImmutableMapEntry <K, V> checkNoConflictInKeyBucket
(Object key, Object newValue, ImmutableMapEntry<K, V> keyBucketHead, boolean throwIfDuplicateKeys) Checks if the given key already appears in the hash chain starting atkeyBucketHead
.(package private) ImmutableSet
<Map.Entry<K, V>> (package private) ImmutableSet
<K> (package private) ImmutableCollection
<V> void
forEach
(BiConsumer<? super K, ? super V> action) (package private) static <K,
V> ImmutableMap <K, V> fromEntries
(Map.Entry<K, V>... entries) (package private) static <K,
V> ImmutableMap <K, V> fromEntryArray
(int n, Map.Entry<K, V>[] entryArray, boolean throwIfDuplicateKeys) Creates an ImmutableMap from the first n entries in entryArray.private static <K,
V> ImmutableMap <K, V> fromEntryArrayCheckingBucketOverflow
(int n, Map.Entry<K, V>[] entryArray, boolean throwIfDuplicateKeys) (package private) static <V> V
get
(Object key, ImmutableMapEntry<?, V>[] keyTable, int mask) (package private) boolean
(package private) static <K,
V> ImmutableMapEntry <K, V> makeImmutable
(Map.Entry<K, V> entry) Makes an entry usable internally by a new ImmutableMap.(package private) static <K,
V> ImmutableMapEntry <K, V> makeImmutable
(Map.Entry<K, V> entry, K key, V value) Makes an entry usable internally by a new ImmutableMap without rereading its contents.(package private) static <K,
V> Map.Entry<K, V>[] removeDuplicates
(Map.Entry<K, V>[] entries, int n, int newN, IdentityHashMap<Map.Entry<K, V>, Boolean> duplicates) Constructs a new entry array where each duplicated key from the original appears only once, at its first position but with its final value.int
size()
(package private) Object
Returns a serializable form of this object.Methods inherited from class com.google.common.collect.ImmutableMap
asMultimap, builder, builderWithExpectedSize, checkNoConflict, clear, compute, computeIfAbsent, computeIfPresent, conflictException, containsKey, containsValue, copyOf, copyOf, entryOf, entrySet, equals, getOrDefault, hashCode, isEmpty, isHashCodeFast, keyIterator, keySet, keySpliterator, merge, of, of, of, of, of, of, of, of, of, of, of, ofEntries, put, putAll, putIfAbsent, remove, remove, replace, replace, replaceAll, toImmutableMap, toImmutableMap, toString, values
-
Field Details
-
EMPTY
-
MAX_LOAD_FACTOR
static final double MAX_LOAD_FACTORClosed addressing tends to perform well even with high load factors. Being conservative here ensures that the table is still likely to be relatively sparse (hence it misses fast) while saving space.- See Also:
-
HASH_FLOODING_FPP
static final double HASH_FLOODING_FPPMaximum allowed false positive probability of detecting a hash flooding attack given random input.- See Also:
-
MAX_HASH_BUCKET_LENGTH
static final int MAX_HASH_BUCKET_LENGTHMaximum allowed length of a hash table bucket before falling back to a j.u.HashMap based implementation. Experimentally determined.- See Also:
-
entries
-
table
-
mask
private final transient int mask -
serialVersionUID
private static final long serialVersionUID- See Also:
-
-
Constructor Details
-
RegularImmutableMap
-
-
Method Details
-
fromEntries
-
fromEntryArray
static <K,V> ImmutableMap<K,V> fromEntryArray(int n, Map.Entry<K, V>[] entryArray, boolean throwIfDuplicateKeys) Creates an ImmutableMap from the first n entries in entryArray. This implementation may replace the entries in entryArray with its own entry objects (though they will have the same key/value contents), and may take ownership of entryArray. -
fromEntryArrayCheckingBucketOverflow
private static <K,V> ImmutableMap<K,V> fromEntryArrayCheckingBucketOverflow(int n, Map.Entry<K, V>[] entryArray, boolean throwIfDuplicateKeys) throws RegularImmutableMap.BucketOverflowException -
removeDuplicates
static <K,V> Map.Entry<K,V>[] removeDuplicates(Map.Entry<K, V>[] entries, int n, int newN, IdentityHashMap<Map.Entry<K, V>, Boolean> duplicates) Constructs a new entry array where each duplicated key from the original appears only once, at its first position but with its final value. Theduplicates
map is modified.- Parameters:
entries
- the original array of entries including duplicatesn
- the number of valid entries inentries
newN
- the expected number of entries once duplicates are removedduplicates
- a map of canonicalMap.Entry
objects for each duplicate key. This map will be updated by the method, setting each value to false as soon as theMap.Entry
has been included in the new entry array.- Returns:
- an array of
newN
entries where no key appears more than once.
-
makeImmutable
Makes an entry usable internally by a new ImmutableMap without rereading its contents. -
makeImmutable
Makes an entry usable internally by a new ImmutableMap. -
checkNoConflictInKeyBucket
@CheckForNull static <K,V> ImmutableMapEntry<K,V> checkNoConflictInKeyBucket(Object key, Object newValue, @CheckForNull ImmutableMapEntry<K, V> keyBucketHead, boolean throwIfDuplicateKeys) throws RegularImmutableMap.BucketOverflowExceptionChecks if the given key already appears in the hash chain starting atkeyBucketHead
. If it does not, then null is returned. If it does, then ifthrowIfDuplicateKeys
is true anIllegalArgumentException
is thrown, and otherwise the existingMap.Entry
is returned.- Throws:
IllegalArgumentException
- if another entry in the bucket has the same key andthrowIfDuplicateKeys
is trueRegularImmutableMap.BucketOverflowException
- if this bucket has too many entries, which may indicate a hash flooding attack
-
get
-
get
@CheckForNull static <V> V get(@CheckForNull Object key, @CheckForNull ImmutableMapEntry<?, V>[] keyTable, int mask) -
forEach
-
size
public int size() -
isPartialView
boolean isPartialView()- Specified by:
isPartialView
in classImmutableMap<K,
V>
-
createEntrySet
ImmutableSet<Map.Entry<K,V>> createEntrySet()- Specified by:
createEntrySet
in classImmutableMap<K,
V>
-
createKeySet
ImmutableSet<K> createKeySet()- Specified by:
createKeySet
in classImmutableMap<K,
V>
-
createValues
ImmutableCollection<V> createValues()- Specified by:
createValues
in classImmutableMap<K,
V>
-
writeReplace
Object writeReplace()Description copied from class:ImmutableMap
Returns a serializable form of this object. Non-public subclasses should not override this method. Publicly-accessible subclasses must override this method and should return a subclass of SerializedForm whose readResolve() method returns objects of the subclass type.- Overrides:
writeReplace
in classImmutableMap<K,
V>
-