Class Maps
- java.lang.Object
- 
- com.google.common.collect.Maps
 
- 
 @GwtCompatible(emulated=true) public final class Maps extends java.lang.Object Static utility methods pertaining toMapinstances (including instances ofSortedMap,BiMap, etc.). Also see this class's counterpartsLists,SetsandQueues.See the Guava User Guide article on Maps.- Since:
- 2.0 (imported from Google Collections Library)
 
- 
- 
Nested Class SummaryNested Classes Modifier and Type Class Description static interfaceMaps.EntryTransformer<K,V1,V2>A transformation of the value of a key-value pair, using both key and value as inputs.
 - 
Method SummaryAll Methods Static Methods Concrete Methods Modifier and Type Method Description static <A,B>
 Converter<A,B>asConverter(BiMap<A,B> bimap)Returns aConverterthat converts values usingbimap.get(), and whose inverse view converts values usingbimap.inverse().get().static <K,V>
 java.util.NavigableMap<K,V>asMap(java.util.NavigableSet<K> set, Function<? super K,V> function)Returns a view of the navigable set as a map, mapping keys from the set according to the specified function.static <K,V>
 java.util.Map<K,V>asMap(java.util.Set<K> set, Function<? super K,V> function)Returns a liveMapview whose keys are the contents ofsetand whose values are computed on demand usingfunction.static <K,V>
 java.util.SortedMap<K,V>asMap(java.util.SortedSet<K> set, Function<? super K,V> function)Returns a view of the sorted set as a map, mapping keys from the set according to the specified function.static <K,V>
 MapDifference<K,V>difference(java.util.Map<? extends K,? extends V> left, java.util.Map<? extends K,? extends V> right)Computes the difference between two maps.static <K,V>
 MapDifference<K,V>difference(java.util.Map<? extends K,? extends V> left, java.util.Map<? extends K,? extends V> right, Equivalence<? super V> valueEquivalence)Computes the difference between two maps.static <K,V>
 SortedMapDifference<K,V>difference(java.util.SortedMap<K,? extends V> left, java.util.Map<? extends K,? extends V> right)Computes the difference between two sorted maps, using the comparator of the left map, orOrdering.natural()if the left map uses the natural ordering of its elements.static <K,V>
 BiMap<K,V>filterEntries(BiMap<K,V> unfiltered, Predicate<? super java.util.Map.Entry<K,V>> entryPredicate)Returns a bimap containing the mappings inunfilteredthat satisfy a predicate.static <K,V>
 java.util.Map<K,V>filterEntries(java.util.Map<K,V> unfiltered, Predicate<? super java.util.Map.Entry<K,V>> entryPredicate)Returns a map containing the mappings inunfilteredthat satisfy a predicate.static <K,V>
 java.util.NavigableMap<K,V>filterEntries(java.util.NavigableMap<K,V> unfiltered, Predicate<? super java.util.Map.Entry<K,V>> entryPredicate)Returns a sorted map containing the mappings inunfilteredthat satisfy a predicate.static <K,V>
 java.util.SortedMap<K,V>filterEntries(java.util.SortedMap<K,V> unfiltered, Predicate<? super java.util.Map.Entry<K,V>> entryPredicate)Returns a sorted map containing the mappings inunfilteredthat satisfy a predicate.static <K,V>
 BiMap<K,V>filterKeys(BiMap<K,V> unfiltered, Predicate<? super K> keyPredicate)Returns a bimap containing the mappings inunfilteredwhose keys satisfy a predicate.static <K,V>
 java.util.Map<K,V>filterKeys(java.util.Map<K,V> unfiltered, Predicate<? super K> keyPredicate)Returns a map containing the mappings inunfilteredwhose keys satisfy a predicate.static <K,V>
 java.util.NavigableMap<K,V>filterKeys(java.util.NavigableMap<K,V> unfiltered, Predicate<? super K> keyPredicate)Returns a navigable map containing the mappings inunfilteredwhose keys satisfy a predicate.static <K,V>
 java.util.SortedMap<K,V>filterKeys(java.util.SortedMap<K,V> unfiltered, Predicate<? super K> keyPredicate)Returns a sorted map containing the mappings inunfilteredwhose keys satisfy a predicate.static <K,V>
 BiMap<K,V>filterValues(BiMap<K,V> unfiltered, Predicate<? super V> valuePredicate)Returns a bimap containing the mappings inunfilteredwhose values satisfy a predicate.static <K,V>
 java.util.Map<K,V>filterValues(java.util.Map<K,V> unfiltered, Predicate<? super V> valuePredicate)Returns a map containing the mappings inunfilteredwhose values satisfy a predicate.static <K,V>
 java.util.NavigableMap<K,V>filterValues(java.util.NavigableMap<K,V> unfiltered, Predicate<? super V> valuePredicate)Returns a navigable map containing the mappings inunfilteredwhose values satisfy a predicate.static <K,V>
 java.util.SortedMap<K,V>filterValues(java.util.SortedMap<K,V> unfiltered, Predicate<? super V> valuePredicate)Returns a sorted map containing the mappings inunfilteredwhose values satisfy a predicate.static ImmutableMap<java.lang.String,java.lang.String>fromProperties(java.util.Properties properties)Creates anImmutableMap<String, String>from aPropertiesinstance.static <K,V>
 java.util.Map.Entry<K,V>immutableEntry(K key, V value)Returns an immutable map entry with the specified key and value.static <K extends java.lang.Enum<K>,V>
 ImmutableMap<K,V>immutableEnumMap(java.util.Map<K,? extends V> map)Returns an immutable map instance containing the given entries.static <K extends java.lang.Enum<K>,V>
 java.util.EnumMap<K,V>newEnumMap(java.lang.Class<K> type)Creates anEnumMapinstance.static <K extends java.lang.Enum<K>,V>
 java.util.EnumMap<K,V>newEnumMap(java.util.Map<K,? extends V> map)Creates anEnumMapwith the same mappings as the specified map.static <K,V>
 java.util.HashMap<K,V>newHashMap()Creates a mutable, emptyHashMapinstance.static <K,V>
 java.util.HashMap<K,V>newHashMap(java.util.Map<? extends K,? extends V> map)Creates a mutableHashMapinstance with the same mappings as the specified map.static <K,V>
 java.util.HashMap<K,V>newHashMapWithExpectedSize(int expectedSize)Creates aHashMapinstance, with a high enough "initial capacity" that it should holdexpectedSizeelements without growth.static <K,V>
 java.util.IdentityHashMap<K,V>newIdentityHashMap()Creates anIdentityHashMapinstance.static <K,V>
 java.util.LinkedHashMap<K,V>newLinkedHashMap()Creates a mutable, empty, insertion-orderedLinkedHashMapinstance.static <K,V>
 java.util.LinkedHashMap<K,V>newLinkedHashMap(java.util.Map<? extends K,? extends V> map)Creates a mutable, insertion-orderedLinkedHashMapinstance with the same mappings as the specified map.static <K extends java.lang.Comparable,V>
 java.util.TreeMap<K,V>newTreeMap()Creates a mutable, emptyTreeMapinstance using the natural ordering of its elements.static <C,K extends C,V>
 java.util.TreeMap<K,V>newTreeMap(java.util.Comparator<C> comparator)Creates a mutable, emptyTreeMapinstance using the given comparator.static <K,V>
 java.util.TreeMap<K,V>newTreeMap(java.util.SortedMap<K,? extends V> map)Creates a mutableTreeMapinstance with the same mappings as the specified map and using the same ordering as the specified map.static <K,V>
 BiMap<K,V>synchronizedBiMap(BiMap<K,V> bimap)Returns a synchronized (thread-safe) bimap backed by the specified bimap.static <K,V>
 java.util.NavigableMap<K,V>synchronizedNavigableMap(java.util.NavigableMap<K,V> navigableMap)Returns a synchronized (thread-safe) navigable map backed by the specified navigable map.static <K,V>
 ImmutableMap<K,V>toMap(java.lang.Iterable<K> keys, Function<? super K,V> valueFunction)Returns an immutable map whose keys are the distinct elements ofkeysand whose value for each key was computed byvalueFunction.static <K,V>
 ImmutableMap<K,V>toMap(java.util.Iterator<K> keys, Function<? super K,V> valueFunction)Returns an immutable map whose keys are the distinct elements ofkeysand whose value for each key was computed byvalueFunction.static <K,V1,V2>
 java.util.Map<K,V2>transformEntries(java.util.Map<K,V1> fromMap, Maps.EntryTransformer<? super K,? super V1,V2> transformer)Returns a view of a map whose values are derived from the original map's entries.static <K,V1,V2>
 java.util.NavigableMap<K,V2>transformEntries(java.util.NavigableMap<K,V1> fromMap, Maps.EntryTransformer<? super K,? super V1,V2> transformer)Returns a view of a navigable map whose values are derived from the original navigable map's entries.static <K,V1,V2>
 java.util.SortedMap<K,V2>transformEntries(java.util.SortedMap<K,V1> fromMap, Maps.EntryTransformer<? super K,? super V1,V2> transformer)Returns a view of a sorted map whose values are derived from the original sorted map's entries.static <K,V1,V2>
 java.util.Map<K,V2>transformValues(java.util.Map<K,V1> fromMap, Function<? super V1,V2> function)Returns a view of a map where each value is transformed by a function.static <K,V1,V2>
 java.util.NavigableMap<K,V2>transformValues(java.util.NavigableMap<K,V1> fromMap, Function<? super V1,V2> function)Returns a view of a navigable map where each value is transformed by a function.static <K,V1,V2>
 java.util.SortedMap<K,V2>transformValues(java.util.SortedMap<K,V1> fromMap, Function<? super V1,V2> function)Returns a view of a sorted map where each value is transformed by a function.static <K,V>
 ImmutableMap<K,V>uniqueIndex(java.lang.Iterable<V> values, Function<? super V,K> keyFunction)Returns an immutable map for which theMap.values()are the given elements in the given order, and each key is the product of invoking a supplied function on its corresponding value.static <K,V>
 ImmutableMap<K,V>uniqueIndex(java.util.Iterator<V> values, Function<? super V,K> keyFunction)Returns an immutable map for which theMap.values()are the given elements in the given order, and each key is the product of invoking a supplied function on its corresponding value.static <K,V>
 BiMap<K,V>unmodifiableBiMap(BiMap<? extends K,? extends V> bimap)Returns an unmodifiable view of the specified bimap.static <K,V>
 java.util.NavigableMap<K,V>unmodifiableNavigableMap(java.util.NavigableMap<K,V> map)Returns an unmodifiable view of the specified navigable map.
 
