All Downloads are FREE. Search and download functionalities are using the official Maven repository.

edu.stanford.nlp.util.Maps Maven / Gradle / Ivy

package edu.stanford.nlp.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import java.util.function.Function;

/**
 * Utilities for Maps, including inverting, composing, and support for list/set values.
 *
 * @author Dan Klein ([email protected])
 */
public class Maps {

  private Maps() {}

  /**
   * Adds the value to the HashSet given by map.get(key), creating a new HashMap if needed.
   *
   */
  public static  void putIntoValueHashSet(Map> map, K key, V value) {
    CollectionFactory factory = CollectionFactory.hashSetFactory();
    putIntoValueCollection(map, key, value, factory);
  }

  /**
   * Adds the value to the ArrayList given by map.get(key), creating a new ArrayList if needed.
   *
   */
  public static  void putIntoValueArrayList(Map> map, K key, V value) {
    CollectionFactory factory = CollectionFactory.arrayListFactory();
    putIntoValueCollection(map, key, value, factory);
  }

  /**
   * Adds the value to the collection given by map.get(key).  A new collection is created using the supplied CollectionFactory.
   *
   */
  public static > void putIntoValueCollection(Map map, K key, V value, CollectionFactory cf) {
    C c = map.get(key);
    if (c == null) {
      c = ErasureUtils.uncheckedCast(cf.newCollection());
      map.put(key, c);
    }
    c.add(value);
  }

  /**
   * Compose two maps map1:x->y and map2:y->z to get a map x->z
   *
   * @return The composed map
   */
  public static  Map compose(Map map1, Map map2) {
    Map composedMap = Generics.newHashMap();
    for (X key : map1.keySet()) {
      composedMap.put(key, map2.get(map1.get(key)));
    }
    return composedMap;
  }

  /**
   * 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.
   *
   * @return The inverted map
   */
  public static  Map invert(Map map) {
    Map invertedMap = Generics.newHashMap();
    for (Map.Entry entry : map.entrySet()) {
      X key = entry.getKey();
      Y value = entry.getValue();
      invertedMap.put(value, key);
    }
    return invertedMap;
  }

  /**
   * Inverts a map x->y to a map y->pow(x) not assuming unique preimages.
   *
   * @return The inverted set
   */
  public static  Map> invertSet(Map map) {
    Map> invertedMap = Generics.newHashMap();
    for (Map.Entry entry : map.entrySet()) {
      X key = entry.getKey();
      Y value = entry.getValue();
      putIntoValueHashSet(invertedMap, value, key);
    }
    return invertedMap;
  }

  /**
   * Sorts a list of entries.  This method is here since the entries might come from a Counter.
   */
  public static , V> List> sortedEntries(Collection> entries) {
    List> entriesList = new ArrayList>(entries);
    Collections.sort(entriesList, (e1, e2) -> e1.getKey().compareTo(e2.getKey()));
    return entriesList;
  }

  /**
   * Returns a List of entries in the map, sorted by key.
   */
  public static , V> List> sortedEntries(Map map) {
    return sortedEntries(map.entrySet());
  }

  /**
   * Stringifies a Map in a stable fashion.
   */
  public static , V> void toStringSorted(Map map, StringBuilder builder) {
    builder.append("{");
    List> sortedProperties = Maps.sortedEntries(map);
    int index = 0;
    for (Entry entry : sortedProperties) {
      if (index > 0) {
        builder.append(", ");
      }
      builder.append(entry.getKey()).append("=").append(entry.getValue());
      index++;
    }
    builder.append("}");
  }

  /**
   * Stringifies a Map in a stable fashion.
   */
  public static , V> String toStringSorted(Map map) {
    StringBuilder builder = new StringBuilder();
    toStringSorted(map, builder);
    return builder.toString();
  }

  /**
   * Removes keys from the map
   */
  public static  void removeKeys(Map map, Collection removekeys){
    for(K k: removekeys){
      map.remove(k);
    }
  }

  /**
   * Adds all of the keys in from to to,
   * applying function to the values to transform them
   * from V2 to V1.
   */
  public static  void addAll(Map to, Map from, Function function) {
    for (Map.Entry entry : from.entrySet()) {
      to.put(entry.getKey(), function.apply(entry.getValue()));
    }
  }

  /**
   * get all values corresponding to the indices (if they exist in the map)
   * @param map
   * @param indices
   * @return
   */
  public static Map getAll(Map map, Collection indices){
    Map result = new HashMap();
    for(T i: indices)
      if(map.containsKey(i)){
        result.put(i, map.get(i));
      }
    return result;
  }

  public static void main(String[] args) {
    Map map1 = Generics.newHashMap();
    map1.put("a", "1");
    map1.put("b", "2");
    map1.put("c", "2");
    map1.put("d", "4");
    Map map2 = Generics.newHashMap();
    map2.put("1", "x");
    map2.put("2", "y");
    map2.put("3", "z");
    System.out.println("map1: " + map1);
    System.out.println("invert(map1): " + Maps.invert(map1));
    System.out.println("invertSet(map1): " + Maps.invertSet(map1));
    System.out.println("map2: " + map2);
    System.out.println("compose(map1,map2): " + Maps.compose(map1, map2));
    Map> setValues = Generics.newHashMap();
    Map> listValues = Generics.newHashMap();
    Maps.putIntoValueArrayList(listValues, "a", "1");
    Maps.putIntoValueArrayList(listValues, "a", "1");
    Maps.putIntoValueArrayList(listValues, "a", "2");
    Maps.putIntoValueHashSet(setValues, "a", "1");
    Maps.putIntoValueHashSet(setValues, "a", "1");
    Maps.putIntoValueHashSet(setValues, "a", "2");
    System.out.println("listValues: " + listValues);
    System.out.println("setValues: " + setValues);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy