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;

/**
 * Utilities for Maps, including inverting, composing, and support for list/set values.
 * 

* @author Dan Klein ([email protected]) * Date: Oct 22, 2003 * Time: 8:56:16 PM */ public class 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 = new HashMap(); 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 = new HashMap(); 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 = new HashMap>(); 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 menthod 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, new Comparator>() { public int compare(Map.Entry e1, Map.Entry e2) { return 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(); } public static void main(String[] args) { Map map1 = new HashMap(); map1.put("a", "1"); map1.put("b", "2"); map1.put("c", "2"); map1.put("d", "4"); Map map2 = new HashMap(); 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 = new HashMap>(); Map> listValues = new HashMap>(); 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