- 
- 
- 
Method Detail- 
immutableEnumMap@GwtCompatible(serializable=true) @Beta public static <K extends java.lang.Enum<K>,V> ImmutableMap<K,V> immutableEnumMap(java.util.Map<K,? extends V> map) Returns an immutable map instance containing the given entries. Internally, the returned map will be backed by anEnumMap.The iteration order of the returned map follows the enum's iteration order, not the order in which the elements appear in the given map. - Parameters:
- map- the map to make an immutable copy of
- Returns:
- an immutable map containing those entries
- Since:
- 14.0
 
 - 
newHashMappublic static <K,V> java.util.HashMap<K,V> newHashMap() Creates a mutable, emptyHashMapinstance.Note: if mutability is not required, use ImmutableMap.of()instead.Note: if Kis anenumtype, usenewEnumMap(java.lang.Class<K>)instead.- Returns:
- a new, empty HashMap
 
 - 
newHashMapWithExpectedSizepublic static <K,V> java.util.HashMap<K,V> newHashMapWithExpectedSize(int expectedSize) Creates aHashMapinstance, with a high enough "initial capacity" that it should holdexpectedSizeelements without growth. This behavior cannot be broadly guaranteed, but it is observed to be true for OpenJDK 1.6. It also can't be guaranteed that the method isn't inadvertently oversizing the returned map.- Parameters:
- expectedSize- the number of elements you expect to add to the returned map
- Returns:
- a new, empty HashMapwith enough capacity to holdexpectedSizeelements without resizing
- Throws:
- java.lang.IllegalArgumentException- if- expectedSizeis negative
 
 - 
newHashMappublic static <K,V> java.util.HashMap<K,V> newHashMap(java.util.Map<? extends K,? extends V> map) Creates a mutableHashMapinstance with the same mappings as the specified map.Note: if mutability is not required, use ImmutableMap.copyOf(Map)instead.Note: if Kis anEnumtype, usenewEnumMap(java.lang.Class<K>)instead.- Parameters:
- map- the mappings to be placed in the new map
- Returns:
- a new HashMapinitialized with the mappings frommap
 
 - 
newLinkedHashMappublic static <K,V> java.util.LinkedHashMap<K,V> newLinkedHashMap() Creates a mutable, empty, insertion-orderedLinkedHashMapinstance.Note: if mutability is not required, use ImmutableMap.of()instead.- Returns:
- a new, empty LinkedHashMap
 
 - 
