Package edu.stanford.nlp.util
Class Maps
- java.lang.Object
-
- edu.stanford.nlp.util.Maps
-
public class Maps extends Object
Utilities for Maps, including inverting, composing, and support for list/set values.- Author:
- Dan Klein (klein@cs.stanford.edu)
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <K,V1,V2>
voidaddAll(Map<K,V1> to, Map<K,V2> from, Function<V2,V1> function)Adds all of the keys infromtoto, applyingfunctionto the values to transform them fromV2toV1.static <X,Y,Z>
Map<X,Z>compose(Map<X,Y> map1, Map<Y,Z> map2)Compose two maps map1:x->y and map2:y->z to get a map x->zstatic <T,V>
Map<T,V>getAll(Map<T,V> map, Collection<T> indices)get all values corresponding to the indices (if they exist in the map)static booleangetBool(Map<String,String> props, String key)Load a boolean property from a Map.static booleangetBool(Map<String,String> props, String key, boolean defaultValue)Load a boolean property from a Map.static <X,Y>
Map<Y,X>invert(Map<X,Y> map)Inverts a map x->y to a map y->x assuming unique preimages.static <X,Y>
Map<Y,Set<X>>invertSet(Map<X,Y> map)Inverts a map x->y to a map y->pow(x) not assuming unique preimages.static voidmain(String[] args)static <K,V>
voidputIntoValueArrayList(Map<K,List<V>> map, K key, V value)Adds the value to the ArrayList given by map.get(key), creating a new ArrayList if needed.static <K,V,C extends Collection<V>>
voidputIntoValueCollection(Map<K,C> map, K key, V value, CollectionFactory<V> cf)Adds the value to the collection given by map.get(key).static <K,V>
voidputIntoValueHashSet(Map<K,Set<V>> map, K key, V value)Adds the value to the HashSet given by map.get(key), creating a new HashMap if needed.static <K,V>
voidremoveKeys(Map<K,V> map, Collection<K> removekeys)Removes keys from the mapstatic <K extends Comparable<? super K>,V>
List<Map.Entry<K,V>>sortedEntries(Collection<Map.Entry<K,V>> entries)Sorts a list of entries.static <K extends Comparable<? super K>,V>
List<Map.Entry<K,V>>sortedEntries(Map<K,V> map)Returns a List of entries in the map, sorted by key.static <T,V>
StringtoString(Map<T,V> map, String preAppend, String postAppend, String keyValSeparator, String itemSeparator)Pretty print a Counter.static <K extends Comparable<K>,V>
StringtoStringSorted(Map<K,V> map)Stringifies a Map in a stable fashion.static <K extends Comparable<K>,V>
voidtoStringSorted(Map<K,V> map, StringBuilder builder)Stringifies a Map in a stable fashion.
-
-
-
Method Detail
-
putIntoValueHashSet
public static <K,V> void putIntoValueHashSet(Map<K,Set<V>> map, K key, V value)
Adds the value to the HashSet given by map.get(key), creating a new HashMap if needed.
-
putIntoValueArrayList
public static <K,V> void putIntoValueArrayList(Map<K,List<V>> map, K key, V value)
Adds the value to the ArrayList given by map.get(key), creating a new ArrayList if needed.
-
putIntoValueCollection
public static <K,V,C extends Collection<V>> void putIntoValueCollection(Map<K,C> map, K key, V value, CollectionFactory<V> cf)
Adds the value to the collection given by map.get(key). A new collection is created using the supplied CollectionFactory.
-
compose
public static <X,Y,Z> Map<X,Z> compose(Map<X,Y> map1, Map<Y,Z> map2)
Compose two maps map1:x->y and map2:y->z to get a map x->z- Returns:
- The composed map
-
invert
public static <X,Y> Map<Y,X> invert(Map<X,Y> map)
Inverts a map x->y to a map y->x assuming unique preimages. If they are not unique, you get an arbitrary ones as the values in the inverted map.- Returns:
- The inverted map
-
invertSet
public static <X,Y> Map<Y,Set<X>> invertSet(Map<X,Y> map)
Inverts a map x->y to a map y->pow(x) not assuming unique preimages.- Returns:
- The inverted set
-
sortedEntries
public static <K extends Comparable<? super K>,V> List<Map.Entry<K,V>> sortedEntries(Collection<Map.Entry<K,V>> entries)
Sorts a list of entries. This method is here since the entries might come from a Counter.
-
sortedEntries
public static <K extends Comparable<? super K>,V> List<Map.Entry<K,V>> sortedEntries(Map<K,V> map)
Returns a List of entries in the map, sorted by key.
-
toStringSorted
public static <K extends Comparable<K>,V> void toStringSorted(Map<K,V> map, StringBuilder builder)
Stringifies a Map in a stable fashion.
-
toStringSorted
public static <K extends Comparable<K>,V> String toStringSorted(Map<K,V> map)
Stringifies a Map in a stable fashion.
-
removeKeys
public static <K,V> void removeKeys(Map<K,V> map, Collection<K> removekeys)
Removes keys from the map
-
addAll
public static <K,V1,V2> void addAll(Map<K,V1> to, Map<K,V2> from, Function<V2,V1> function)
Adds all of the keys infromtoto, applyingfunctionto the values to transform them fromV2toV1.
-
getAll
public static <T,V> Map<T,V> getAll(Map<T,V> map, Collection<T> indices)
get all values corresponding to the indices (if they exist in the map)- Parameters:
map-indices-- Returns:
- a submap corresponding to the indices
-
getBool
public static boolean getBool(Map<String,String> props, String key)
Load a boolean property from a Map. If the key is not present, returns false.
-
getBool
public static boolean getBool(Map<String,String> props, String key, boolean defaultValue)
Load a boolean property from a Map. If the key is not present, returns defaultValue.
-
toString
public static <T,V> String toString(Map<T,V> map, String preAppend, String postAppend, String keyValSeparator, String itemSeparator)
Pretty print a Counter. This one has more flexibility in formatting, and doesn't sort the keys.
-
main
public static void main(String[] args)
-
-