@Override public ImmutableSetMultimap<K, V> toImmutable() { MutableMap<K, ImmutableSet<V>> map = UnifiedMap.newMap(); this.map.forEachKeyValue((key, set) -> map.put(key, set.toImmutable())); return new ImmutableSetMultimapImpl<>(map); }
BigDecimal oldValue = map.get(key); BigDecimal valueToAdd = function.valueOf(each); map.put(key, oldValue == null ? valueToAdd : oldValue.add(valueToAdd)); }, (map1, map2) -> map2.forEachKeyValue((key, value) -> BigDecimal oldValue = map1.get(key); map1.put(key, oldValue == null ? value : oldValue.add(value)); }); return map1;
@Override public <VV> ImmutableMap<VV, V> groupByUniqueKey(Function<? super V, ? extends VV> function) { MutableMap<VV, V> map = this.delegate.groupByUniqueKey(function); return map.toImmutable(); }
public void writeExternal(ObjectOutput out) throws IOException { out.writeInt(this.map.size()); this.map.forEachKeyValue(new MultimapKeyValuesSerializingProcedure<>(out)); }
@Override public MutableMap<K, RichIterable<V>> toMap() { MutableMap<K, RichIterable<V>> result = (MutableMap<K, RichIterable<V>>) (MutableMap<?, ?>) this.map.newEmpty(); this.map.forEachKeyValue((key, collection) -> { MutableCollection<V> mutableCollection = collection.newEmpty(); mutableCollection.addAll(collection); result.put(key, mutableCollection); }); return result; }
@Override public <R extends Collection<V>> MutableMap<K, R> toMap(Function0<R> collectionFactory) { MutableMap<K, R> result = (MutableMap<K, R>) this.createMapWithKeyCount(this.map.size()); this.map.forEachKeyValue((key, collection) -> { R mutableCollection = collectionFactory.value(); mutableCollection.addAll(collection); result.put(key, mutableCollection); }); return result; }
@Override public <K, V> MutableMap<K, V> aggregateBy( Function<? super T, ? extends K> groupBy, Function0<? extends V> zeroValueFactory, Function2<? super V, ? super T, ? extends V> nonMutatingAggregator) { MutableMap<K, V> map = UnifiedMap.newMap(); this.each(each -> { K key = groupBy.valueOf(each); V value = map.getIfAbsentPut(key, zeroValueFactory); map.put(key, nonMutatingAggregator.value(value, each)); }); return map; } }
private MutableSet<String> doGetKnownItems(String user) { try (AutoLock al = knownItemsLock.autoReadLock()) { return knownItems.get(user); } }
/** * <p>Returns the elements as a MutableMap applying the keyFunction and valueFunction to each element.</p> * <p>Examples:</p> * {@code MutableMap<Integer, String> map1 = * Interval.oneTo(5).stream().collect(Collectors2.toMap(Functions.identity(), Object::toString));}<br> * {@code MutableMap<Integer, String> map2 = * Interval.oneTo(5).reduceInPlace(Collectors2.toMap(Functions.identity(), Object::toString));} * <p> * Equivalent to using @{@link RichIterable#toMap(Function, Function)} * </p> * {@code MutableMap<Integer, String> map = Interval.oneTo(5).toMap(Functions.identity(), Object::toString);} */ public static <T, K, V> Collector<T, ?, MutableMap<K, V>> toMap( Function<? super T, ? extends K> keyFunction, Function<? super T, ? extends V> valueFunction) { return Collector.of( Maps.mutable::empty, (map, each) -> map.put(keyFunction.valueOf(each), valueFunction.valueOf(each)), (r1, r2) -> { r1.putAll(r2); return r1; }, EMPTY_CHARACTERISTICS); }
@Override public void combineOne(SumByBigIntegerProcedure<T, V> thingToCombine) { if (this.result.isEmpty()) { this.result.putAll(thingToCombine.getResult()); } else { thingToCombine.getResult().forEachKeyValue((key, value) -> this.result.updateValue(key, Functions0.zeroBigInteger(), original -> original.add(value))); } } }
@Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { HashingStrategy<? super K> strategy = (HashingStrategy<? super K>) in.readObject(); int size = in.readInt(); MutableMap<K, V> deserializedMap = UnifiedMapWithHashingStrategy.newMap(strategy); for (int i = 0; i < size; i++) { deserializedMap.put((K) in.readObject(), (V) in.readObject()); } this.map = deserializedMap.toImmutable(); }
/** * Return a new map swapping key-value for value-key. * If the original map contains entries with the same value, the result mapping is undefined, * in that the last entry applied wins (the order of application is undefined). */ public static <K, V> MutableMap<V, K> reverseMapping(Map<K, V> map) { MutableMap<V, K> reverseMap = UnifiedMap.newMap(map.size()); MapIterate.forEachKeyValue(map, (sourceKey, sourceValue) -> reverseMap.put(sourceValue, sourceKey)); return reverseMap; }
@Override public <K2, V2> ImmutableMap<K2, V2> collect(Function2<? super K, ? super V, Pair<K2, V2>> function) { MutableMap<K2, V2> collectedMap = UnifiedMap.newMap(this.size()); this.forEachKeyValue((key, value) -> collectedMap.add(function.value(key, value))); return collectedMap.toImmutable(); }
/** * Get and return the value in the Map at the specified key, or if there is no value at the key, return the result * of evaluating the specified {@link Function0}, and put that value in the map at the specified key. * <p> * This method handles the {@code null}-value-at-key case correctly. */ public static <K, V> V getIfAbsentPut(Map<K, V> map, K key, Function0<? extends V> instanceBlock) { if (map instanceof MutableMap) { return ((MutableMap<K, V>) map).getIfAbsentPut(key, instanceBlock); } V result = map.get(key); if (MapIterate.isAbsent(result, map, key)) { result = instanceBlock.value(); map.put(key, result); } return result; }
protected Main() { // use the hashing strategy to allow commands of any case to be handled MutableMap<String, Procedure<String[]>> commandMap = HashingStrategyMaps.mutable.of(HashingStrategies.fromFunction(new Function<String, String>() { @Override public String valueOf(String s) { return s.toLowerCase(); } })); commandMap.putAll(getCommandMap().toMap()); this.commandMap = commandMap.toImmutable(); }
@Override public Set<Entry<K, V>> entrySet() { synchronized (this.lock) { return SynchronizedMutableSet.of(this.getDelegate().entrySet(), this.lock); } }
@Benchmark public long test10_UsingEclipseMap() throws IOException { final long[] i = {0}; mutableMap.forEachKeyValue((key, value) -> { i[0] += key + value; }); return i[0]; }