newLinkedHashMappublic static <K,V> java.util.LinkedHashMap<K,V> newLinkedHashMap(java.util.Map<? extends K,? extends V> map) Creates a mutable, insertion-orderedLinkedHashMapinstance with the same mappings as the specified map.Note: if mutability is not required, use ImmutableMap.copyOf(Map)instead.- Parameters:
- map- the mappings to be placed in the new map
- Returns:
- a new, LinkedHashMapinitialized with the mappings frommap
 
 - 
newTreeMappublic static <K extends java.lang.Comparable,V> java.util.TreeMap<K,V> newTreeMap() Creates a mutable, emptyTreeMapinstance using the natural ordering of its elements.Note: if mutability is not required, use ImmutableSortedMap.of()instead.- Returns:
- a new, empty TreeMap
 
 - 
newTreeMappublic static <K,V> java.util.TreeMap<K,V> newTreeMap(java.util.SortedMap<K,? extends V> map) Creates a mutableTreeMapinstance with the same mappings as the specified map and using the same ordering as the specified map.Note: if mutability is not required, use ImmutableSortedMap.copyOfSorted(SortedMap)instead.- Parameters:
- map- the sorted map whose mappings are to be placed in the new map and whose comparator is to be used to sort the new map
- Returns:
- a new TreeMapinitialized with the mappings frommapand using the comparator ofmap
 
 - 
newTreeMappublic static <C,K extends C,V> java.util.TreeMap<K,V> newTreeMap(@Nullable java.util.Comparator<C> comparator) Creates a mutable, emptyTreeMapinstance using the given comparator.Note: if mutability is not required, use ImmutableSortedMap.orderedBy(comparator).build()instead.- Parameters:
- comparator- the comparator to sort the keys with
- Returns:
- a new, empty TreeMap
 
 - 
newEnumMappublic static <K extends java.lang.Enum<K>,V> java.util.EnumMap<K,V> newEnumMap(java.lang.Class<K> type) Creates anEnumMapinstance.- Parameters:
- type- the key type for this map
- Returns:
- a new, empty EnumMap
 
 - 
newEnumMappublic static <K extends java.lang.Enum<K>,V> java.util.EnumMap<K,V> newEnumMap(java.util.Map<K,? extends V> map) Creates anEnumMapwith the same mappings as the specified map.- Parameters:
- map- the map from which to initialize this- EnumMap
- Returns:
- a new EnumMapinitialized with the mappings frommap
- Throws:
- java.lang.IllegalArgumentException- if- mis not an- EnumMapinstance and contains no mappings
 
 - 
newIdentityHashMappublic static <K,V> java.util.IdentityHashMap<K,V> newIdentityHashMap() Creates anIdentityHashMapinstance.- Returns:
- a new, empty IdentityHashMap
 
 - 
differencepublic static <K,V> MapDifference<K,V> difference(java.util.Map<? extends K,? extends V> left, java.util.Map<? extends K,? extends V> right) Computes the difference between two maps. This difference is an immutable snapshot of the state of the maps at the time this method is called. It will never change, even if the maps change at a later time.Since this method uses HashMapinstances internally, the keys of the supplied maps must be well-behaved with respect toObject.equals(java.lang.Object)andObject.hashCode().Note:If you only need to know whether two maps have the same mappings, call left.equals(right)instead of this method.- Parameters:
- left- the map to treat as the "left" map for purposes of comparison
- right- the map to treat as the "right" map for purposes of comparison
- Returns:
- the difference between the two maps
 
 - 
difference@Beta public static <K,V> MapDifference<K,V> difference(java.util.Map<? extends K,? extends V> left, java.util.Map<? extends K,? extends V> right, Equivalence<? super V> valueEquivalence) Computes the difference between two maps. This difference is an immutable snapshot of the state of the maps at the time this method is called. It will never change, even if the maps change at a later time.Values are compared using a provided equivalence, in the case of equality, the value on the 'left' is returned in the difference. Since this method uses HashMapinstances internally, the keys of the supplied maps must be well-behaved with respect toObject.equals(java.lang.Object)andObject.hashCode().- Parameters:
- left- the map to treat as the "left" map for purposes of comparison
- right- the map to treat as the "right" map for purposes of comparison
- valueEquivalence- the equivalence relationship to use to compare values
- Returns:
- the difference between the two maps
- Since:
- 10.0
 
 - 
differencepublic static <K,V> SortedMapDifference<K,V> difference(java.util.SortedMap<K,? extends V> left, java.util.Map<? extends K,? extends V> right) Computes the difference between two sorted maps, using the comparator of the left map, orOrdering.natural()if the left map uses the natural ordering of its elements. This difference is an immutable snapshot of the state of the maps at the time this method is called. It will never change, even if the maps change at a later time.Since this method uses TreeMapinstances internally, the keys of the right map must all compare as distinct according to the comparator of the left map.Note:If you only need to know whether two sorted maps have the same mappings, call left.equals(right)instead of this method.- Parameters:
- left- the map to treat as the "left" map for purposes of comparison
- right- the map to treat as the "right" map for purposes of comparison
- Returns:
- the difference between the two maps
- Since:
- 11.0
 
 - 
asMap@Beta public static <K,V> java.util.Map<K,V> asMap(java.util.Set<K> set, Function<? super K,V> function) Returns a liveMapview whose keys are the contents ofsetand whose values are computed on demand usingfunction. To get an immutable copy instead, usetoMap(Iterable, Function).Specifically, for each kin the backing set, the returned map has an entry mappingktofunction.apply(k). ThekeySet,values, andentrySetviews of the returned map iterate in the same order as the backing set.Modifications to the backing set are read through to the returned map. The returned map supports removal operations if the backing set does. Removal operations write through to the backing set. The returned map does not support put operations. Warning: If the function rejects null, caution is required to make sure the set does not containnull, because the view cannot stopnullfrom being added to the set.Warning: This method assumes that for any instance kof key typeK,k.equals(k2)implies thatk2is also of typeK. Using a key type for which this may not hold, such asArrayList, may risk aClassCastExceptionwhen calling methods on the resulting map view.- Since:
- 14.0
 
 - 
asMap@Beta public static <K,V> java.util.SortedMap<K,V> asMap(java.util.SortedSet<K> set, Function<? super K,V> function) Returns a view of the sorted set as a map, mapping keys from the set according to the specified function.Specifically, for each kin the backing set, the returned map has an entry mappingktofunction.apply(k). ThekeySet,values, andentrySetviews of the returned map iterate in the same order as the backing set.Modifications to the backing set are read through to the returned map. The returned map supports removal operations if the backing set does. Removal operations write through to the backing set. The returned map does not support put operations. Warning: If the function rejects null, caution is required to make sure the set does not containnull, because the view cannot stopnullfrom being added to the set.Warning: This method assumes that for any instance kof key typeK,k.equals(k2)implies thatk2is also of typeK. Using a key type for which this may not hold, such asArrayList, may risk aClassCastExceptionwhen calling methods on the resulting map view.- Since:
- 14.0
 
 - 
