Class SynchronizedMutableMap<K,​V>

    • Constructor Detail

      • SynchronizedMutableMap

        public SynchronizedMutableMap​(MutableMap<K,​V> newMap)
      • SynchronizedMutableMap

        public SynchronizedMutableMap​(MutableMap<K,​V> newMap,
                                      java.lang.Object newLock)
    • Method Detail

      • of

        public static <K,​V,​M extends java.util.Map<K,​V>> SynchronizedMutableMap<K,​V> of​(M map)
        This method will take a MutableMap and wrap it directly in a SynchronizedMutableMap. It will take any other non-Eclipse-Collections map and first adapt it will a MapAdapter, and then return a SynchronizedMutableMap that wraps the adapter.
      • of

        public static <K,​V,​M extends java.util.Map<K,​V>> SynchronizedMutableMap<K,​V> of​(M map,
                                                                                                                java.lang.Object lock)
        This method will take a MutableMap and wrap it directly in a SynchronizedMutableMap. It will take any other non-Eclipse-Collections map and first adapt it will a MapAdapter, and then return a SynchronizedMutableMap that wraps the adapter. Additionally, a developer specifies which lock to use with the collection.
      • withKeyValue

        public MutableMap<K,​V> withKeyValue​(K key,
                                                  V value)
        Description copied from interface: MutableMapIterable
        This method allows mutable, fixed size, and immutable maps the ability to add elements to their existing elements. In order to support fixed size maps, a new instance of a map would have to be returned including the keys and values of the original plus the additional key and value. In the case of mutable maps, the original map is modified and then returned. In order to use this method properly with mutable and fixed size maps the following approach must be taken:
         map = map.withKeyValue("new key", "new value");
         
        In the case of FixedSizeMap, a new instance will be returned by withKeyValue, and any variables that previously referenced the original map will need to be redirected to reference the new instance. In the case of a FastMap or UnifiedMap, you will be replacing the reference to map with map, since FastMap and UnifiedMap will both return "this" after calling put on themselves.
        Specified by:
        withKeyValue in interface MutableMap<K,​V>
        Specified by:
        withKeyValue in interface MutableMapIterable<K,​V>
        See Also:
        Map.put(Object, Object)
      • withAllKeyValues

        public MutableMap<K,​V> withAllKeyValues​(java.lang.Iterable<? extends Pair<? extends K,​? extends V>> keyValues)
        Description copied from interface: MutableMapIterable
        This method allows mutable, fixed size, and immutable maps the ability to add elements to their existing elements. In order to support fixed size maps, a new instance of a map would have to be returned including the keys and values of the original plus all the additional keys and values. In the case of mutable maps, the original map is modified and then returned. In order to use this method properly with mutable and fixed size maps the following approach must be taken:
         map = map.withAllKeyValues(FastList.newListWith(PairImpl.of("new key", "new value")));
         
        In the case of FixedSizeMap, a new instance will be returned by withAllKeyValues, and any variables that previously referenced the original map will need to be redirected to reference the new instance. In the case of a FastMap or UnifiedMap, you will be replacing the reference to map with map, since FastMap and UnifiedMap will both return "this" after calling put on themselves.
        Specified by:
        withAllKeyValues in interface MutableMap<K,​V>
        Specified by:
        withAllKeyValues in interface MutableMapIterable<K,​V>
        See Also:
        Map.put(Object, Object)
      • withoutKey

        public MutableMap<K,​V> withoutKey​(K key)
        Description copied from interface: MutableMapIterable
        This method allows mutable, fixed size, and immutable maps the ability to remove elements from their existing elements. In order to support fixed size maps, a new instance of a map would have to be returned including the keys and values of the original minus the key and value to be removed. In the case of mutable maps, the original map is modified and then returned. In order to use this method properly with mutable and fixed size maps the following approach must be taken:
         map = map.withoutKey("key");
         
        In the case of FixedSizeMap, a new instance will be returned by withoutKey, and any variables that previously referenced the original map will need to be redirected to reference the new instance. In the case of a FastMap or UnifiedMap, you will be replacing the reference to map with map, since FastMap and UnifiedMap will both return "this" after calling remove on themselves.
        Specified by:
        withoutKey in interface MutableMap<K,​V>
        Specified by:
        withoutKey in interface MutableMapIterable<K,​V>
        See Also:
        Map.remove(Object)
      • withoutAllKeys

        public MutableMap<K,​V> withoutAllKeys​(java.lang.Iterable<? extends K> keys)
        Description copied from interface: MutableMapIterable
        This method allows mutable, fixed size, and immutable maps the ability to remove elements from their existing elements. In order to support fixed size maps, a new instance of a map would have to be returned including the keys and values of the original minus all the keys and values to be removed. In the case of mutable maps, the original map is modified and then returned. In order to use this method properly with mutable and fixed size maps the following approach must be taken:
         map = map.withoutAllKeys(FastList.newListWith("key1", "key2"));
         
        In the case of FixedSizeMap, a new instance will be returned by withoutAllKeys, and any variables that previously referenced the original map will need to be redirected to reference the new instance. In the case of a FastMap or UnifiedMap, you will be replacing the reference to map with map, since FastMap and UnifiedMap will both return "this" after calling remove on themselves.
        Specified by:
        withoutAllKeys in interface MutableMap<K,​V>
        Specified by:
        withoutAllKeys in interface MutableMapIterable<K,​V>
        See Also:
        Map.remove(Object)
      • writeReplace

        protected java.lang.Object writeReplace()
      • collectKeysAndValues

        public <E> MutableMap<K,​V> collectKeysAndValues​(java.lang.Iterable<E> iterable,
                                                              Function<? super E,​? extends K> keyFunction,
                                                              Function<? super E,​? extends V> function)
        Description copied from interface: MutableMap
        Adds all the entries derived from iterable to this. The key and value for each entry is determined by applying the keyFunction and valueFunction to each item in collection. Any entry in map that has the same key as an entry in this will have its value replaced by that in map.
        Specified by:
        collectKeysAndValues in interface MutableMap<K,​V>
      • select

        public MutableMap<K,​V> select​(Predicate2<? super K,​? super V> predicate)
        Description copied from interface: MapIterable
        For each key and value of the map the predicate is evaluated, if the result of the evaluation is true, that key and value are returned in a new map.
         MapIterable<City, Person> selected =
             peopleByCity.select((city, person) -> city.getName().equals("Anytown") && person.getLastName().equals("Smith"));
         
        Specified by:
        select in interface MapIterable<K,​V>
        Specified by:
        select in interface MutableMap<K,​V>
        Specified by:
        select in interface MutableMapIterable<K,​V>
        Specified by:
        select in interface UnsortedMapIterable<K,​V>
      • reject

        public MutableMap<K,​V> reject​(Predicate2<? super K,​? super V> predicate)
        Description copied from interface: MapIterable
        For each key and value of the map the predicate is evaluated, if the result of the evaluation is false, that key and value are returned in a new map.
         MapIterable<City, Person> rejected =
             peopleByCity.reject((city, person) -> city.getName().equals("Anytown") && person.getLastName().equals("Smith"));
         
        Specified by:
        reject in interface MapIterable<K,​V>
        Specified by:
        reject in interface MutableMap<K,​V>
        Specified by:
        reject in interface MutableMapIterable<K,​V>
        Specified by:
        reject in interface UnsortedMapIterable<K,​V>
      • collect

        public <K2,​V2> MutableMap<K2,​V2> collect​(Function2<? super K,​? super V,​Pair<K2,​V2>> pairFunction)
        Description copied from interface: MapIterable
        For each key and value of the map the function is evaluated. The results of these evaluations are returned in a new map. The map returned will use the values projected from the function rather than the original values.
         MapIterable<String, String> collected =
             peopleByCity.collect((City city, Person person) -> Pair.of(city.getCountry(), person.getAddress().getCity()));
         
        Specified by:
        collect in interface MapIterable<K,​V>
        Specified by:
        collect in interface MutableMap<K,​V>
        Specified by:
        collect in interface MutableMapIterable<K,​V>
        Specified by:
        collect in interface UnsortedMapIterable<K,​V>
      • collectValues

        public <R> MutableMap<K,​R> collectValues​(Function2<? super K,​? super V,​? extends R> function)
        Description copied from interface: MapIterable
        For each key and value of the map the function is evaluated. The results of these evaluations are returned in a new map. The map returned will use the values projected from the function rather than the original values.
         MapIterable<City, String> collected =
             peopleByCity.collectValues((City city, Person person) -> person.getFirstName() + " " + person.getLastName());
         
        Specified by:
        collectValues in interface MapIterable<K,​V>
        Specified by:
        collectValues in interface MutableMap<K,​V>
        Specified by:
        collectValues in interface MutableMapIterable<K,​V>
        Specified by:
        collectValues in interface UnsortedMapIterable<K,​V>
      • collect

        public <A> MutableBag<A> collect​(Function<? super V,​? extends A> function)
        Description copied from interface: RichIterable
        Returns a new collection with the results of applying the specified function on each element of the source collection. This method is also commonly called transform or map.

        Example using a Java 8 lambda expression:

         RichIterable<String> names =
             people.collect(person -> person.getFirstName() + " " + person.getLastName());
         
        Specified by:
        collect in interface MutableMap<K,​V>
        Specified by:
        collect in interface RichIterable<K>
        Specified by:
        collect in interface UnsortedMapIterable<K,​V>
        Overrides:
        collect in class AbstractSynchronizedRichIterable<V>
      • collectIf

        public <A> MutableBag<A> collectIf​(Predicate<? super V> predicate,
                                           Function<? super V,​? extends A> function)
        Description copied from interface: RichIterable
        Returns a new collection with the results of applying the specified function on each element of the source collection, but only for those elements which return true upon evaluation of the predicate. This is the optimized equivalent of calling iterable.select(predicate).collect(function).

        Example using a Java 8 lambda and method reference:

         RichIterable<String> strings = Lists.mutable.with(1, 2, 3).collectIf(e -> e != null, Object::toString);
         

        Example using Predicates factory:

         RichIterable<String> strings = Lists.mutable.with(1, 2, 3).collectIf(Predicates.notNull(), Functions.getToString());
         
        Specified by:
        collectIf in interface MutableMap<K,​V>
        Specified by:
        collectIf in interface RichIterable<K>
        Specified by:
        collectIf in interface UnsortedMapIterable<K,​V>
        Overrides:
        collectIf in class AbstractSynchronizedRichIterable<V>
      • flatCollect

        public <A> MutableBag<A> flatCollect​(Function<? super V,​? extends java.lang.Iterable<A>> function)
        Description copied from interface: RichIterable
        flatCollect is a special case of RichIterable.collect(Function). With collect, when the Function returns a collection, the result is a collection of collections. flatCollect outputs a single "flattened" collection instead. This method is commonly called flatMap.

        Consider the following example where we have a Person class, and each Person has a list of Address objects. Take the following Function:

         Function<Person, List<Address>> addressFunction = Person::getAddresses;
         RichIterable<Person> people = ...;
         
        Using collect returns a collection of collections of addresses.
         RichIterable<List<Address>> addresses = people.collect(addressFunction);
         
        Using flatCollect returns a single flattened list of addresses.
         RichIterable<Address> addresses = people.flatCollect(addressFunction);
         
        Specified by:
        flatCollect in interface MutableMap<K,​V>
        Specified by:
        flatCollect in interface RichIterable<K>
        Specified by:
        flatCollect in interface UnsortedMapIterable<K,​V>
        Overrides:
        flatCollect in class AbstractSynchronizedRichIterable<V>
        Parameters:
        function - The Function to apply
        Returns:
        a new flattened collection produced by applying the given function
      • aggregateBy

        public <KK,​VV> MutableMap<KK,​VV> aggregateBy​(Function<? super V,​? extends KK> groupBy,
                                                                 Function0<? extends VV> zeroValueFactory,
                                                                 Function2<? super VV,​? super V,​? extends VV> nonMutatingAggregator)
        Description copied from interface: RichIterable
        Applies an aggregate function over the iterable grouping results into a map based on the specific groupBy function. Aggregate results are allowed to be immutable as they will be replaced in place in the map. A second function specifies the initial "zero" aggregate value to work with (i.e. Integer.valueOf(0)).
        Specified by:
        aggregateBy in interface MutableMap<K,​V>
        Specified by:
        aggregateBy in interface MutableMapIterable<K,​V>
        Specified by:
        aggregateBy in interface RichIterable<K>
        Overrides:
        aggregateBy in class AbstractSynchronizedMapIterable<K,​V>
        Since:
        11.0
      • aggregateBy

        public <K1,​V1,​V2> MutableMap<K1,​V2> aggregateBy​(Function<? super K,​? extends K1> keyFunction,
                                                                          Function<? super V,​? extends V1> valueFunction,
                                                                          Function0<? extends V2> zeroValueFactory,
                                                                          Function2<? super V2,​? super V1,​? extends V2> nonMutatingAggregator)
        Description copied from interface: MapIterable
        Applies an aggregate function over the map grouping results into a map based on the specific key and value groupBy functions. Aggregate results are allowed to be immutable as they will be replaced in place in the map. A second function specifies the initial "zero" aggregate value to work with.
         MapIterable<String, Interval> map = Maps.mutable.with("oneToFive", Interval.fromTo(1, 5), "sixToNine", Interval.fromTo(6, 9));
        
         MapIterable<String, Long> result = map.aggregateBy(
                 eachKey -> {
                     return eachKey.equals("oneToFive")  ? "lessThanSix" : "greaterOrEqualsToSix";
                 },
                 each -> each.sumOfInt(Integer::intValue),
                 () -> 0L,
                 (argument1, argument2) -> argument1 + argument2);
        
         MapIterable<String, Long> expected =
                 Maps.mutable.with("lessThanSix", Interval.fromTo(1, 5).sumOfInt(Integer::intValue),
                         "greaterOrEqualsToSix", Interval.fromTo(6, 9).sumOfInt(Integer::intValue));
         Assert.assertEquals(expected, result);
         
        Specified by:
        aggregateBy in interface MapIterable<K,​V>
        Specified by:
        aggregateBy in interface MutableMap<K,​V>
        Specified by:
        aggregateBy in interface MutableMapIterable<K,​V>
        Overrides:
        aggregateBy in class AbstractSynchronizedMapIterable<K,​V>
        Since:
        11.0
      • zip

        @Deprecated
        public <S> MutableBag<Pair<V,​S>> zip​(java.lang.Iterable<S> that)
        Deprecated.
        in 6.0. Use OrderedIterable.zip(Iterable) instead.
        Description copied from interface: RichIterable
        Returns a RichIterable formed from this RichIterable and another RichIterable by combining corresponding elements in pairs. If one of the two RichIterables is longer than the other, its remaining elements are ignored.
        Specified by:
        zip in interface MutableMap<K,​V>
        Specified by:
        zip in interface MutableMapIterable<K,​V>
        Specified by:
        zip in interface RichIterable<K>
        Specified by:
        zip in interface UnsortedMapIterable<K,​V>
        Overrides:
        zip in class AbstractSynchronizedMapIterable<K,​V>
        Type Parameters:
        S - the type of the second half of the returned pairs
        Parameters:
        that - The RichIterable providing the second half of each result pair
        Returns:
        A new RichIterable containing pairs consisting of corresponding elements of this RichIterable and that. The length of the returned RichIterable is the minimum of the lengths of this RichIterable and that.
      • flip

        public MutableSetMultimap<V,​K> flip()
        Description copied from interface: MapIterable
        Given a map from Domain -> Range return a multimap from Range -> Domain. We chose the name 'flip' rather than 'invert' or 'transpose' since this method does not have the property of applying twice returns the original.

        Since the keys in the input are unique, the values in the output are unique, so the return type should be a SetMultimap. However, since SetMultimap and SortedSetMultimap don't inherit from one another, SetMultimap here does not allow SortedMapIterable to have a SortedSetMultimap return. Thus, we compromise and call this Multimap, even though all implementations will be a SetMultimap or SortedSetMultimap.

        Specified by:
        flip in interface MapIterable<K,​V>
        Specified by:
        flip in interface MutableMap<K,​V>
        Specified by:
        flip in interface MutableMapIterable<K,​V>
        Specified by:
        flip in interface UnsortedMapIterable<K,​V>
      • keySet

        public java.util.Set<K> keySet()
        Specified by:
        keySet in interface java.util.Map<K,​V>
      • values

        public java.util.Collection<V> values()
        Specified by:
        values in interface java.util.Map<K,​V>
      • entrySet

        public java.util.Set<java.util.Map.Entry<K,​V>> entrySet()
        Specified by:
        entrySet in interface java.util.Map<K,​V>
      • asUnmodifiable

        public MutableMap<K,​V> asUnmodifiable()
        Description copied from interface: MutableMapIterable
        Returns an unmodifiable view of this map. This is the equivalent of using Collections.unmodifiableMap(this) only with a return type that supports the full iteration protocols available on MutableMapIterable. Methods which would mutate the underlying map will throw UnsupportedOperationExceptions.
        Specified by:
        asUnmodifiable in interface MutableMap<K,​V>
        Specified by:
        asUnmodifiable in interface MutableMapIterable<K,​V>
        Returns:
        an unmodifiable view of this map.
        See Also:
        Collections.unmodifiableMap(Map)
      • asSynchronized

        public MutableMap<K,​V> asSynchronized()
        Description copied from interface: MutableMapIterable
        Returns a synchronized wrapper backed by this map. This is the equivalent of calling Collections.synchronizedMap(this) only with the more feature rich return type of MutableMapIterable.

        The preferred way of iterating over a synchronized map is to use the forEachKey(), forEachValue() and forEachKeyValue() methods which are properly synchronized internally.

          MutableMap synchedMap = map.asSynchronized();
        
          synchedMap.forEachKey(key -> ... );
          synchedMap.forEachValue(value -> ... );
          synchedMap.forEachKeyValue((key, value) -> ... );
         

        If you want to iterate imperatively over the keySet(), values(), or entrySet(), you will need to protect the iteration by wrapping the code in a synchronized block on the map.

        Specified by:
        asSynchronized in interface MutableMap<K,​V>
        Specified by:
        asSynchronized in interface MutableMapIterable<K,​V>
        See Also:
        Collections.synchronizedMap(Map)