Modifier and Type | Class and Description |
---|---|
private static class |
Maps.AbstractFilteredMap<K,V> |
private static class |
Maps.Accumulator<K extends java.lang.Enum<K>,V> |
private static class |
Maps.AsMapView<K,V> |
private static class |
Maps.BiMapConverter<A,B> |
(package private) static class |
Maps.DescendingMap<K,V> |
private static class |
Maps.EntryFunction |
(package private) static class |
Maps.EntrySet<K,V> |
static interface |
Maps.EntryTransformer<K,V1,V2>
A transformation of the value of a key-value pair, using both key and value as inputs.
|
(package private) static class |
Maps.FilteredEntryBiMap<K,V> |
(package private) static class |
Maps.FilteredEntryMap<K,V> |
private static class |
Maps.FilteredEntryNavigableMap<K,V> |
private static class |
Maps.FilteredEntrySortedMap<K,V> |
private static class |
Maps.FilteredKeyMap<K,V> |
private static class |
Maps.FilteredMapValues<K,V> |
(package private) static class |
Maps.IteratorBasedAbstractMap<K,V> |
(package private) static class |
Maps.KeySet<K,V> |
(package private) static class |
Maps.MapDifferenceImpl<K,V> |
private static class |
Maps.NavigableAsMapView<K,V> |
(package private) static class |
Maps.NavigableKeySet<K,V> |
private static class |
Maps.SortedAsMapView<K,V> |
(package private) static class |
Maps.SortedKeySet<K,V> |
(package private) static class |
Maps.SortedMapDifferenceImpl<K,V> |
(package private) static class |
Maps.TransformedEntriesMap<K,V1,V2> |
private static class |
Maps.TransformedEntriesNavigableMap<K,V1,V2> |
(package private) static class |
Maps.TransformedEntriesSortedMap<K,V1,V2> |
private static class |
Maps.UnmodifiableBiMap<K,V> |
(package private) static class |
Maps.UnmodifiableEntries<K,V> |
(package private) static class |
Maps.UnmodifiableEntrySet<K,V> |
(package private) static class |
Maps.UnmodifiableNavigableMap<K,V> |
(package private) static class |
Maps.ValueDifferenceImpl<V> |
(package private) static class |
Maps.Values<K,V> |
(package private) static class |
Maps.ViewCachingAbstractMap<K,V>
AbstractMap extension that makes it easy to cache customized keySet, values, and
entrySet views. |
Modifier | Constructor and Description |
---|---|
private |
Maps() |
Modifier and Type | Method and Description |
---|---|
static <A,B> Converter<A,B> |
asConverter(BiMap<A,B> bimap)
Returns a
Converter that converts values using bimap.get() , and whose
inverse view converts values using bimap.inverse() .get() . |
(package private) static <K,V1,V2> Function<java.util.Map.Entry<K,V1>,java.util.Map.Entry<K,V2>> |
asEntryToEntryFunction(Maps.EntryTransformer<? super K,? super V1,V2> transformer)
Views an entry transformer as a function from entries to entries.
|
(package private) static <K,V1,V2> Function<java.util.Map.Entry<K,V1>,V2> |
asEntryToValueFunction(Maps.EntryTransformer<? super K,? super V1,V2> transformer)
Views an entry transformer as a function from
Entry to values. |
(package private) static <K,V1,V2> Maps.EntryTransformer<K,V1,V2> |
asEntryTransformer(Function<? super V1,V2> function)
Views a function as an entry transformer that ignores the entry key.
|
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 live
Map view whose keys are the contents of set and whose values are
computed on demand using function . |
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.
|
(package private) static <K,V> java.util.Iterator<java.util.Map.Entry<K,V>> |
asMapEntryIterator(java.util.Set<K> set,
Function<? super K,V> function) |
(package private) static <K,V1,V2> Function<V1,V2> |
asValueToValueFunction(Maps.EntryTransformer<? super K,V1,V2> transformer,
K key) |
(package private) static int |
capacity(int expectedSize)
Returns a capacity that is sufficient to keep the map from being resized as long as it grows no
larger than expectedSize and the load factor is ≥ its default (0.75).
|
(package private) static <K,V> boolean |
containsEntryImpl(java.util.Collection<java.util.Map.Entry<K,V>> c,
java.lang.Object o)
Implements
Collection.contains safely for forwarding collections of map entries. |
(package private) static boolean |
containsKeyImpl(java.util.Map<?,?> map,
java.lang.Object key)
An admittedly inefficient implementation of
Map.containsKey(java.lang.Object) . |
(package private) static boolean |
containsValueImpl(java.util.Map<?,?> map,
java.lang.Object value)
An implementation of
Map.containsValue(java.lang.Object) . |
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, or
Ordering.natural() if the left map uses the natural ordering of its elements. |
private static <K,V> void |
doDifference(java.util.Map<? extends K,? extends V> left,
java.util.Map<? extends K,? extends V> right,
Equivalence<? super V> valueEquivalence,
java.util.Map<K,V> onlyOnLeft,
java.util.Map<K,V> onlyOnRight,
java.util.Map<K,V> onBoth,
java.util.Map<K,MapDifference.ValueDifference<V>> differences) |
(package private) static boolean |
equalsImpl(java.util.Map<?,?> map,
java.lang.Object object)
An implementation of
Map.equals(java.lang.Object) . |
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 in
unfiltered that 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 in
unfiltered that 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 in
unfiltered that 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 in
unfiltered that satisfy a predicate. |
private static <K,V> java.util.Map<K,V> |
filterFiltered(Maps.AbstractFilteredMap<K,V> map,
Predicate<? super java.util.Map.Entry<K,V>> entryPredicate)
Support
clear() , removeAll() , and retainAll() when filtering a filtered
map. |
private static <K,V> BiMap<K,V> |
filterFiltered(Maps.FilteredEntryBiMap<K,V> map,
Predicate<? super java.util.Map.Entry<K,V>> entryPredicate)
Support
clear() , removeAll() , and retainAll() when filtering a filtered
map. |
private static <K,V> java.util.NavigableMap<K,V> |
filterFiltered(Maps.FilteredEntryNavigableMap<K,V> map,
Predicate<? super java.util.Map.Entry<K,V>> entryPredicate)
Support
clear() , removeAll() , and retainAll() when filtering a filtered
navigable map. |
private static <K,V> java.util.SortedMap<K,V> |
filterFiltered(Maps.FilteredEntrySortedMap<K,V> map,
Predicate<? super java.util.Map.Entry<K,V>> entryPredicate)
Support
clear() , removeAll() , and retainAll() when filtering a filtered
sorted map. |
static <K,V> BiMap<K,V> |
filterKeys(BiMap<K,V> unfiltered,
Predicate<? super K> keyPredicate)
Returns a bimap containing the mappings in
unfiltered whose 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 in
unfiltered whose 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 in
unfiltered whose 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 in
unfiltered whose 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 in
unfiltered whose 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 in
unfiltered whose 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 in
unfiltered whose 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 in
unfiltered whose values satisfy a
predicate. |
static ImmutableMap<java.lang.String,java.lang.String> |
fromProperties(java.util.Properties properties)
Creates an
ImmutableMap<String, String> from a Properties instance. |
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> |
immutableEnumMap(java.util.Map<K,? extends V> map)
Returns an immutable map instance containing the given entries.
|
(package private) static <E> ImmutableMap<E,java.lang.Integer> |
indexMap(java.util.Collection<E> list)
Returns a map from the ith element of list to i.
|
(package private) static <K> Function<java.util.Map.Entry<K,?>,K> |
keyFunction() |
(package private) static <K,V> java.util.Iterator<K> |
keyIterator(java.util.Iterator<java.util.Map.Entry<K,V>> entryIterator) |
(package private) static <K> K |
keyOrNull(java.util.Map.Entry<K,?> entry) |
(package private) static <K> Predicate<java.util.Map.Entry<K,?>> |
keyPredicateOnEntries(Predicate<? super K> keyPredicate) |
static <K,V> java.util.concurrent.ConcurrentMap<K,V> |
newConcurrentMap()
Creates a new empty
ConcurrentHashMap instance. |
static <K extends java.lang.Enum<K>,V> |
newEnumMap(java.lang.Class<K> type)
Creates an
EnumMap instance. |
static <K extends java.lang.Enum<K>,V> |
newEnumMap(java.util.Map<K,? extends V> map)
Creates an
EnumMap with the same mappings as the specified map. |
static <K,V> java.util.HashMap<K,V> |
newHashMap()
Creates a mutable, empty
HashMap instance. |
static <K,V> java.util.HashMap<K,V> |
newHashMap(java.util.Map<? extends K,? extends V> map)
Creates a mutable
HashMap instance with the same mappings as the specified map. |
static <K,V> java.util.HashMap<K,V> |
newHashMapWithExpectedSize(int expectedSize)
Creates a
HashMap instance, with a high enough "initial capacity" that it should
hold expectedSize elements without growth. |
static <K,V> java.util.IdentityHashMap<K,V> |
newIdentityHashMap()
Creates an
IdentityHashMap instance. |
static <K,V> java.util.LinkedHashMap<K,V> |
newLinkedHashMap()
Creates a mutable, empty, insertion-ordered
LinkedHashMap instance. |
static <K,V> java.util.LinkedHashMap<K,V> |
newLinkedHashMap(java.util.Map<? extends K,? extends V> map)
Creates a mutable, insertion-ordered
LinkedHashMap instance with the same
mappings as the specified map. |
static <K,V> java.util.LinkedHashMap<K,V> |
newLinkedHashMapWithExpectedSize(int expectedSize)
Creates a
LinkedHashMap instance, with a high enough "initial capacity" that it
should hold expectedSize elements without growth. |
static <K extends java.lang.Comparable,V> |
newTreeMap()
Creates a mutable, empty
TreeMap instance using the natural ordering of its
elements. |
static <C,K extends C,V> |
newTreeMap(java.util.Comparator<C> comparator)
Creates a mutable, empty
TreeMap instance using the given comparator. |
static <K,V> java.util.TreeMap<K,V> |
newTreeMap(java.util.SortedMap<K,? extends V> map)
Creates a mutable
TreeMap instance with the same mappings as the specified map
and using the same ordering as the specified map. |
(package private) static <E> java.util.Comparator<? super E> |
orNaturalOrder(java.util.Comparator<? super E> comparator)
Returns the specified comparator if not null; otherwise returns
Ordering.natural() . |
(package private) static <K,V> void |
putAllImpl(java.util.Map<K,V> self,
java.util.Map<? extends K,? extends V> map)
An implementation of
Map.putAll(java.util.Map<? extends K, ? extends V>) . |
(package private) static <K,V> boolean |
removeEntryImpl(java.util.Collection<java.util.Map.Entry<K,V>> c,
java.lang.Object o)
Implements
Collection.remove safely for forwarding collections of map entries. |
private static <E> java.util.NavigableSet<E> |
removeOnlyNavigableSet(java.util.NavigableSet<E> set) |
private static <E> java.util.Set<E> |
removeOnlySet(java.util.Set<E> set) |
private static <E> java.util.SortedSet<E> |
removeOnlySortedSet(java.util.SortedSet<E> set) |
(package private) static boolean |
safeContainsKey(java.util.Map<?,?> map,
java.lang.Object key)
Delegates to
Map.containsKey(java.lang.Object) . |
(package private) static <V> V |
safeGet(java.util.Map<?,V> map,
java.lang.Object key)
Delegates to
Map.get(java.lang.Object) . |
(package private) static <V> V |
safeRemove(java.util.Map<?,V> map,
java.lang.Object key)
Delegates to
Map.remove(java.lang.Object) . |
static <K extends java.lang.Comparable<? super K>,V> |
subMap(java.util.NavigableMap<K,V> map,
Range<K> range)
Returns a view of the portion of
map whose keys are contained by range . |
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 <T,K extends java.lang.Enum<K>,V> |
toImmutableEnumMap(java.util.function.Function<? super T,? extends K> keyFunction,
java.util.function.Function<? super T,? extends V> valueFunction)
Returns a
Collector that accumulates elements into an ImmutableMap whose keys
and values are the result of applying the provided mapping functions to the input elements. |
static <T,K extends java.lang.Enum<K>,V> |
toImmutableEnumMap(java.util.function.Function<? super T,? extends K> keyFunction,
java.util.function.Function<? super T,? extends V> valueFunction,
java.util.function.BinaryOperator<V> mergeFunction)
Returns a
Collector that accumulates elements into an ImmutableMap whose keys
and values are the result of applying the provided mapping functions to the input elements. |
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 of
keys and whose value
for each key was computed by valueFunction . |
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 of
keys and whose value
for each key was computed by valueFunction . |
(package private) static java.lang.String |
toStringImpl(java.util.Map<?,?> map)
An implementation of
Map#toString . |
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.
|
(package private) static <V2,K,V1> java.util.Map.Entry<K,V2> |
transformEntry(Maps.EntryTransformer<? super K,? super V1,V2> transformer,
java.util.Map.Entry<K,V1> entry)
Returns a view of an entry transformed by the specified transformer.
|
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 a map with the given
values , indexed by keys derived from those values. |
static <K,V> ImmutableMap<K,V> |
uniqueIndex(java.util.Iterator<V> values,
Function<? super V,K> keyFunction)
Returns a map with the given
values , indexed by keys derived from those values. |
static <K,V> BiMap<K,V> |
unmodifiableBiMap(BiMap<? extends K,? extends V> bimap)
Returns an unmodifiable view of the specified bimap.
|
(package private) static <K,V> java.util.Map.Entry<K,V> |
unmodifiableEntry(java.util.Map.Entry<? extends K,? extends V> entry)
Returns an unmodifiable view of the specified map entry.
|
(package private) static <K,V> UnmodifiableIterator<java.util.Map.Entry<K,V>> |
unmodifiableEntryIterator(java.util.Iterator<java.util.Map.Entry<K,V>> entryIterator) |
(package private) static <K,V> java.util.Set<java.util.Map.Entry<K,V>> |
unmodifiableEntrySet(java.util.Set<java.util.Map.Entry<K,V>> entrySet)
Returns an unmodifiable view of the specified set of entries.
|
private static <K,V> java.util.Map<K,V> |
unmodifiableMap(java.util.Map<K,? extends V> map) |
static <K,V> java.util.NavigableMap<K,V> |
unmodifiableNavigableMap(java.util.NavigableMap<K,? extends V> map)
Returns an unmodifiable view of the specified navigable map.
|
private static <K,V> java.util.Map.Entry<K,V> |
unmodifiableOrNull(java.util.Map.Entry<K,? extends V> entry) |
(package private) static <V> Function<java.util.Map.Entry<?,V>,V> |
valueFunction() |
(package private) static <K,V> java.util.Iterator<V> |
valueIterator(java.util.Iterator<java.util.Map.Entry<K,V>> entryIterator) |
(package private) static <V> V |
valueOrNull(java.util.Map.Entry<?,V> entry) |
(package private) static <V> Predicate<java.util.Map.Entry<?,V>> |
valuePredicateOnEntries(Predicate<? super V> valuePredicate) |
static <K> Function<java.util.Map.Entry<K,?>,K> keyFunction()
static <V> Function<java.util.Map.Entry<?,V>,V> valueFunction()
static <K,V> java.util.Iterator<K> keyIterator(java.util.Iterator<java.util.Map.Entry<K,V>> entryIterator)
static <K,V> java.util.Iterator<V> valueIterator(java.util.Iterator<java.util.Map.Entry<K,V>> entryIterator)
@GwtCompatible(serializable=true) @Beta public static <K extends java.lang.Enum<K>,V> ImmutableMap<K,V> immutableEnumMap(java.util.Map<K,? extends V> map)
EnumMap
.
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.
map
- the map to make an immutable copy of@Beta public static <T,K extends java.lang.Enum<K>,V> java.util.stream.Collector<T,?,ImmutableMap<K,V>> toImmutableEnumMap(java.util.function.Function<? super T,? extends K> keyFunction, java.util.function.Function<? super T,? extends V> valueFunction)
Collector
that accumulates elements into an ImmutableMap
whose keys
and values are the result of applying the provided mapping functions to the input elements. The
resulting implementation is specialized for enum key types. The returned map and its views will
iterate over keys in their enum definition order, not encounter order.
If the mapped keys contain duplicates, an IllegalArgumentException
is thrown when
the collection operation is performed. (This differs from the Collector
returned by
Collectors.toMap(Function, Function)
, which throws an IllegalStateException
.)
@Beta public static <T,K extends java.lang.Enum<K>,V> java.util.stream.Collector<T,?,ImmutableMap<K,V>> toImmutableEnumMap(java.util.function.Function<? super T,? extends K> keyFunction, java.util.function.Function<? super T,? extends V> valueFunction, java.util.function.BinaryOperator<V> mergeFunction)
Collector
that accumulates elements into an ImmutableMap
whose keys
and values are the result of applying the provided mapping functions to the input elements. The
resulting implementation is specialized for enum key types. The returned map and its views will
iterate over keys in their enum definition order, not encounter order.
If the mapped keys contain duplicates, the values are merged using the specified merging function.
public static <K,V> java.util.HashMap<K,V> newHashMap()
HashMap
instance.
Note: if mutability is not required, use ImmutableMap.of()
instead.
Note: if K
is an enum
type, use newEnumMap(java.lang.Class<K>)
instead.
Note for Java 7 and later: this method is now unnecessary and should be treated as
deprecated. Instead, use the HashMap
constructor directly, taking advantage of the new
"diamond" syntax.
HashMap
public static <K,V> java.util.HashMap<K,V> newHashMap(java.util.Map<? extends K,? extends V> map)
HashMap
instance with the same mappings as the specified map.
Note: if mutability is not required, use ImmutableMap.copyOf(Map)
instead.
Note: if K
is an Enum
type, use newEnumMap(java.lang.Class<K>)
instead.
Note for Java 7 and later: this method is now unnecessary and should be treated as
deprecated. Instead, use the HashMap
constructor directly, taking advantage of the new
"diamond" syntax.
map
- the mappings to be placed in the new mapHashMap
initialized with the mappings from map
public static <K,V> java.util.HashMap<K,V> newHashMapWithExpectedSize(int expectedSize)
HashMap
instance, with a high enough "initial capacity" that it should
hold expectedSize
elements without growth. This behavior cannot be broadly guaranteed,
but it is observed to be true for OpenJDK 1.7. It also can't be guaranteed that the method
isn't inadvertently oversizing the returned map.expectedSize
- the number of entries you expect to add to the returned mapHashMap
with enough capacity to hold expectedSize
entries
without resizingjava.lang.IllegalArgumentException
- if expectedSize
is negativestatic int capacity(int expectedSize)
public static <K,V> java.util.LinkedHashMap<K,V> newLinkedHashMap()
LinkedHashMap
instance.
Note: if mutability is not required, use ImmutableMap.of()
instead.
Note for Java 7 and later: this method is now unnecessary and should be treated as
deprecated. Instead, use the LinkedHashMap
constructor directly, taking advantage of
the new "diamond" syntax.
LinkedHashMap
public static <K,V> java.util.LinkedHashMap<K,V> newLinkedHashMap(java.util.Map<? extends K,? extends V> map)
LinkedHashMap
instance with the same
mappings as the specified map.
Note: if mutability is not required, use ImmutableMap.copyOf(Map)
instead.
Note for Java 7 and later: this method is now unnecessary and should be treated as
deprecated. Instead, use the LinkedHashMap
constructor directly, taking advantage of
the new "diamond" syntax.
map
- the mappings to be placed in the new mapLinkedHashMap
initialized with the mappings from map
public static <K,V> java.util.LinkedHashMap<K,V> newLinkedHashMapWithExpectedSize(int expectedSize)
LinkedHashMap
instance, with a high enough "initial capacity" that it
should hold expectedSize
elements without growth. This behavior cannot be
broadly guaranteed, but it is observed to be true for OpenJDK 1.7. It also can't be guaranteed
that the method isn't inadvertently oversizing the returned map.expectedSize
- the number of entries you expect to add to the returned mapLinkedHashMap
with enough capacity to hold expectedSize
entries without resizingjava.lang.IllegalArgumentException
- if expectedSize
is negativepublic static <K,V> java.util.concurrent.ConcurrentMap<K,V> newConcurrentMap()
ConcurrentHashMap
instance.public static <K extends java.lang.Comparable,V> java.util.TreeMap<K,V> newTreeMap()
TreeMap
instance using the natural ordering of its
elements.
Note: if mutability is not required, use ImmutableSortedMap.of()
instead.
Note for Java 7 and later: this method is now unnecessary and should be treated as
deprecated. Instead, use the TreeMap
constructor directly, taking advantage of the new
"diamond" syntax.
TreeMap
public static <K,V> java.util.TreeMap<K,V> newTreeMap(java.util.SortedMap<K,? extends V> map)
TreeMap
instance 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.
Note for Java 7 and later: this method is now unnecessary and should be treated as
deprecated. Instead, use the TreeMap
constructor directly, taking advantage of the new
"diamond" syntax.
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 mapTreeMap
initialized with the mappings from map
and using the
comparator of map
public static <C,K extends C,V> java.util.TreeMap<K,V> newTreeMap(java.util.Comparator<C> comparator)
TreeMap
instance using the given comparator.
Note: if mutability is not required, use ImmutableSortedMap.orderedBy(comparator).build()
instead.
Note for Java 7 and later: this method is now unnecessary and should be treated as
deprecated. Instead, use the TreeMap
constructor directly, taking advantage of the new
"diamond" syntax.
comparator
- the comparator to sort the keys withTreeMap
public static <K extends java.lang.Enum<K>,V> java.util.EnumMap<K,V> newEnumMap(java.lang.Class<K> type)
EnumMap
instance.type
- the key type for this mapEnumMap
public static <K extends java.lang.Enum<K>,V> java.util.EnumMap<K,V> newEnumMap(java.util.Map<K,? extends V> map)
EnumMap
with the same mappings as the specified map.
Note for Java 7 and later: this method is now unnecessary and should be treated as
deprecated. Instead, use the EnumMap
constructor directly, taking advantage of the new
"diamond" syntax.
map
- the map from which to initialize this EnumMap
EnumMap
initialized with the mappings from map
java.lang.IllegalArgumentException
- if m
is not an EnumMap
instance and contains
no mappingspublic static <K,V> java.util.IdentityHashMap<K,V> newIdentityHashMap()
IdentityHashMap
instance.
Note for Java 7 and later: this method is now unnecessary and should be treated as
deprecated. Instead, use the IdentityHashMap
constructor directly, taking advantage of
the new "diamond" syntax.
IdentityHashMap
public static <K,V> MapDifference<K,V> difference(java.util.Map<? extends K,? extends V> left, java.util.Map<? extends K,? extends V> right)
Since this method uses HashMap
instances internally, the keys of the supplied maps
must be well-behaved with respect to Object.equals(java.lang.Object)
and Object.hashCode()
.
Note:If you only need to know whether two maps have the same mappings, call left.equals(right)
instead of this method.
left
- the map to treat as the "left" map for purposes of comparisonright
- the map to treat as the "right" map for purposes of comparisonpublic 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)
Since this method uses HashMap
instances internally, the keys of the supplied maps
must be well-behaved with respect to Object.equals(java.lang.Object)
and Object.hashCode()
.
left
- the map to treat as the "left" map for purposes of comparisonright
- the map to treat as the "right" map for purposes of comparisonvalueEquivalence
- the equivalence relationship to use to compare valuespublic static <K,V> SortedMapDifference<K,V> difference(java.util.SortedMap<K,? extends V> left, java.util.Map<? extends K,? extends V> right)
Ordering.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 TreeMap
instances 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.
left
- the map to treat as the "left" map for purposes of comparisonright
- the map to treat as the "right" map for purposes of comparisonprivate static <K,V> void doDifference(java.util.Map<? extends K,? extends V> left, java.util.Map<? extends K,? extends V> right, Equivalence<? super V> valueEquivalence, java.util.Map<K,V> onlyOnLeft, java.util.Map<K,V> onlyOnRight, java.util.Map<K,V> onBoth, java.util.Map<K,MapDifference.ValueDifference<V>> differences)
private static <K,V> java.util.Map<K,V> unmodifiableMap(java.util.Map<K,? extends V> map)
static <E> java.util.Comparator<? super E> orNaturalOrder(java.util.Comparator<? super E> comparator)
Ordering.natural()
.
This method is an abomination of generics; the only purpose of this method is to contain the
ugly type-casting in one place.public static <K,V> java.util.Map<K,V> asMap(java.util.Set<K> set, Function<? super K,V> function)
Map
view whose keys are the contents of set
and whose values are
computed on demand using function
. To get an immutable copy instead, use toMap(Iterable, Function)
.
Specifically, for each k
in the backing set, the returned map has an entry mapping
k
to function.apply(k)
. The keySet
, values
, and entrySet
views 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 contain null
, because the view cannot stop null
from being added
to the set.
Warning: This method assumes that for any instance k
of key type K
,
k.equals(k2)
implies that k2
is also of type K
. Using a key type for
which this may not hold, such as ArrayList
, may risk a ClassCastException
when
calling methods on the resulting map view.
public static <K,V> java.util.SortedMap<K,V> asMap(java.util.SortedSet<K> set, Function<? super K,V> function)
Specifically, for each k
in the backing set, the returned map has an entry mapping
k
to function.apply(k)
. The keySet
, values
, and entrySet
views 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 contain null
, because the view cannot stop null
from being added
to the set.
Warning: This method assumes that for any instance k
of key type K
,
k.equals(k2)
implies that k2
is also of type K
. Using a key type for
which this may not hold, such as ArrayList
, may risk a ClassCastException
when
calling methods on the resulting map view.
@GwtIncompatible public static <K,V> java.util.NavigableMap<K,V> asMap(java.util.NavigableSet<K> set, Function<? super K,V> function)
Specifically, for each k
in the backing set, the returned map has an entry mapping
k
to function.apply(k)
. The keySet
, values
, and entrySet
views 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 contain null
, because the view cannot stop null
from being added
to the set.
Warning: This method assumes that for any instance k
of key type K
,
k.equals(k2)
implies that k2
is also of type K
. Using a key type for
which this may not hold, such as ArrayList
, may risk a ClassCastException
when
calling methods on the resulting map view.
static <K,V> java.util.Iterator<java.util.Map.Entry<K,V>> asMapEntryIterator(java.util.Set<K> set, Function<? super K,V> function)
private static <E> java.util.Set<E> removeOnlySet(java.util.Set<E> set)
private static <E> java.util.SortedSet<E> removeOnlySortedSet(java.util.SortedSet<E> set)
@GwtIncompatible private static <E> java.util.NavigableSet<E> removeOnlyNavigableSet(java.util.NavigableSet<E> set)
public static <K,V> ImmutableMap<K,V> toMap(java.lang.Iterable<K> keys, Function<? super K,V> valueFunction)
keys
and whose value
for each key was computed by valueFunction
. The map's iteration order is the order of
the first appearance of each key in keys
.
When there are multiple instances of a key in keys
, it is unspecified whether valueFunction
will be applied to more than one instance of that key and, if it is, which
result will be mapped to that key in the returned map.
If keys
is a Set
, a live view can be obtained instead of a copy using asMap(Set, Function)
.
java.lang.NullPointerException
- if any element of keys
is null
, or if valueFunction
produces null
for any keypublic static <K,V> ImmutableMap<K,V> toMap(java.util.Iterator<K> keys, Function<? super K,V> valueFunction)
keys
and whose value
for each key was computed by valueFunction
. The map's iteration order is the order of
the first appearance of each key in keys
.
When there are multiple instances of a key in keys
, it is unspecified whether valueFunction
will be applied to more than one instance of that key and, if it is, which
result will be mapped to that key in the returned map.
java.lang.NullPointerException
- if any element of keys
is null
, or if valueFunction
produces null
for any keypublic static <K,V> ImmutableMap<K,V> uniqueIndex(java.lang.Iterable<V> values, Function<? super V,K> keyFunction)
values
, indexed by keys derived from those values. In
other words, each input value produces an entry in the map whose key is the result of applying
keyFunction
to that value. These entries appear in the same order as the input values.
Example usage:
Color red = new Color("red", 255, 0, 0);
...
ImmutableSet<Color> allColors = ImmutableSet.of(red, green, blue);
Map<String, Color> colorForName =
uniqueIndex(allColors, toStringFunction());
assertThat(colorForName).containsEntry("red", red);
If your index may associate multiple values with each key, use Multimaps.index
.
values
- the values to use when constructing the Map
keyFunction
- the function used to produce the key for each valuekeyFunction
on each value
in the input collection to that valuejava.lang.IllegalArgumentException
- if keyFunction
produces the same key for more than one
value in the input collectionjava.lang.NullPointerException
- if any element of values
is null
, or if keyFunction
produces null
for any valuepublic static <K,V> ImmutableMap<K,V> uniqueIndex(java.util.Iterator<V> values, Function<? super V,K> keyFunction)
values
, indexed by keys derived from those values. In
other words, each input value produces an entry in the map whose key is the result of applying
keyFunction
to that value. These entries appear in the same order as the input values.
Example usage:
Color red = new Color("red", 255, 0, 0);
...
Iterator<Color> allColors = ImmutableSet.of(red, green, blue).iterator();
Map<String, Color> colorForName =
uniqueIndex(allColors, toStringFunction());
assertThat(colorForName).containsEntry("red", red);
If your index may associate multiple values with each key, use Multimaps.index
.
values
- the values to use when constructing the Map
keyFunction
- the function used to produce the key for each valuekeyFunction
on each value
in the input collection to that valuejava.lang.IllegalArgumentException
- if keyFunction
produces the same key for more than one
value in the input collectionjava.lang.NullPointerException
- if any element of values
is null
, or if keyFunction
produces null
for any value@GwtIncompatible public static ImmutableMap<java.lang.String,java.lang.String> fromProperties(java.util.Properties properties)
ImmutableMap<String, String>
from a Properties
instance. Properties
normally derive from Map<Object, Object>
, but they typically contain strings, which is
awkward. This method lets you get a plain-old-Map
out of a Properties
.properties
- a Properties
object to be convertedproperties
java.lang.ClassCastException
- if any key in Properties
is not a String
java.lang.NullPointerException
- if any key or value in Properties
is null@GwtCompatible(serializable=true) public static <K,V> java.util.Map.Entry<K,V> immutableEntry(K key, V value)
Map.Entry.setValue(V)
operation throws an UnsupportedOperationException
.
The returned entry is serializable.
key
- the key to be associated with the returned entryvalue
- the value to be associated with the returned entrystatic <K,V> java.util.Set<java.util.Map.Entry<K,V>> unmodifiableEntrySet(java.util.Set<java.util.Map.Entry<K,V>> entrySet)
Map.Entry.setValue(V)
operation throws an UnsupportedOperationException
, as do any operations that would
modify the returned set.entrySet
- the entries for which to return an unmodifiable viewstatic <K,V> java.util.Map.Entry<K,V> unmodifiableEntry(java.util.Map.Entry<? extends K,? extends V> entry)
Map.Entry.setValue(V)
operation
throws an UnsupportedOperationException
. This also has the side-effect of redefining
equals
to comply with the Entry contract, to avoid a possible nefarious implementation
of equals.entry
- the entry for which to return an unmodifiable viewstatic <K,V> UnmodifiableIterator<java.util.Map.Entry<K,V>> unmodifiableEntryIterator(java.util.Iterator<java.util.Map.Entry<K,V>> entryIterator)
@Beta public static <A,B> Converter<A,B> asConverter(BiMap<A,B> bimap)
Converter
that converts values using bimap.get()
, and whose
inverse view converts values using bimap.inverse()
.get()
.
To use a plain Map
as a Function
, see Functions.forMap(Map)
or Functions.forMap(Map, Object)
.
public static <K,V> BiMap<K,V> synchronizedBiMap(BiMap<K,V> 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.
bimap
- the bimap to be wrapped in a synchronized viewpublic static <K,V> BiMap<K,V> unmodifiableBiMap(BiMap<? extends K,? extends V> bimap)
UnsupportedOperationException
.
The returned bimap will be serializable if the specified bimap is serializable.
bimap
- the bimap for which an unmodifiable view is to be returnedpublic static <K,V1,V2> java.util.Map<K,V2> transformValues(java.util.Map<K,V1> fromMap, Function<? super V1,V2> function)
Map<String, Integer> map = ImmutableMap.of("a", 4, "b", 9);
Function<Integer, Double> sqrt =
new Function<Integer, Double>() {
public Double apply(Integer in) {
return Math.sqrt((int) in);
}
};
Map<String, Double> transformed = Maps.transformValues(map, sqrt);
System.out.println(transformed);
... prints {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)
and Map.toString()
. For this to perform well, function
should 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.
public static <K,V1,V2> java.util.SortedMap<K,V2> transformValues(java.util.SortedMap<K,V1> fromMap, Function<? super V1,V2> function)
SortedMap<String, Integer> map = ImmutableSortedMap.of("a", 4, "b", 9);
Function<Integer, Double> sqrt =
new Function<Integer, Double>() {
public Double apply(Integer in) {
return Math.sqrt((int) in);
}
};
SortedMap<String, Double> transformed =
Maps.transformValues(map, sqrt);
System.out.println(transformed);
... prints {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)
and Map.toString()
. For this to perform well, function
should 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.
@GwtIncompatible public static <K,V1,V2> java.util.NavigableMap<K,V2> transformValues(java.util.NavigableMap<K,V1> fromMap, Function<? super V1,V2> function)
NavigableMap<String, Integer> map = Maps.newTreeMap();
map.put("a", 4);
map.put("b", 9);
Function<Integer, Double> sqrt =
new Function<Integer, Double>() {
public Double apply(Integer in) {
return Math.sqrt((int) in);
}
};
NavigableMap<String, Double> transformed =
Maps.transformNavigableValues(map, sqrt);
System.out.println(transformed);
... prints {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)
and Map.toString()
. For this to perform well, function
should 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.
public static <K,V1,V2> java.util.Map<K,V2> transformEntries(java.util.Map<K,V1> fromMap, Maps.EntryTransformer<? super K,? super V1,V2> transformer)
transformValues(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:
Map<String, Boolean> options =
ImmutableMap.of("verbose", true, "sort", false);
EntryTransformer<String, Boolean, String> flagPrefixer =
new EntryTransformer<String, Boolean, String>() {
public String transformEntry(String key, Boolean value) {
return value ? key : "no" + key;
}
};
Map<String, String> transformed =
Maps.transformEntries(options, flagPrefixer);
System.out.println(transformed);
... prints {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)
and Object.toString()
. For this to perform
well, transformer
should 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 k
of EntryTransformer
key type K
, k.equals(k2)
implies that k2
is also of
type K
. Using an EntryTransformer
key type for which this may not hold, such as
ArrayList
, may risk a ClassCastException
when calling methods on the
transformed map.
public static <K,V1,V2> java.util.SortedMap<K,V2> transformEntries(java.util.SortedMap<K,V1> fromMap, Maps.EntryTransformer<? super K,? super V1,V2> transformer)
transformValues(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:
Map<String, Boolean> options =
ImmutableSortedMap.of("verbose", true, "sort", false);
EntryTransformer<String, Boolean, String> flagPrefixer =
new EntryTransformer<String, Boolean, String>() {
public String transformEntry(String key, Boolean value) {
return value ? key : "yes" + key;
}
};
SortedMap<String, String> transformed =
Maps.transformEntries(options, flagPrefixer);
System.out.println(transformed);
... prints {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)
and Object.toString()
. For this to perform
well, transformer
should 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 k
of EntryTransformer
key type K
, k.equals(k2)
implies that k2
is also of
type K
. Using an EntryTransformer
key type for which this may not hold, such as
ArrayList
, may risk a ClassCastException
when calling methods on the
transformed map.
@GwtIncompatible 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)
transformValues(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:
NavigableMap<String, Boolean> options = Maps.newTreeMap();
options.put("verbose", false);
options.put("sort", true);
EntryTransformer<String, Boolean, String> flagPrefixer =
new EntryTransformer<String, Boolean, String>() {
public String transformEntry(String key, Boolean value) {
return value ? key : ("yes" + key);
}
};
NavigableMap<String, String> transformed =
LabsMaps.transformNavigableEntries(options, flagPrefixer);
System.out.println(transformed);
... prints {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)
and Object.toString()
. For this to perform
well, transformer
should 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 k
of EntryTransformer
key type K
, k.equals(k2)
implies that k2
is also of
type K
. Using an EntryTransformer
key type for which this may not hold, such as
ArrayList
, may risk a ClassCastException
when calling methods on the
transformed map.
static <K,V1,V2> Maps.EntryTransformer<K,V1,V2> asEntryTransformer(Function<? super V1,V2> function)
static <K,V1,V2> Function<V1,V2> asValueToValueFunction(Maps.EntryTransformer<? super K,V1,V2> transformer, K key)
static <K,V1,V2> Function<java.util.Map.Entry<K,V1>,V2> asEntryToValueFunction(Maps.EntryTransformer<? super K,? super V1,V2> transformer)
Entry
to values.static <V2,K,V1> java.util.Map.Entry<K,V2> transformEntry(Maps.EntryTransformer<? super K,? super V1,V2> transformer, java.util.Map.Entry<K,V1> entry)
static <K,V1,V2> Function<java.util.Map.Entry<K,V1>,java.util.Map.Entry<K,V2>> asEntryToEntryFunction(Maps.EntryTransformer<? super K,? super V1,V2> transformer)
static <K> Predicate<java.util.Map.Entry<K,?>> keyPredicateOnEntries(Predicate<? super K> keyPredicate)
static <V> Predicate<java.util.Map.Entry<?,V>> valuePredicateOnEntries(Predicate<? super V> valuePredicate)
public static <K,V> java.util.Map<K,V> filterKeys(java.util.Map<K,V> unfiltered, Predicate<? super K> keyPredicate)
unfiltered
whose keys satisfy a predicate. The
returned map is a live view of unfiltered
; changes to one affect the other.
The resulting map's keySet()
, entrySet()
, and values()
views have
iterators that don't support remove()
, but all other methods are supported by the map
and its views. When given a key that doesn't satisfy the predicate, the map's put()
and
putAll()
methods throw an IllegalArgumentException
.
When methods such as removeAll()
and clear()
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 unfiltered
is.
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: keyPredicate
must be consistent with equals, as documented at
Predicate.apply(T)
. Do not provide a predicate such as Predicates.instanceOf(ArrayList.class)
, which is inconsistent with equals.
public static <K,V> java.util.SortedMap<K,V> filterKeys(java.util.SortedMap<K,V> unfiltered, Predicate<? super K> keyPredicate)
unfiltered
whose keys satisfy a
predicate. The returned map is a live view of unfiltered
; changes to one affect the
other.
The resulting map's keySet()
, entrySet()
, and values()
views have
iterators that don't support remove()
, but all other methods are supported by the map
and its views. When given a key that doesn't satisfy the predicate, the map's put()
and
putAll()
methods throw an IllegalArgumentException
.
When methods such as removeAll()
and clear()
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 unfiltered
is.
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: keyPredicate
must be consistent with equals, as documented at
Predicate.apply(T)
. Do not provide a predicate such as Predicates.instanceOf(ArrayList.class)
, which is inconsistent with equals.
@GwtIncompatible public static <K,V> java.util.NavigableMap<K,V> filterKeys(java.util.NavigableMap<K,V> unfiltered, Predicate<? super K> keyPredicate)
unfiltered
whose keys satisfy a
predicate. The returned map is a live view of unfiltered
; changes to one affect the
other.
The resulting map's keySet()
, entrySet()
, and values()
views have
iterators that don't support remove()
, but all other methods are supported by the map
and its views. When given a key that doesn't satisfy the predicate, the map's put()
and
putAll()
methods throw an IllegalArgumentException
.
When methods such as removeAll()
and clear()
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 unfiltered
is.
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: keyPredicate
must be consistent with equals, as documented at
Predicate.apply(T)
. Do not provide a predicate such as Predicates.instanceOf(ArrayList.class)
, which is inconsistent with equals.
public static <K,V> BiMap<K,V> filterKeys(BiMap<K,V> unfiltered, Predicate<? super K> keyPredicate)
unfiltered
whose keys satisfy a predicate.
The returned bimap is a live view of unfiltered
; changes to one affect the other.
The resulting bimap's keySet()
, entrySet()
, and values()
views have
iterators that don't support remove()
, but all other methods are supported by the bimap
and its views. When given a key that doesn't satisfy the predicate, the bimap's put()
,
forcePut()
and putAll()
methods throw an IllegalArgumentException
.
When methods such as removeAll()
and clear()
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 unfiltered
is.
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: entryPredicate
must be consistent with equals , as documented
at Predicate.apply(T)
.
public static <K,V> java.util.Map<K,V> filterValues(java.util.Map<K,V> unfiltered, Predicate<? super V> valuePredicate)
unfiltered
whose values satisfy a predicate.
The returned map is a live view of unfiltered
; changes to one affect the other.
The resulting map's keySet()
, entrySet()
, and values()
views have
iterators that don't support remove()
, but all other methods are supported by the map
and its views. When given a value that doesn't satisfy the predicate, the map's put()
,
putAll()
, and Map.Entry.setValue(V)
methods throw an IllegalArgumentException
.
When methods such as removeAll()
and clear()
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 unfiltered
is.
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: valuePredicate
must be consistent with equals, as documented
at Predicate.apply(T)
. Do not provide a predicate such as Predicates.instanceOf(ArrayList.class)
, which is inconsistent with equals.
public static <K,V> java.util.SortedMap<K,V> filterValues(java.util.SortedMap<K,V> unfiltered, Predicate<? super V> valuePredicate)
unfiltered
whose values satisfy a
predicate. The returned map is a live view of unfiltered
; changes to one affect the
other.
The resulting map's keySet()
, entrySet()
, and values()
views have
iterators that don't support remove()
, but all other methods are supported by the map
and its views. When given a value that doesn't satisfy the predicate, the map's put()
,
putAll()
, and Map.Entry.setValue(V)
methods throw an IllegalArgumentException
.
When methods such as removeAll()
and clear()
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 unfiltered
is.
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: valuePredicate
must be consistent with equals, as documented
at Predicate.apply(T)
. Do not provide a predicate such as Predicates.instanceOf(ArrayList.class)
, which is inconsistent with equals.
@GwtIncompatible public static <K,V> java.util.NavigableMap<K,V> filterValues(java.util.NavigableMap<K,V> unfiltered, Predicate<? super V> valuePredicate)
unfiltered
whose values satisfy a
predicate. The returned map is a live view of unfiltered
; changes to one affect the
other.
The resulting map's keySet()
, entrySet()
, and values()
views have
iterators that don't support remove()
, but all other methods are supported by the map
and its views. When given a value that doesn't satisfy the predicate, the map's put()
,
putAll()
, and Map.Entry.setValue(V)
methods throw an IllegalArgumentException
.
When methods such as removeAll()
and clear()
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 unfiltered
is.
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: valuePredicate
must be consistent with equals, as documented
at Predicate.apply(T)
. Do not provide a predicate such as Predicates.instanceOf(ArrayList.class)
, which is inconsistent with equals.
public static <K,V> BiMap<K,V> filterValues(BiMap<K,V> unfiltered, Predicate<? super V> valuePredicate)
unfiltered
whose values satisfy a predicate.
The returned bimap is a live view of unfiltered
; changes to one affect the other.
The resulting bimap's keySet()
, entrySet()
, and values()
views have
iterators that don't support remove()
, but all other methods are supported by the bimap
and its views. When given a value that doesn't satisfy the predicate, the bimap's put()
, forcePut()
and putAll()
methods throw an IllegalArgumentException
. Similarly, the map's entries have a Map.Entry.setValue(V)
method
that throws an IllegalArgumentException
when the provided value doesn't satisfy the
predicate.
When methods such as removeAll()
and clear()
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 unfiltered
is.
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: entryPredicate
must be consistent with equals , as documented
at Predicate.apply(T)
.
public static <K,V> java.util.Map<K,V> filterEntries(java.util.Map<K,V> unfiltered, Predicate<? super java.util.Map.Entry<K,V>> entryPredicate)
unfiltered
that satisfy a predicate. The
returned map is a live view of unfiltered
; changes to one affect the other.
The resulting map's keySet()
, entrySet()
, and values()
views have
iterators that don't support remove()
, 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's put()
and putAll()
methods throw an IllegalArgumentException
. Similarly, the
map's entries have a Map.Entry.setValue(V)
method that throws an IllegalArgumentException
when the existing key and the provided value don't satisfy the
predicate.
When methods such as removeAll()
and clear()
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 unfiltered
is.
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: entryPredicate
must be consistent with equals, as documented
at Predicate.apply(T)
.
public static <K,V> java.util.SortedMap<K,V> filterEntries(java.util.SortedMap<K,V> unfiltered, Predicate<? super java.util.Map.Entry<K,V>> entryPredicate)
unfiltered
that satisfy a predicate.
The returned map is a live view of unfiltered
; changes to one affect the other.
The resulting map's keySet()
, entrySet()
, and values()
views have
iterators that don't support remove()
, 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's put()
and putAll()
methods throw an IllegalArgumentException
. Similarly, the
map's entries have a Map.Entry.setValue(V)
method that throws an IllegalArgumentException
when the existing key and the provided value don't satisfy the
predicate.
When methods such as removeAll()
and clear()
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 unfiltered
is.
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: entryPredicate
must be consistent with equals, as documented
at Predicate.apply(T)
.
@GwtIncompatible 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)
unfiltered
that satisfy a predicate.
The returned map is a live view of unfiltered
; changes to one affect the other.
The resulting map's keySet()
, entrySet()
, and values()
views have
iterators that don't support remove()
, 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's put()
and putAll()
methods throw an IllegalArgumentException
. Similarly, the
map's entries have a Map.Entry.setValue(V)
method that throws an IllegalArgumentException
when the existing key and the provided value don't satisfy the
predicate.
When methods such as removeAll()
and clear()
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 unfiltered
is.
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: entryPredicate
must be consistent with equals, as documented
at Predicate.apply(T)
.
public static <K,V> BiMap<K,V> filterEntries(BiMap<K,V> unfiltered, Predicate<? super java.util.Map.Entry<K,V>> entryPredicate)
unfiltered
that satisfy a predicate. The
returned bimap is a live view of unfiltered
; changes to one affect the other.
The resulting bimap's keySet()
, entrySet()
, and values()
views have
iterators that don't support remove()
, 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's
put()
, forcePut()
and putAll()
methods throw an IllegalArgumentException
. Similarly, the map's entries have an Map.Entry.setValue(V)
method
that throws an IllegalArgumentException
when the existing key and the provided value
don't satisfy the predicate.
When methods such as removeAll()
and clear()
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 unfiltered
is.
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: entryPredicate
must be consistent with equals , as documented
at Predicate.apply(T)
.
private static <K,V> java.util.Map<K,V> filterFiltered(Maps.AbstractFilteredMap<K,V> map, Predicate<? super java.util.Map.Entry<K,V>> entryPredicate)
clear()
, removeAll()
, and retainAll()
when filtering a filtered
map.private static <K,V> java.util.SortedMap<K,V> filterFiltered(Maps.FilteredEntrySortedMap<K,V> map, Predicate<? super java.util.Map.Entry<K,V>> entryPredicate)
clear()
, removeAll()
, and retainAll()
when filtering a filtered
sorted map.@GwtIncompatible private static <K,V> java.util.NavigableMap<K,V> filterFiltered(Maps.FilteredEntryNavigableMap<K,V> map, Predicate<? super java.util.Map.Entry<K,V>> entryPredicate)
clear()
, removeAll()
, and retainAll()
when filtering a filtered
navigable map.private static <K,V> BiMap<K,V> filterFiltered(Maps.FilteredEntryBiMap<K,V> map, Predicate<? super java.util.Map.Entry<K,V>> entryPredicate)
clear()
, removeAll()
, and retainAll()
when filtering a filtered
map.@GwtIncompatible public static <K,V> java.util.NavigableMap<K,V> unmodifiableNavigableMap(java.util.NavigableMap<K,? extends V> map)
UnsupportedOperationException
.
The returned navigable map will be serializable if the specified navigable map is serializable.
This method's signature will not permit you to convert a NavigableMap<? extends K,
V>
to a NavigableMap<K, V>
. If it permitted this, the returned map's comparator()
method might return a Comparator<? extends K>
, which works only on a
particular subtype of K
, but promise that it's a Comparator<? super K>
, which
must work on any type of K
.
map
- the navigable map for which an unmodifiable view is to be returnedprivate static <K,V> java.util.Map.Entry<K,V> unmodifiableOrNull(java.util.Map.Entry<K,? extends V> entry)
@GwtIncompatible public static <K,V> java.util.NavigableMap<K,V> synchronizedNavigableMap(java.util.NavigableMap<K,V> navigableMap)
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
, headMap
or tailMap
views.
NavigableMap<K, V> map = synchronizedNavigableMap(new TreeMap<K, V>());
// Needn't be in synchronized block
NavigableSet<K> set = map.navigableKeySet();
synchronized (map) { // Synchronizing on map, not set!
Iterator<K> it = set.iterator(); // Must be in synchronized block
while (it.hasNext()) {
foo(it.next());
}
}
or:
NavigableMap<K, V> map = synchronizedNavigableMap(new TreeMap<K, V>());
NavigableMap<K, V> map2 = map.subMap(foo, false, bar, true);
// Needn't be in synchronized block
NavigableSet<K> set2 = map2.descendingKeySet();
synchronized (map) { // Synchronizing on map, not map2 or set2!
Iterator<K> 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.
navigableMap
- the navigable map to be "wrapped" in a synchronized navigable map.static <V> V safeGet(java.util.Map<?,V> map, java.lang.Object key)
Map.get(java.lang.Object)
. Returns null
on ClassCastException
and NullPointerException
.static boolean safeContainsKey(java.util.Map<?,?> map, java.lang.Object key)
Map.containsKey(java.lang.Object)
. Returns false
on ClassCastException
and
NullPointerException
.static <V> V safeRemove(java.util.Map<?,V> map, java.lang.Object key)
Map.remove(java.lang.Object)
. Returns null
on ClassCastException
and NullPointerException
.static boolean containsKeyImpl(java.util.Map<?,?> map, java.lang.Object key)
Map.containsKey(java.lang.Object)
.static boolean containsValueImpl(java.util.Map<?,?> map, java.lang.Object value)
Map.containsValue(java.lang.Object)
.static <K,V> boolean containsEntryImpl(java.util.Collection<java.util.Map.Entry<K,V>> c, java.lang.Object o)
Collection.contains
safely for forwarding collections of map entries. If
o
is an instance of Entry
, it is wrapped using unmodifiableEntry(java.util.Map.Entry<? extends K, ? extends V>)
to
protect against a possible nefarious equals method.
Note that c
is the backing (delegate) collection, rather than the forwarding
collection.
c
- the delegate (unwrapped) collection of map entrieso
- the object that might be contained in c
true
if c
contains o
static <K,V> boolean removeEntryImpl(java.util.Collection<java.util.Map.Entry<K,V>> c, java.lang.Object o)
Collection.remove
safely for forwarding collections of map entries. If
o
is an instance of Entry
, it is wrapped using unmodifiableEntry(java.util.Map.Entry<? extends K, ? extends V>)
to
protect against a possible nefarious equals method.
Note that c
is backing (delegate) collection, rather than the forwarding collection.
c
- the delegate (unwrapped) collection of map entrieso
- the object to remove from c
true
if c
was changedstatic boolean equalsImpl(java.util.Map<?,?> map, java.lang.Object object)
Map.equals(java.lang.Object)
.static java.lang.String toStringImpl(java.util.Map<?,?> map)
Map#toString
.static <K,V> void putAllImpl(java.util.Map<K,V> self, java.util.Map<? extends K,? extends V> map)
Map.putAll(java.util.Map<? extends K, ? extends V>)
.static <K> K keyOrNull(java.util.Map.Entry<K,?> entry)
static <V> V valueOrNull(java.util.Map.Entry<?,V> entry)
static <E> ImmutableMap<E,java.lang.Integer> indexMap(java.util.Collection<E> list)
@Beta @GwtIncompatible public static <K extends java.lang.Comparable<? super K>,V> java.util.NavigableMap<K,V> subMap(java.util.NavigableMap<K,V> map, Range<K> range)
map
whose keys are contained by range
.
This method delegates to the appropriate methods of NavigableMap
(namely subMap()
, tailMap()
, and headMap()
) to actually construct the view. Consult these methods for a full
description of the returned view's behavior.
Warning: Range
s always represent a range of values using the values' natural
ordering. NavigableMap
on the other hand can specify a custom ordering via a Comparator
, which can violate the natural ordering. Using this method (or in general using
Range
) with unnaturally-ordered maps can lead to unexpected and undefined behavior.