asMap@Beta @GwtIncompatible("NavigableMap") public static <K,V> java.util.NavigableMap<K,V> asMap(java.util.NavigableSet<K> set, Function<? super K,V> function) Returns a view of the navigable set as a map, mapping keys from the set according to the specified function.Specifically, for each kin the backing set, the returned map has an entry mappingktofunction.apply(k). ThekeySet,values, andentrySetviews of the returned map iterate in the same order as the backing set.Modifications to the backing set are read through to the returned map. The returned map supports removal operations if the backing set does. Removal operations write through to the backing set. The returned map does not support put operations. Warning: If the function rejects null, caution is required to make sure the set does not containnull, because the view cannot stopnullfrom being added to the set.Warning: This method assumes that for any instance kof key typeK,k.equals(k2)implies thatk2is also of typeK. Using a key type for which this may not hold, such asArrayList, may risk aClassCastExceptionwhen calling methods on the resulting map view.- Since:
- 14.0
 
 - 
toMap@Beta public static <K,V> ImmutableMap<K,V> toMap(java.lang.Iterable<K> keys, Function<? super K,V> valueFunction) Returns an immutable map whose keys are the distinct elements ofkeysand whose value for each key was computed byvalueFunction. The map's iteration order is the order of the first appearance of each key inkeys.If keysis aSet, a live view can be obtained instead of a copy usingasMap(Set, Function).- Throws:
- java.lang.NullPointerException- if any element of- keysis- null, or if- valueFunctionproduces- nullfor any key
- Since:
- 14.0
 
 - 
toMap@Beta public static <K,V> ImmutableMap<K,V> toMap(java.util.Iterator<K> keys, Function<? super K,V> valueFunction) Returns an immutable map whose keys are the distinct elements ofkeysand whose value for each key was computed byvalueFunction. The map's iteration order is the order of the first appearance of each key inkeys.- Throws:
- java.lang.NullPointerException- if any element of- keysis- null, or if- valueFunctionproduces- nullfor any key
- Since:
- 14.0
 
 - 
uniqueIndexpublic static <K,V> ImmutableMap<K,V> uniqueIndex(java.lang.Iterable<V> values, Function<? super V,K> keyFunction) Returns an immutable map for which theMap.values()are the given elements in the given order, and each key is the product of invoking a supplied function on its corresponding value.- Parameters:
- values- the values to use when constructing the- Map
- keyFunction- the function used to produce the key for each value
- Returns:
- a map mapping the result of evaluating the function keyFunctionon each value in the input collection to that value
- Throws:
- java.lang.IllegalArgumentException- if- keyFunctionproduces the same key for more than one value in the input collection
- java.lang.NullPointerException- if any elements of- valuesis null, or if- keyFunctionproduces- nullfor any value
 
 - 
uniqueIndexpublic static <K,V> ImmutableMap<K,V> uniqueIndex(java.util.Iterator<V> values, Function<? super V,K> keyFunction) Returns an immutable map for which theMap.values()are the given elements in the given order, and each key is the product of invoking a supplied function on its corresponding value.- Parameters:
- values- the values to use when constructing the- Map
- keyFunction- the function used to produce the key for each value
- Returns:
- a map mapping the result of evaluating the function keyFunctionon each value in the input collection to that value
- Throws:
- java.lang.IllegalArgumentException- if- keyFunctionproduces the same key for more than one value in the input collection
- java.lang.NullPointerException- if any elements of- valuesis null, or if- keyFunctionproduces- nullfor any value
- Since:
- 10.0
 
 - 
fromProperties@GwtIncompatible("java.util.Properties") public static ImmutableMap<java.lang.String,java.lang.String> fromProperties(java.util.Properties properties) Creates anImmutableMap<String, String>from aPropertiesinstance. Properties normally derive fromMap<Object, Object>, but they typically contain strings, which is awkward. This method lets you get a plain-old-Mapout of aProperties.- Parameters:
- properties- a- Propertiesobject to be converted
- Returns:
- an immutable map containing all the entries in properties
- Throws:
- java.lang.ClassCastException- if any key in- Propertiesis not a- String
- java.lang.NullPointerException- if any key or value in- Propertiesis null
 
 - 
immutableEntry@GwtCompatible(serializable=true) public static <K,V> java.util.Map.Entry<K,V> immutableEntry(@Nullable K key, @Nullable V value) Returns an immutable map entry with the specified key and value. TheMap.Entry.setValue(V)operation throws anUnsupportedOperationException.The returned entry is serializable. - Parameters:
- key- the key to be associated with the returned entry
- value- the value to be associated with the returned entry
 
 - 
asConverter@Beta public static <A,B> Converter<A,B> asConverter(BiMap<A,B> bimap) Returns aConverterthat converts values usingbimap.get(), and whose inverse view converts values usingbimap.inverse().get().To use a plain Mapas aFunction, seeFunctions.forMap(Map)orFunctions.forMap(Map, Object).- Since:
- 16.0
 
 - 
synchronizedBiMappublic static <K,V> BiMap<K,V> synchronizedBiMap(BiMap<K,V> bimap) Returns a synchronized (thread-safe) bimap backed by the specified bimap. In order to guarantee serial access, it is critical that all access to the backing bimap is accomplished through the returned bimap.It is imperative that the user manually synchronize on the returned map when accessing any of its collection views: BiMap<Long, String> map = Maps.synchronizedBiMap( HashBiMap.<Long, String>create()); ... Set<Long> set = map.keySet(); // Needn't be in synchronized block ... synchronized (map) { // Synchronizing on map, not set! Iterator<Long> it = set.iterator(); // Must be in synchronized block while (it.hasNext()) { foo(it.next()); } }Failure to follow this advice may result in non-deterministic behavior. The returned bimap will be serializable if the specified bimap is serializable. - Parameters:
- bimap- the bimap to be wrapped in a synchronized view
- Returns:
- a sychronized view of the specified bimap
 
 - 
unmodifiableBiMappublic static <K,V> BiMap<K,V> unmodifiableBiMap(BiMap<? extends K,? extends V> bimap) Returns an unmodifiable view of the specified bimap. This method allows modules to provide users with "read-only" access to internal bimaps. Query operations on the returned bimap "read through" to the specified bimap, and attempts to modify the returned map, whether direct or via its collection views, result in anUnsupportedOperationException.The returned bimap will be serializable if the specified bimap is serializable. - Parameters:
- bimap- the bimap for which an unmodifiable view is to be returned
- Returns:
- an unmodifiable view of the specified bimap
 
 - 
