|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.google.common.collect.Maps
public final class Maps
Static utility methods pertaining to Map
instances. Also see this
class's counterparts Lists
and Sets
.
Method Summary | ||
---|---|---|
static
|
difference(Map<? extends K,? extends V> left,
Map<? extends K,? extends V> right)
Computes the difference between two maps. |
|
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
|
filterKeys(Map<K,V> unfiltered,
Predicate<? super K> keyPredicate)
Returns a 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 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 enough capacity to hold the
specified number of elements without rehashing. |
|
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
|
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
|
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
|
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 |
---|
public static <K,V> 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
instead.
HashMap
public static <K,V> HashMap<K,V> newHashMapWithExpectedSize(int expectedSize)
HashMap
instance with enough capacity to hold the
specified number of elements without rehashing.
expectedSize
- the expected size
HashMap
with enough capacity to hold expectedSize
elements without rehashing
IllegalArgumentException
- if expectedSize
is negativepublic static <K,V> HashMap<K,V> newHashMap(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
instead.
map
- the mappings to be placed in the new map
HashMap
initialized with the mappings from map
public static <K,V> LinkedHashMap<K,V> newLinkedHashMap()
LinkedHashMap
instance.
Note: if mutability is not required, use ImmutableMap.of()
instead.
LinkedHashMap
public static <K,V> LinkedHashMap<K,V> newLinkedHashMap(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.
map
- the mappings to be placed in the new map
LinkedHashMap
initialized with the mappings from
map
public static <K,V> ConcurrentMap<K,V> newConcurrentMap()
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.
ConcurrentMap
public static <K extends Comparable,V> TreeMap<K,V> newTreeMap()
TreeMap
instance using the natural
ordering of its elements.
Note: if mutability is not required, use ImmutableSortedMap.of()
instead.
TreeMap
public static <K,V> TreeMap<K,V> newTreeMap(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.
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
TreeMap
initialized with the mappings from map
and using the comparator of map
public static <C,K extends C,V> TreeMap<K,V> newTreeMap(@Nullable Comparator<C> comparator)
TreeMap
instance using the given
comparator.
Note: if mutability is not required, use ImmutableSortedMap.orderedBy(comparator).build()
instead.
comparator
- the comparator to sort the keys with
TreeMap
public static <K extends Enum<K>,V> EnumMap<K,V> newEnumMap(Class<K> type)
EnumMap
instance.
type
- the key type for this map
EnumMap
public static <K extends Enum<K>,V> EnumMap<K,V> newEnumMap(Map<K,? extends V> map)
EnumMap
with the same mappings as the specified map.
map
- the map from which to initialize this EnumMap
EnumMap
initialized with the mappings from map
IllegalArgumentException
- if m
is not an EnumMap
instance and contains no mappingspublic static <K,V> IdentityHashMap<K,V> newIdentityHashMap()
IdentityHashMap
instance.
IdentityHashMap
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 view
public static <K,V> MapDifference<K,V> difference(Map<? extends K,? extends V> left, 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 comparison
public static <K,V> ImmutableMap<K,V> uniqueIndex(Iterable<V> values, Function<? super V,K> keyFunction)
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.
values
- the values to use when constructing the Map
keyFunction
- the function used to produce the key for each value
keyFunction
on each value in the input collection to that value
IllegalArgumentException
- if keyFunction
produces the same
key for more than one value in the input collection
NullPointerException
- if any elements of values
is null, or
if keyFunction
produces null
for any valuepublic static ImmutableMap<String,String> fromProperties(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 converted
properties
ClassCastException
- if any key in Properties
is not a String
NullPointerException
- if any key or value in Properties
is
nullpublic static <K,V> Map.Entry<K,V> immutableEntry(@Nullable K key, @Nullable 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 entrypublic 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 returned
public static <K,V1,V2> Map<K,V2> transformValues(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,V> Map<K,V> filterKeys(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. The map's put()
and putAll()
methods throw an IllegalArgumentException
if a key that doesn't satisfy the predicate is
provided.
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.
public static <K,V> Map<K,V> filterValues(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. The Map.put(K, V)
,
Map.putAll(java.util.Map extends K, ? extends V>)
, and Map.Entry.setValue(V)
methods throw an IllegalArgumentException
if a value that doesn't satisfy the predicate is
provided.
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.
public static <K,V> Map<K,V> filterEntries(Map<K,V> unfiltered, Predicate<? super 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. The map's put()
and putAll()
methods throw an IllegalArgumentException
if a key/value pair that doesn't satisfy the
predicate is provided. 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.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |