javadoc.com.google.common.collect.Maps.html Maven / Gradle / Ivy
Maps (Guava: Google Core Libraries for Java 11.0.1 API)
Overview
Package
Class
Use
Tree
Deprecated
Index
Help
PREV CLASS
NEXT CLASS
FRAMES
NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD
DETAIL: FIELD | CONSTR | METHOD
com.google.common.collect
Class Maps
java.lang.Object
com.google.common.collect.Maps
@GwtCompatible(emulated=true)
public final class Maps
- extends Object
Static utility methods pertaining to Map
instances. Also see this
class's counterparts Lists
and Sets
.
- Since:
- 2.0 (imported from Google Collections Library)
- Author:
- Kevin Bourrillion, Mike Bostock, Isaac Shum, Louis Wasserman
Nested Class Summary | |
---|---|
static interface |
Maps.EntryTransformer<K,V1,V2>
A transformation of the value of a key-value pair, using both key and value as inputs. |
Method Summary | ||
---|---|---|
static
|
difference(Map<? extends K,? extends V> left,
Map<? extends K,? extends V> right)
Computes the difference between two maps. |
|
static
|
difference(Map<? extends K,? extends V> left,
Map<? extends K,? extends V> right,
Equivalence<? super V> valueEquivalence)
Computes the difference between two maps. |
|
static
|
difference(SortedMap<K,? extends V> left,
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. |
|
static
|
filterEntries(Map<K,V> unfiltered,
Predicate<? super Map.Entry<K,V>> entryPredicate)
Returns a map containing the mappings in unfiltered that satisfy a
predicate. |
|
static
|
filterEntries(SortedMap<K,V> unfiltered,
Predicate<? super Map.Entry<K,V>> entryPredicate)
Returns a sorted map containing the mappings in unfiltered that
satisfy a predicate. |
|
static
|
filterKeys(Map<K,V> unfiltered,
Predicate<? super K> keyPredicate)
Returns a map containing the mappings in unfiltered whose keys
satisfy a predicate. |
|
static
|
filterKeys(SortedMap<K,V> unfiltered,
Predicate<? super K> keyPredicate)
Returns a sorted map containing the mappings in unfiltered whose
keys satisfy a predicate. |
|
static
|
filterValues(Map<K,V> unfiltered,
Predicate<? super V> valuePredicate)
Returns a map containing the mappings in unfiltered whose values
satisfy a predicate. |
|
static
|
filterValues(SortedMap<K,V> unfiltered,
Predicate<? super V> valuePredicate)
Returns a sorted map containing the mappings in unfiltered whose
values satisfy a predicate. |
|
static ImmutableMap<String,String> |
fromProperties(Properties properties)
Creates an ImmutableMap<String, String> from a Properties
instance. |
|
static
|
immutableEntry(K key,
V value)
Returns an immutable map entry with the specified key and value. |
|
static
|
newConcurrentMap()
Returns a general-purpose instance of ConcurrentMap , which supports
all optional operations of the ConcurrentMap interface. |
|
static
|
newEnumMap(Class<K> type)
Creates an EnumMap instance. |
|
static
|
newEnumMap(Map<K,? extends V> map)
Creates an EnumMap with the same mappings as the specified map. |
|
static
|
newHashMap()
Creates a mutable, empty HashMap instance. |
|
static
|
newHashMap(Map<? extends K,? extends V> map)
Creates a mutable HashMap instance with the same mappings as
the specified map. |
|
static
|
newHashMapWithExpectedSize(int expectedSize)
Creates a HashMap instance, with a high enough "initial capacity"
that it should hold expectedSize elements without growth. |
|
static
|
newIdentityHashMap()
Creates an IdentityHashMap instance. |
|
static
|
newLinkedHashMap()
Creates a mutable, empty, insertion-ordered LinkedHashMap
instance. |
|
static
|
newLinkedHashMap(Map<? extends K,? extends V> map)
Creates a mutable, insertion-ordered LinkedHashMap instance
with the same mappings as the specified map. |
|
static
|
newTreeMap()
Creates a mutable, empty TreeMap instance using the natural
ordering of its elements. |
|
static
|
newTreeMap(Comparator<C> comparator)
Creates a mutable, empty TreeMap instance using the given
comparator. |
|
static
|
newTreeMap(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. |
|
static
|
synchronizedBiMap(BiMap<K,V> bimap)
Returns a synchronized (thread-safe) bimap backed by the specified bimap. |
|
static
|
transformEntries(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
|
transformEntries(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
|
transformValues(Map<K,V1> fromMap,
Function<? super V1,V2> function)
Returns a view of a map where each value is transformed by a function. |
|
static
|
transformValues(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
|
uniqueIndex(I values,
Function<? super V,K> keyFunction)
Deprecated. use uniqueIndex(Iterator, Function) by casting values to Iterator<V> , or better yet, by implementing only
Iterator and not Iterable . This method is scheduled
for deletion in March 2012. |
|
static
|
uniqueIndex(Iterable<V> values,
Function<? super V,K> keyFunction)
Returns an immutable map for which the Map.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
|
uniqueIndex(Iterator<V> values,
Function<? super V,K> keyFunction)
Returns an immutable map for which the Map.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
|
unmodifiableBiMap(BiMap<? extends K,? extends V> bimap)
Returns an unmodifiable view of the specified bimap. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method Detail |
---|
newHashMap
public static <K,V> HashMap<K,V> newHashMap()
- Creates a mutable, empty
HashMap
instance.Note: if mutability is not required, use
ImmutableMap.of()
instead.Note: if
K
is anenum
type, usenewEnumMap(java.lang.Class
instead.) - Returns:
- a new, empty
HashMap
newHashMapWithExpectedSize
public static <K,V> HashMap<K,V> newHashMapWithExpectedSize(int expectedSize)
- Creates a
HashMap
instance, with a high enough "initial capacity" that it should holdexpectedSize
elements 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
HashMap
with enough capacity to holdexpectedSize
elements without resizing - Throws:
IllegalArgumentException
- ifexpectedSize
is negative
newHashMap
public static <K,V> HashMap<K,V> newHashMap(Map<? extends K,? extends V> map)
- Creates a mutable
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 anEnum
type, usenewEnumMap(java.lang.Class
instead.) - Parameters:
map
- the mappings to be placed in the new map- Returns:
- a new
HashMap
initialized with the mappings frommap
newLinkedHashMap
public static <K,V> LinkedHashMap<K,V> newLinkedHashMap()
- Creates a mutable, empty, insertion-ordered
LinkedHashMap
instance.Note: if mutability is not required, use
ImmutableMap.of()
instead.- Returns:
- a new, empty
LinkedHashMap
newLinkedHashMap
public static <K,V> LinkedHashMap<K,V> newLinkedHashMap(Map<? extends K,? extends V> map)
- Creates a mutable, insertion-ordered
LinkedHashMap
instance 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,
LinkedHashMap
initialized with the mappings frommap
newConcurrentMap
public static <K,V> ConcurrentMap<K,V> newConcurrentMap()
- Returns a general-purpose instance of
ConcurrentMap
, which supports all optional operations of the ConcurrentMap interface. It does not permit null keys or values. It is serializable.This is currently accomplished by calling
MapMaker.makeMap()
.It is preferable to use
MapMaker
directly (rather than through this method), as it presents numerous useful configuration options, such as the concurrency level, load factor, key/value reference types, and value computation.- Returns:
- a new, empty
ConcurrentMap
- Since:
- 3.0
newTreeMap
public static <K extends Comparable,V> TreeMap<K,V> newTreeMap()
- Creates a mutable, empty
TreeMap
instance using the natural ordering of its elements.Note: if mutability is not required, use
ImmutableSortedMap.of()
instead.- Returns:
- a new, empty
TreeMap
newTreeMap
public static <K,V> TreeMap<K,V> newTreeMap(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.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
TreeMap
initialized with the mappings frommap
and using the comparator ofmap
newTreeMap
public static <C,K extends C,V> TreeMap<K,V> newTreeMap(@Nullable Comparator<C> comparator)
- Creates a mutable, empty
TreeMap
instance 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
newEnumMap
public static <K extends Enum<K>,V> EnumMap<K,V> newEnumMap(Class<K> type)
- Creates an
EnumMap
instance.- Parameters:
type
- the key type for this map- Returns:
- a new, empty
EnumMap
newEnumMap
public static <K extends Enum<K>,V> EnumMap<K,V> newEnumMap(Map<K,? extends V> map)
- Creates an
EnumMap
with the same mappings as the specified map.- Parameters:
map
- the map from which to initialize thisEnumMap
- Returns:
- a new
EnumMap
initialized with the mappings frommap
- Throws:
IllegalArgumentException
- ifm
is not anEnumMap
instance and contains no mappings
newIdentityHashMap
public static <K,V> IdentityHashMap<K,V> newIdentityHashMap()
- Creates an
IdentityHashMap
instance.- Returns:
- a new, empty
IdentityHashMap
synchronizedBiMap
public 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()); } }
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
difference
public static <K,V> MapDifference<K,V> difference(Map<? extends K,? extends V> left, 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
HashMap
instances 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 comparisonright
- 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(Map<? extends K,? extends V> left, 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
HashMap
instances 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 comparisonright
- the map to treat as the "right" map for purposes of comparisonvalueEquivalence
- the equivalence relationship to use to compare values- Returns:
- the difference between the two maps
- Since:
- 10.0
difference
@Beta public static <K,V> SortedMapDifference<K,V> difference(SortedMap<K,? extends V> left, 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. 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.- Parameters:
left
- the map to treat as the "left" map for purposes of comparisonright
- the map to treat as the "right" map for purposes of comparison- Returns:
- the difference between the two maps
- Since:
- 11.0
uniqueIndex
public static <K,V> ImmutableMap<K,V> uniqueIndex(Iterable<V> values, Function<? super V,K> keyFunction)
- Returns an immutable map for which the
Map.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 theMap
keyFunction
- the function used to produce the key for each value- Returns:
- a map mapping the result of evaluating the function
keyFunction
on each value in the input collection to that value - Throws:
IllegalArgumentException
- ifkeyFunction
produces the same key for more than one value in the input collectionNullPointerException
- if any elements ofvalues
is null, or ifkeyFunction
producesnull
for any value
uniqueIndex
@Beta @Deprecated public static <K,V,I extends Object & Iterable<V> & Iterator<V>> ImmutableMap<K,V> uniqueIndex(I values, Function<? super V,K> keyFunction)
- Deprecated. use
uniqueIndex(Iterator, Function)
by castingvalues
toIterator<V>
, or better yet, by implementing onlyIterator
and notIterable
. This method is scheduled for deletion in March 2012.- Deprecated.
- Since:
- 10.0
- Deprecated.
uniqueIndex
public static <K,V> ImmutableMap<K,V> uniqueIndex(Iterator<V> values, Function<? super V,K> keyFunction)
- Returns an immutable map for which the
Map.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 theMap
keyFunction
- the function used to produce the key for each value- Returns:
- a map mapping the result of evaluating the function
keyFunction
on each value in the input collection to that value - Throws:
IllegalArgumentException
- ifkeyFunction
produces the same key for more than one value in the input collectionNullPointerException
- if any elements ofvalues
is null, or ifkeyFunction
producesnull
for any value- Since:
- 10.0
fromProperties
@GwtIncompatible(value="java.util.Properties") public static ImmutableMap<String,String> fromProperties(Properties properties)
- Creates an
ImmutableMap<String, String>
from aProperties
instance. Properties normally derive fromMap<Object, Object>
, but they typically contain strings, which is awkward. This method lets you get a plain-old-Map
out of aProperties
.- Parameters:
properties
- aProperties
object to be converted- Returns:
- an immutable map containing all the entries in
properties
- Throws:
ClassCastException
- if any key inProperties
is not aString
NullPointerException
- if any key or value inProperties
is null
immutableEntry
@GwtCompatible(serializable=true) public static <K,V> Map.Entry<K,V> immutableEntry(@Nullable K key, @Nullable V value)
- Returns an immutable map entry with the specified key and value. The
Map.Entry.setValue(V)
operation throws anUnsupportedOperationException
.The returned entry is serializable.
- Parameters:
key
- the key to be associated with the returned entryvalue
- the value to be associated with the returned entry
unmodifiableBiMap
public 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 an
UnsupportedOperationException
.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
transformValues
public static <K,V1,V2> Map<K,V2> transformValues(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:
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);
{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,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.
transformValues
@Beta public static <K,V1,V2> SortedMap<K,V2> transformValues(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:
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.transformSortedValues(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,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.- Since:
- 11.0
transformEntries
public static <K,V1,V2> Map<K,V2> transformEntries(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 to
transformValues(java.util.Map
, this method's entry-transformation logic may depend on the key as well as the value., com.google.common.base.Function super V1, V2>) 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);
{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,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
ofEntryTransformer
key typeK
,k.equals(k2)
implies thatk2
is also of typeK
. Using anEntryTransformer
key type for which this may not hold, such asArrayList
, may risk aClassCastException
when calling methods on the transformed map.- Since:
- 7.0
transformEntries
@Beta public static <K,V1,V2> SortedMap<K,V2> transformEntries(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 to
transformValues(java.util.Map
, this method's entry-transformation logic may depend on the key as well as the value., com.google.common.base.Function super V1, V2>) 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 = LabsMaps.transformSortedEntries(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,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
ofEntryTransformer
key typeK
,k.equals(k2)
implies thatk2
is also of typeK
. Using anEntryTransformer
key type for which this may not hold, such asArrayList
, may risk aClassCastException
when calling methods on the transformed map.- Since:
- 11.0
filterKeys
public static <K,V> Map<K,V> filterKeys(Map<K,V> unfiltered, Predicate<? super K> keyPredicate)
- Returns a map containing the mappings in
unfiltered
whose 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
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 atPredicate.apply(T)
. Do not provide a predicate such asPredicates.instanceOf(ArrayList.class)
, which is inconsistent with equals.
filterKeys
@Beta public static <K,V> SortedMap<K,V> filterKeys(SortedMap<K,V> unfiltered, Predicate<? super K> keyPredicate)
- Returns a sorted map containing the mappings in
unfiltered
whose 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
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 atPredicate.apply(T)
. Do not provide a predicate such asPredicates.instanceOf(ArrayList.class)
, which is inconsistent with equals.- Since:
- 11.0
filterValues
public static <K,V> Map<K,V> filterValues(Map<K,V> unfiltered, Predicate<? super V> valuePredicate)
- Returns a map containing the mappings in
unfiltered
whose 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
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 atPredicate.apply(T)
. Do not provide a predicate such asPredicates.instanceOf(ArrayList.class)
, which is inconsistent with equals.
filterValues
@Beta public static <K,V> SortedMap<K,V> filterValues(SortedMap<K,V> unfiltered, Predicate<? super V> valuePredicate)
- Returns a sorted map containing the mappings in
unfiltered
whose 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
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 atPredicate.apply(T)
. Do not provide a predicate such asPredicates.instanceOf(ArrayList.class)
, which is inconsistent with equals.- Since:
- 11.0
filterEntries
public static <K,V> Map<K,V> filterEntries(Map<K,V> unfiltered, Predicate<? super Map.Entry<K,V>> entryPredicate)
- Returns a map containing the mappings in
unfiltered
that 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 anIllegalArgumentException
when 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
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 atPredicate.apply(T)
.
filterEntries
@Beta public static <K,V> SortedMap<K,V> filterEntries(SortedMap<K,V> unfiltered, Predicate<? super Map.Entry<K,V>> entryPredicate)
- Returns a sorted map containing the mappings in
unfiltered
that 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 anIllegalArgumentException
when 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
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 atPredicate.apply(T)
.- Since:
- 11.0
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
Copyright © 2010-2012. All Rights Reserved.