transformValuespublic static <K,V1,V2> java.util.Map<K,V2> transformValues(java.util.Map<K,V1> fromMap, Function<? super V1,V2> function)Returns a view of a map where each value is transformed by a function. All other properties of the map, such as iteration order, are left intact. For example, the code:{ @code Map... printsmap = ImmutableMap.of("a", 4, "b", 9); Function sqrt = new Function () { public Double apply(Integer in) { return Math.sqrt((int) in); } }; Map transformed = Maps.transformValues(map, sqrt); System.out.println(transformed); } {a=2.0, b=3.0}.Changes in the underlying map are reflected in this view. Conversely, this view supports removal operations, and these are reflected in the underlying map. It's acceptable for the underlying map to contain null keys, and even null values provided that the function is capable of accepting null input. The transformed map might contain null values, if the function sometimes gives a null result. The returned map is not thread-safe or serializable, even if the underlying map is. The function is applied lazily, invoked when needed. This is necessary for the returned map to be a view, but it means that the function will be applied many times for bulk operations like Map.containsValue(java.lang.Object)andMap.toString(). For this to perform well,functionshould be fast. To avoid lazy evaluation when the returned map doesn't need to be a view, copy the returned map into a new map of your choosing.
 - 
transformValuespublic static <K,V1,V2> java.util.SortedMap<K,V2> transformValues(java.util.SortedMap<K,V1> fromMap, Function<? super V1,V2> function)Returns a view of a sorted map where each value is transformed by a function. All other properties of the map, such as iteration order, are left intact. For example, the code:{ @code SortedMap... printsmap = ImmutableSortedMap.of("a", 4, "b", 9); Function sqrt = new Function () { public Double apply(Integer in) { return Math.sqrt((int) in); } }; SortedMap transformed = Maps.transformValues(map, sqrt); System.out.println(transformed); } {a=2.0, b=3.0}.Changes in the underlying map are reflected in this view. Conversely, this view supports removal operations, and these are reflected in the underlying map. It's acceptable for the underlying map to contain null keys, and even null values provided that the function is capable of accepting null input. The transformed map might contain null values, if the function sometimes gives a null result. The returned map is not thread-safe or serializable, even if the underlying map is. The function is applied lazily, invoked when needed. This is necessary for the returned map to be a view, but it means that the function will be applied many times for bulk operations like Map.containsValue(java.lang.Object)andMap.toString(). For this to perform well,functionshould be fast. To avoid lazy evaluation when the returned map doesn't need to be a view, copy the returned map into a new map of your choosing.- Since:
- 11.0
 
 - 
transformValues@GwtIncompatible("NavigableMap") public static <K,V1,V2> java.util.NavigableMap<K,V2> transformValues(java.util.NavigableMap<K,V1> fromMap, Function<? super V1,V2> function) Returns a view of a navigable map where each value is transformed by a function. All other properties of the map, such as iteration order, are left intact. For example, the code:{ @code NavigableMap... printsmap = Maps.newTreeMap(); map.put("a", 4); map.put("b", 9); Function sqrt = new Function () { public Double apply(Integer in) { return Math.sqrt((int) in); } }; NavigableMap transformed = Maps.transformNavigableValues(map, sqrt); System.out.println(transformed); } {a=2.0, b=3.0}. Changes in the underlying map are reflected in this view. Conversely, this view supports removal operations, and these are reflected in the underlying map.It's acceptable for the underlying map to contain null keys, and even null values provided that the function is capable of accepting null input. The transformed map might contain null values, if the function sometimes gives a null result. The returned map is not thread-safe or serializable, even if the underlying map is. The function is applied lazily, invoked when needed. This is necessary for the returned map to be a view, but it means that the function will be applied many times for bulk operations like Map.containsValue(java.lang.Object)andMap.toString(). For this to perform well,functionshould be fast. To avoid lazy evaluation when the returned map doesn't need to be a view, copy the returned map into a new map of your choosing.- Since:
- 13.0
 
 - 
transformEntriespublic static <K,V1,V2> java.util.Map<K,V2> transformEntries(java.util.Map<K,V1> fromMap, Maps.EntryTransformer<? super K,? super V1,V2> transformer)Returns a view of a map whose values are derived from the original map's entries. In contrast totransformValues(java.util.Map<K, V1>, com.google.common.base.Function<? super V1, V2>), this method's entry-transformation logic may depend on the key as well as the value.All other properties of the transformed map, such as iteration order, are left intact. For example, the code: { @code Map... printsoptions = ImmutableMap.of("verbose", true, "sort", false); EntryTransformer flagPrefixer = new EntryTransformer () { public String transformEntry(String key, Boolean value) { return value ? key : "no" + key; } }; Map transformed = Maps.transformEntries(options, flagPrefixer); System.out.println(transformed); } {verbose=verbose, sort=nosort}.Changes in the underlying map are reflected in this view. Conversely, this view supports removal operations, and these are reflected in the underlying map. It's acceptable for the underlying map to contain null keys and null values provided that the transformer is capable of accepting null inputs. The transformed map might contain null values if the transformer sometimes gives a null result. The returned map is not thread-safe or serializable, even if the underlying map is. The transformer is applied lazily, invoked when needed. This is necessary for the returned map to be a view, but it means that the transformer will be applied many times for bulk operations like Map.containsValue(java.lang.Object)andObject.toString(). For this to perform well,transformershould be fast. To avoid lazy evaluation when the returned map doesn't need to be a view, copy the returned map into a new map of your choosing.Warning: This method assumes that for any instance kofEntryTransformerkey typeK,k.equals(k2)implies thatk2is also of typeK. Using anEntryTransformerkey type for which this may not hold, such asArrayList, may risk aClassCastExceptionwhen calling methods on the transformed map.- Since:
- 7.0
 
 - 
