/** * Adds the value to the ArrayList given by map.get(key), creating a new ArrayList if needed. * */ public static <K, V> void putIntoValueArrayList(Map<K, List<V>> map, K key, V value) { CollectionFactory<V> factory = CollectionFactory.arrayListFactory(); putIntoValueCollection(map, key, value, factory); }
/** * Creates a new empty TwoDimensionalCollectionValuedMap which uses a HashMap as the * underlying Map, and HashSets as the Collections in each mapping. Does not * treat Collections as immutable. */ public TwoDimensionalCollectionValuedMap() { this(MapFactory.<K2,Collection<V>>hashMapFactory(), CollectionFactory.<V>hashSetFactory(), false); }
/** Just add the key (empty collection, but key is in the keySet). */ public void addKey(K key) { Collection<V> c = map.get(key); if (c == null) { c = cf.newCollection(); map.put(key, c); } }
@Override public Collection<V> get(Object key) { // key could be not in original or in deltaMap // key could be not in original but in deltaMap // key could be in original but removed from deltaMap // key could be in original but mapped to something else in deltaMap Collection<V> deltaResult = deltaMap.get(key); if (deltaResult == null) { return originalMap.get(key); } if (deltaResult == removedValue) { return cf.newEmptyCollection(); } return deltaResult; }
/** * Creates a new CollectionValuedMap. * * @param mf A MapFactory which will be used to generate the underlying Map * @param cf A CollectionFactory which will be used to generate the Collections * in each mapping * @param treatCollectionsAsImmutable If true, forces this Map to create new a Collection every time a * new value is added to or deleted from the Collection a mapping. * @param map An existing map to use rather than initializing one with mf. If this is non-null it is * used to initialize the map rather than mf. */ private CollectionValuedMap(MapFactory<K, Collection<V>> mf, CollectionFactory<V> cf, boolean treatCollectionsAsImmutable, Map<K, Collection<V>> map) { if (cf == null) { throw new IllegalArgumentException(); } if (mf == null && map == null) { throw new IllegalArgumentException(); } this.mf = mf; this.cf = cf; this.treatCollectionsAsImmutable = treatCollectionsAsImmutable; this.emptyValue = cf.newEmptyCollection(); if (map != null) { this.map = map; } else { this.map = Collections.synchronizedMap(mf.newMap()); } }
@SuppressWarnings("Convert2streamapi") public Collection<V> allValues() { Collection<V> c = cf.newCollection(); for (Collection<V> c1 : map.values()) { c.addAll(c1); } return c; }
/** * Creates a new empty CollectionValuedMap which uses a HashMap as the * underlying Map, and HashSets as the Collections in each mapping. Does not * treat Collections as immutable. */ public CollectionValuedMap() { this(MapFactory.hashMapFactory(), CollectionFactory.hashSetFactory(), false); }
List<ValuedInterval<CoreMap,Integer>> processed = new ArrayList<>(); CollectionValuedMap<Integer, ValuedInterval<CoreMap,Integer>> unprocessed = new CollectionValuedMap<>(CollectionFactory.<ValuedInterval<CoreMap, Integer>>arrayListFactory()); for (ValuedInterval<CoreMap,Integer> v:timexList) { CoreMap timexAnn = v.getValue();
@Override public Collection<V> get(Object key) { // key could be not in original or in deltaMap // key could be not in original but in deltaMap // key could be in original but removed from deltaMap // key could be in original but mapped to something else in deltaMap Collection<V> deltaResult = deltaMap.get(key); if (deltaResult == null) { return originalMap.get(key); } if (deltaResult == removedValue) { return cf.newEmptyCollection(); } return deltaResult; }
/** * Adds the value to the collection given by map.get(key). A new collection is created using the supplied CollectionFactory. * */ public static <K, V, C extends Collection<V>> void putIntoValueCollection(Map<K, C> map, K key, V value, CollectionFactory<V> cf) { C c = map.get(key); if (c == null) { c = ErasureUtils.<C>uncheckedCast(cf.newCollection()); map.put(key, c); } c.add(value); }
/** * Adds the value to the HashSet given by map.get(key), creating a new HashMap if needed. * */ public static <K, V> void putIntoValueHashSet(Map<K, Set<V>> map, K key, V value) { CollectionFactory<V> factory = CollectionFactory.hashSetFactory(); putIntoValueCollection(map, key, value, factory); }
CollectionValuedMap<String, CoreMap> corefChainMap = new CollectionValuedMap<>(CollectionFactory.<CoreMap>arrayListFactory()); List<CoreMap> nerChunks = new ArrayList<>(); for (int i = 0; i < sentences.size(); i++) {
@Override public Collection<V> get(Object key) { // key could be not in original or in deltaMap // key could be not in original but in deltaMap // key could be in original but removed from deltaMap // key could be in original but mapped to something else in deltaMap Collection<V> deltaResult = deltaMap.get(key); if (deltaResult == null) { return originalMap.get(key); } if (deltaResult == removedValue) { return cf.newEmptyCollection(); } return deltaResult; }
/** * Creates a new Collection from the given CollectionFactory, and adds all of the Objects * returned by the given Iterator. */ public static <E> Collection<E> asCollection(Iterator<? extends E> iter, CollectionFactory<E> cf) { Collection<E> c = cf.newCollection(); return addAll(iter, c); }
/** * For internal debugging purposes only. */ public static void main(String[] args) { String[] test = {"a", "b", "c"}; List<String> l = Arrays.asList(test); System.out.println(asArrayList(l.iterator())); System.out.println(asHashSet(l.iterator())); System.out.println(asCollection(l.iterator(), CollectionFactory.<String>hashSetFactory())); ArrayList<String> al = new ArrayList<>(); al.add("d"); System.out.println(addAll(l.iterator(), al)); }
CollectionValuedMap<String, CoreMap> corefChainMap = new CollectionValuedMap<>(CollectionFactory.<CoreMap>arrayListFactory()); List<CoreMap> nerChunks = new ArrayList<>(); for (int i = 0; i < sentences.size(); i++) {
/** * Creates a new empty CollectionValuedMap. * @param mf a MapFactory which will be used to generate the underlying Map * @param cf a CollectionFactory which will be used to generate the Collections in each mapping * @param treatCollectionsAsImmutable if true, forces this Map to create new a Collection everytime * a new value is added to or deleted from the Collection a mapping. */ public CollectionValuedMap(MapFactory<K, Collection<V>> mf, CollectionFactory<V> cf, boolean treatCollectionsAsImmutable) { this.mf = mf; this.cf = cf; this.treatCollectionsAsImmutable = treatCollectionsAsImmutable; this.emptyValue = cf.newEmptyCollection(); if (mf != null) { map = Collections.synchronizedMap(mf.newMap()); } }
@Override public void add(K key, V value) { Collection<V> deltaC = deltaMap.get(key); if (deltaC == null) { deltaC = cf.newCollection(); Collection<V> originalC = originalMap.get(key); if (originalC != null) { deltaC.addAll(originalC); } deltaMap.put(key, deltaC); } deltaC.add(value); }
public static <K,V> CollectionValuedMap<Iterable<K>, V> collectionValuedTrieMap() { return new CollectionValuedMap<>( TrieMapUtils.<K, Collection<V>>trieMapFactory(), CollectionFactory.<V>hashSetFactory(), false); }
/** * Adds the value to the ArrayList given by map.get(key), creating a new ArrayList if needed. * */ public static <K, V> void putIntoValueArrayList(Map<K, List<V>> map, K key, V value) { CollectionFactory<V> factory = CollectionFactory.arrayListFactory(); putIntoValueCollection(map, key, value, factory); }