com.google.common.collect
Class SortedMaps

java.lang.Object
  extended by com.google.common.collect.SortedMaps

@Beta
@GwtCompatible
public final class SortedMaps
extends Object

Static utility methods pertaining to SortedMap instances.

Since:
8.0
Author:
Louis Wasserman

Method Summary
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.
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.
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.
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.
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.
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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

transformValues

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);
... 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.


transformEntries

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.SortedMap, com.google.common.base.Function), 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 =
       LabsMaps.transformSortedEntries(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.


difference

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 comparison
right - the map to treat as the "right" map for purposes of comparison
Returns:
the difference between the two maps

filterKeys

@GwtIncompatible(value="untested")
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 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.


filterValues

@GwtIncompatible(value="untested")
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 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.


filterEntries

@GwtIncompatible(value="untested")
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 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).



Copyright © 2010-2011. All Rights Reserved.