transformEntriespublic static <K,V1,V2> java.util.SortedMap<K,V2> transformEntries(java.util.SortedMap<K,V1> fromMap, Maps.EntryTransformer<? super K,? super V1,V2> transformer)Returns a view of a sorted map whose values are derived from the original sorted map's entries. In contrast totransformValues(java.util.Map<K, V1>, com.google.common.base.Function<? super V1, V2>), this method's entry-transformation logic may depend on the key as well as the value.All other properties of the transformed map, such as iteration order, are left intact. For example, the code: { @code Map... printsoptions = ImmutableSortedMap.of("verbose", true, "sort", false); EntryTransformer flagPrefixer = new EntryTransformer () { public String transformEntry(String key, Boolean value) { return value ? key : "yes" + key; } }; SortedMap transformed = Maps.transformEntries(options, flagPrefixer); System.out.println(transformed); } {sort=yessort, verbose=verbose}.Changes in the underlying map are reflected in this view. Conversely, this view supports removal operations, and these are reflected in the underlying map. It's acceptable for the underlying map to contain null keys and null values provided that the transformer is capable of accepting null inputs. The transformed map might contain null values if the transformer sometimes gives a null result. The returned map is not thread-safe or serializable, even if the underlying map is. The transformer is applied lazily, invoked when needed. This is necessary for the returned map to be a view, but it means that the transformer will be applied many times for bulk operations like Map.containsValue(java.lang.Object)andObject.toString(). For this to perform well,transformershould be fast. To avoid lazy evaluation when the returned map doesn't need to be a view, copy the returned map into a new map of your choosing.Warning: This method assumes that for any instance kofEntryTransformerkey typeK,k.equals(k2)implies thatk2is also of typeK. Using anEntryTransformerkey type for which this may not hold, such asArrayList, may risk aClassCastExceptionwhen calling methods on the transformed map.- Since:
- 11.0
 
 - 
transformEntries@GwtIncompatible("NavigableMap") public static <K,V1,V2> java.util.NavigableMap<K,V2> transformEntries(java.util.NavigableMap<K,V1> fromMap, Maps.EntryTransformer<? super K,? super V1,V2> transformer) Returns a view of a navigable map whose values are derived from the original navigable map's entries. In contrast totransformValues(java.util.Map<K, V1>, com.google.common.base.Function<? super V1, V2>), this method's entry-transformation logic may depend on the key as well as the value.All other properties of the transformed map, such as iteration order, are left intact. For example, the code: { @code NavigableMap... printsoptions = Maps.newTreeMap(); options.put("verbose", false); options.put("sort", true); EntryTransformer flagPrefixer = new EntryTransformer () { public String transformEntry(String key, Boolean value) { return value ? key : ("yes" + key); } }; NavigableMap transformed = LabsMaps.transformNavigableEntries(options, flagPrefixer); System.out.println(transformed); } {sort=yessort, verbose=verbose}.Changes in the underlying map are reflected in this view. Conversely, this view supports removal operations, and these are reflected in the underlying map. It's acceptable for the underlying map to contain null keys and null values provided that the transformer is capable of accepting null inputs. The transformed map might contain null values if the transformer sometimes gives a null result. The returned map is not thread-safe or serializable, even if the underlying map is. The transformer is applied lazily, invoked when needed. This is necessary for the returned map to be a view, but it means that the transformer will be applied many times for bulk operations like Map.containsValue(java.lang.Object)andObject.toString(). For this to perform well,transformershould be fast. To avoid lazy evaluation when the returned map doesn't need to be a view, copy the returned map into a new map of your choosing.Warning: This method assumes that for any instance kofEntryTransformerkey typeK,k.equals(k2)implies thatk2is also of typeK. Using anEntryTransformerkey type for which this may not hold, such asArrayList, may risk aClassCastExceptionwhen calling methods on the transformed map.- Since:
- 13.0
 
 - 
filterKeyspublic static <K,V> java.util.Map<K,V> filterKeys(java.util.Map<K,V> unfiltered, Predicate<? super K> keyPredicate)Returns a map containing the mappings inunfilteredwhose keys satisfy a predicate. The returned map is a live view ofunfiltered; changes to one affect the other.The resulting map's keySet(),entrySet(), andvalues()views have iterators that don't supportremove(), but all other methods are supported by the map and its views. When given a key that doesn't satisfy the predicate, the map'sput()andputAll()methods throw anIllegalArgumentException.When methods such as removeAll()andclear()are called on the filtered map or its views, only mappings whose keys satisfy the filter will be removed from the underlying map.The returned map isn't threadsafe or serializable, even if unfilteredis.Many of the filtered map's methods, such as size(), iterate across every key/value mapping in the underlying map and determine which satisfy the filter. When a live view is not needed, it may be faster to copy the filtered map and use the copy.Warning: keyPredicatemust be consistent with equals, as documented atPredicate.apply(T). Do not provide a predicate such asPredicates.instanceOf(ArrayList.class), which is inconsistent with equals.
 - 
filterKeyspublic static <K,V> java.util.SortedMap<K,V> filterKeys(java.util.SortedMap<K,V> unfiltered, Predicate<? super K> keyPredicate)Returns a sorted map containing the mappings inunfilteredwhose keys satisfy a predicate. The returned map is a live view ofunfiltered; changes to one affect the other.The resulting map's keySet(),entrySet(), andvalues()views have iterators that don't supportremove(), but all other methods are supported by the map and its views. When given a key that doesn't satisfy the predicate, the map'sput()andputAll()methods throw anIllegalArgumentException.When methods such as removeAll()andclear()are called on the filtered map or its views, only mappings whose keys satisfy the filter will be removed from the underlying map.The returned map isn't threadsafe or serializable, even if unfilteredis.Many of the filtered map's methods, such as size(), iterate across every key/value mapping in the underlying map and determine which satisfy the filter. When a live view is not needed, it may be faster to copy the filtered map and use the copy.Warning: keyPredicatemust be consistent with equals, as documented atPredicate.apply(T). Do not provide a predicate such asPredicates.instanceOf(ArrayList.class), which is inconsistent with equals.- Since:
- 11.0
 
 - 
filterKeys@GwtIncompatible("NavigableMap") public static <K,V> java.util.NavigableMap<K,V> filterKeys(java.util.NavigableMap<K,V> unfiltered, Predicate<? super K> keyPredicate) Returns a navigable map containing the mappings inunfilteredwhose keys satisfy a predicate. The returned map is a live view ofunfiltered; changes to one affect the other.The resulting map's keySet(),entrySet(), andvalues()views have iterators that don't supportremove(), but all other methods are supported by the map and its views. When given a key that doesn't satisfy the predicate, the map'sput()andputAll()methods throw anIllegalArgumentException.When methods such as removeAll()andclear()are called on the filtered map or its views, only mappings whose keys satisfy the filter will be removed from the underlying map.The returned map isn't threadsafe or serializable, even if unfilteredis.Many of the filtered map's methods, such as size(), iterate across every key/value mapping in the underlying map and determine which satisfy the filter. When a live view is not needed, it may be faster to copy the filtered map and use the copy.Warning: keyPredicatemust be consistent with equals, as documented atPredicate.apply(T). Do not provide a predicate such asPredicates.instanceOf(ArrayList.class), which is inconsistent with equals.- Since:
- 14.0
 
 - 
filterKeyspublic static <K,V> BiMap<K,V> filterKeys(BiMap<K,V> unfiltered, Predicate<? super K> keyPredicate) Returns a bimap containing the mappings inunfilteredwhose keys satisfy a predicate. The returned bimap is a live view ofunfiltered; changes to one affect the other.The resulting bimap's keySet(),entrySet(), andvalues()views have iterators that don't supportremove(), but all other methods are supported by the bimap and its views. When given a key that doesn't satisfy the predicate, the bimap'sput(),forcePut()andputAll()methods throw anIllegalArgumentException.When methods such as removeAll()andclear()are called on the filtered bimap or its views, only mappings that satisfy the filter will be removed from the underlying bimap.The returned bimap isn't threadsafe or serializable, even if unfilteredis.Many of the filtered bimap's methods, such as size(), iterate across every key in the underlying bimap and determine which satisfy the filter. When a live view is not needed, it may be faster to copy the filtered bimap and use the copy.Warning: entryPredicatemust be consistent with equals , as documented atPredicate.apply(T).- Since:
- 14.0
 
 - 
filterValuespublic static <K,V> java.util.Map<K,V> filterValues(java.util.Map<K,V> unfiltered, Predicate<? super V> valuePredicate)Returns a map containing the mappings inunfilteredwhose values satisfy a predicate. The returned map is a live view ofunfiltered; changes to one affect the other.The resulting map's keySet(),entrySet(), andvalues()views have iterators that don't supportremove(), but all other methods are supported by the map and its views. When given a value that doesn't satisfy the predicate, the map'sput(),putAll(), andMap.Entry.setValue(V)methods throw anIllegalArgumentException.When methods such as removeAll()andclear()are called on the filtered map or its views, only mappings whose values satisfy the filter will be removed from the underlying map.The returned map isn't threadsafe or serializable, even if unfilteredis.Many of the filtered map's methods, such as size(), iterate across every key/value mapping in the underlying map and determine which satisfy the filter. When a live view is not needed, it may be faster to copy the filtered map and use the copy.Warning: valuePredicatemust be consistent with equals, as documented atPredicate.apply(T). Do not provide a predicate such asPredicates.instanceOf(ArrayList.class), which is inconsistent with equals.
 - 
filterValuespublic static <K,V> java.util.SortedMap<K,V> filterValues(java.util.SortedMap<K,V> unfiltered, Predicate<? super V> valuePredicate)Returns a sorted map containing the mappings inunfilteredwhose values satisfy a predicate. The returned map is a live view ofunfiltered; changes to one affect the other.The resulting map's keySet(),entrySet(), andvalues()views have iterators that don't supportremove(), but all other methods are supported by the map and its views. When given a value that doesn't satisfy the predicate, the map'sput(),putAll(), andMap.Entry.setValue(V)methods throw anIllegalArgumentException.When methods such as removeAll()andclear()are called on the filtered map or its views, only mappings whose values satisfy the filter will be removed from the underlying map.The returned map isn't threadsafe or serializable, even if unfilteredis.Many of the filtered map's methods, such as size(), iterate across every key/value mapping in the underlying map and determine which satisfy the filter. When a live view is not needed, it may be faster to copy the filtered map and use the copy.Warning: valuePredicatemust be consistent with equals, as documented atPredicate.apply(T). Do not provide a predicate such asPredicates.instanceOf(ArrayList.class), which is inconsistent with equals.- Since:
- 11.0
 
 - 
filterValues@GwtIncompatible("NavigableMap") public static <K,V> java.util.NavigableMap<K,V> filterValues(java.util.NavigableMap<K,V> unfiltered, Predicate<? super V> valuePredicate) Returns a navigable map containing the mappings inunfilteredwhose values satisfy a predicate. The returned map is a live view ofunfiltered; changes to one affect the other.The resulting map's keySet(),entrySet(), andvalues()views have iterators that don't supportremove(), but all other methods are supported by the map and its views. When given a value that doesn't satisfy the predicate, the map'sput(),putAll(), andMap.Entry.setValue(V)methods throw anIllegalArgumentException.When methods such as removeAll()andclear()are called on the filtered map or its views, only mappings whose values satisfy the filter will be removed from the underlying map.The returned map isn't threadsafe or serializable, even if unfilteredis.Many of the filtered map's methods, such as size(), iterate across every key/value mapping in the underlying map and determine which satisfy the filter. When a live view is not needed, it may be faster to copy the filtered map and use the copy.Warning: valuePredicatemust be consistent with equals, as documented atPredicate.apply(T). Do not provide a predicate such asPredicates.instanceOf(ArrayList.class), which is inconsistent with equals.- Since:
- 14.0
 
 - 
filterValuespublic static <K,V> BiMap<K,V> filterValues(BiMap<K,V> unfiltered, Predicate<? super V> valuePredicate) Returns a bimap containing the mappings inunfilteredwhose values satisfy a predicate. The returned bimap is a live view ofunfiltered; changes to one affect the other.The resulting bimap's keySet(),entrySet(), andvalues()views have iterators that don't supportremove(), but all other methods are supported by the bimap and its views. When given a value that doesn't satisfy the predicate, the bimap'sput(),forcePut()andputAll()methods throw anIllegalArgumentException. Similarly, the map's entries have aMap.Entry.setValue(V)method that throws anIllegalArgumentExceptionwhen the provided value doesn't satisfy the predicate.When methods such as removeAll()andclear()are called on the filtered bimap or its views, only mappings that satisfy the filter will be removed from the underlying bimap.The returned bimap isn't threadsafe or serializable, even if unfilteredis.Many of the filtered bimap's methods, such as size(), iterate across every value in the underlying bimap and determine which satisfy the filter. When a live view is not needed, it may be faster to copy the filtered bimap and use the copy.Warning: entryPredicatemust be consistent with equals , as documented atPredicate.apply(T).- Since:
- 14.0
 
 - 
filterEntriespublic static <K,V> java.util.Map<K,V> filterEntries(java.util.Map<K,V> unfiltered, Predicate<? super java.util.Map.Entry<K,V>> entryPredicate)Returns a map containing the mappings inunfilteredthat satisfy a predicate. The returned map is a live view ofunfiltered; changes to one affect the other.The resulting map's keySet(),entrySet(), andvalues()views have iterators that don't supportremove(), but all other methods are supported by the map and its views. When given a key/value pair that doesn't satisfy the predicate, the map'sput()andputAll()methods throw anIllegalArgumentException. Similarly, the map's entries have aMap.Entry.setValue(V)method that throws anIllegalArgumentExceptionwhen the existing key and the provided value don't satisfy the predicate.When methods such as removeAll()andclear()are called on the filtered map or its views, only mappings that satisfy the filter will be removed from the underlying map.The returned map isn't threadsafe or serializable, even if unfilteredis.Many of the filtered map's methods, such as size(), iterate across every key/value mapping in the underlying map and determine which satisfy the filter. When a live view is not needed, it may be faster to copy the filtered map and use the copy.Warning: entryPredicatemust be consistent with equals, as documented atPredicate.apply(T).
 - 
filterEntriespublic static <K,V> java.util.SortedMap<K,V> filterEntries(java.util.SortedMap<K,V> unfiltered, Predicate<? super java.util.Map.Entry<K,V>> entryPredicate)Returns a sorted map containing the mappings inunfilteredthat satisfy a predicate. The returned map is a live view ofunfiltered; changes to one affect the other.The resulting map's keySet(),entrySet(), andvalues()views have iterators that don't supportremove(), but all other methods are supported by the map and its views. When given a key/value pair that doesn't satisfy the predicate, the map'sput()andputAll()methods throw anIllegalArgumentException. Similarly, the map's entries have aMap.Entry.setValue(V)method that throws anIllegalArgumentExceptionwhen the existing key and the provided value don't satisfy the predicate.When methods such as removeAll()andclear()are called on the filtered map or its views, only mappings that satisfy the filter will be removed from the underlying map.The returned map isn't threadsafe or serializable, even if unfilteredis.Many of the filtered map's methods, such as size(), iterate across every key/value mapping in the underlying map and determine which satisfy the filter. When a live view is not needed, it may be faster to copy the filtered map and use the copy.Warning: entryPredicatemust be consistent with equals, as documented atPredicate.apply(T).- Since:
- 11.0
 
 - 
filterEntries@GwtIncompatible("NavigableMap") public static <K,V> java.util.NavigableMap<K,V> filterEntries(java.util.NavigableMap<K,V> unfiltered, Predicate<? super java.util.Map.Entry<K,V>> entryPredicate) Returns a sorted map containing the mappings inunfilteredthat satisfy a predicate. The returned map is a live view ofunfiltered; changes to one affect the other.The resulting map's keySet(),entrySet(), andvalues()views have iterators that don't supportremove(), but all other methods are supported by the map and its views. When given a key/value pair that doesn't satisfy the predicate, the map'sput()andputAll()methods throw anIllegalArgumentException. Similarly, the map's entries have aMap.Entry.setValue(V)method that throws anIllegalArgumentExceptionwhen the existing key and the provided value don't satisfy the predicate.When methods such as removeAll()andclear()are called on the filtered map or its views, only mappings that satisfy the filter will be removed from the underlying map.The returned map isn't threadsafe or serializable, even if unfilteredis.Many of the filtered map's methods, such as size(), iterate across every key/value mapping in the underlying map and determine which satisfy the filter. When a live view is not needed, it may be faster to copy the filtered map and use the copy.Warning: entryPredicatemust be consistent with equals, as documented atPredicate.apply(T).- Since:
- 14.0
 
 - 
filterEntriespublic static <K,V> BiMap<K,V> filterEntries(BiMap<K,V> unfiltered, Predicate<? super java.util.Map.Entry<K,V>> entryPredicate) Returns a bimap containing the mappings inunfilteredthat satisfy a predicate. The returned bimap is a live view ofunfiltered; changes to one affect the other.The resulting bimap's keySet(),entrySet(), andvalues()views have iterators that don't supportremove(), but all other methods are supported by the bimap and its views. When given a key/value pair that doesn't satisfy the predicate, the bimap'sput(),forcePut()andputAll()methods throw anIllegalArgumentException. Similarly, the map's entries have anMap.Entry.setValue(V)method that throws anIllegalArgumentExceptionwhen the existing key and the provided value don't satisfy the predicate.When methods such as removeAll()andclear()are called on the filtered bimap or its views, only mappings that satisfy the filter will be removed from the underlying bimap.The returned bimap isn't threadsafe or serializable, even if unfilteredis.Many of the filtered bimap's methods, such as size(), iterate across every key/value mapping in the underlying bimap and determine which satisfy the filter. When a live view is not needed, it may be faster to copy the filtered bimap and use the copy.Warning: entryPredicatemust be consistent with equals , as documented atPredicate.apply(T).- Since:
- 14.0
 
 - 
unmodifiableNavigableMap@GwtIncompatible("NavigableMap") public static <K,V> java.util.NavigableMap<K,V> unmodifiableNavigableMap(java.util.NavigableMap<K,V> map) Returns an unmodifiable view of the specified navigable map. Query operations on the returned map read through to the specified map, and attempts to modify the returned map, whether direct or via its views, result in anUnsupportedOperationException.The returned navigable map will be serializable if the specified navigable map is serializable. - Parameters:
- map- the navigable map for which an unmodifiable view is to be returned
- Returns:
- an unmodifiable view of the specified navigable map
- Since:
- 12.0
 
 - 
synchronizedNavigableMap@GwtIncompatible("NavigableMap") public static <K,V> java.util.NavigableMap<K,V> synchronizedNavigableMap(java.util.NavigableMap<K,V> navigableMap) Returns a synchronized (thread-safe) navigable map backed by the specified navigable map. In order to guarantee serial access, it is critical that all access to the backing navigable map is accomplished through the returned navigable map (or its views).It is imperative that the user manually synchronize on the returned navigable map when iterating over any of its collection views, or the collections views of any of its descendingMap,subMap,headMaportailMapviews.{ @code NavigableMapmap = synchronizedNavigableMap(new TreeMap ()); // Needn't be in synchronized block NavigableSet set = map.navigableKeySet(); synchronized (map) { // Synchronizing on map, not set! Iterator it = set.iterator(); // Must be in synchronized block while (it.hasNext()) { foo(it.next()); } } } or: { @code NavigableMapmap = synchronizedNavigableMap(new TreeMap ()); NavigableMap map2 = map.subMap(foo, false, bar, true); // Needn't be in synchronized block NavigableSet set2 = map2.descendingKeySet(); synchronized (map) { // Synchronizing on map, not map2 or set2! Iterator it = set2.iterator(); // Must be in synchronized block while (it.hasNext()) { foo(it.next()); } } } Failure to follow this advice may result in non-deterministic behavior. The returned navigable map will be serializable if the specified navigable map is serializable. - Parameters:
- navigableMap- the navigable map to be "wrapped" in a synchronized navigable map.
- Returns:
- a synchronized view of the specified navigable map.
- Since:
- 13.0
 
 
- 
 
-