Class AbstractSynchronizedMapIterable<K,​V>

    • Constructor Detail

      • AbstractSynchronizedMapIterable

        protected AbstractSynchronizedMapIterable​(MutableMapIterable<K,​V> delegate)
      • AbstractSynchronizedMapIterable

        protected AbstractSynchronizedMapIterable​(MutableMapIterable<K,​V> delegate,
                                                  java.lang.Object lock)
    • Method Detail

      • get

        public V get​(java.lang.Object key)
        Specified by:
        get in interface java.util.Map<K,​V>
        Specified by:
        get in interface MapIterable<K,​V>
        See Also:
        Map.get(Object)
      • getIfAbsent

        public V getIfAbsent​(K key,
                             Function0<? extends V> function)
        Description copied from interface: MapIterable
        Return the value in the Map that corresponds to the specified key, or if there is no value at the key, return the result of evaluating the specified Function0.
        Specified by:
        getIfAbsent in interface MapIterable<K,​V>
      • getIfAbsentValue

        public V getIfAbsentValue​(K key,
                                  V value)
        Description copied from interface: MapIterable
        Return the value in the Map that corresponds to the specified key, or if there is no value at the key, return value.
        Specified by:
        getIfAbsentValue in interface MapIterable<K,​V>
      • getIfAbsentWith

        public <P> V getIfAbsentWith​(K key,
                                     Function<? super P,​? extends V> function,
                                     P parameter)
        Description copied from interface: MapIterable
        Return the value in the Map that corresponds to the specified key, or if there is no value at the key, return the result of evaluating the specified function and parameter.
        Specified by:
        getIfAbsentWith in interface MapIterable<K,​V>
      • ifPresentApply

        public <A> A ifPresentApply​(K key,
                                    Function<? super V,​? extends A> function)
        Description copied from interface: MapIterable
        If there is a value in the Map that corresponds to the specified key return the result of applying the specified Function on the value, otherwise return null.
        Specified by:
        ifPresentApply in interface MapIterable<K,​V>
      • containsKey

        public boolean containsKey​(java.lang.Object key)
        Specified by:
        containsKey in interface java.util.Map<K,​V>
        Specified by:
        containsKey in interface MapIterable<K,​V>
        See Also:
        Map.containsKey(Object)
      • containsValue

        public boolean containsValue​(java.lang.Object value)
        Specified by:
        containsValue in interface java.util.Map<K,​V>
        Specified by:
        containsValue in interface MapIterable<K,​V>
        See Also:
        Map.containsValue(Object)
      • forEachValue

        public void forEachValue​(Procedure<? super V> procedure)
        Description copied from interface: MapIterable
        Calls the procedure with each value of the map.
             Set<String> result = UnifiedSet.newSet();
             MutableMap<Integer, String> map = this.newMapWithKeysValues(1, "One", 2, "Two", 3, "Three", 4, "Four");
             map.forEachValue(new CollectionAddProcedure<String>(result));
             Verify.assertSetsEqual(UnifiedSet.newSetWith("One", "Two", "Three", "Four"), result);
         
        Specified by:
        forEachValue in interface MapIterable<K,​V>
      • forEachKey

        public void forEachKey​(Procedure<? super K> procedure)
        Description copied from interface: MapIterable
        Calls the procedure with each key of the map.
             final Collection<Integer> result = new ArrayList<Integer>();
             MutableMap<Integer, String> map = this.newMapWithKeysValues(1, "1", 2, "2", 3, "3");
             map.forEachKey(new CollectionAddProcedure<Integer>(result));
             Verify.assertContainsAll(result, 1, 2, 3);
         
        Specified by:
        forEachKey in interface MapIterable<K,​V>
      • forEachKeyValue

        public void forEachKeyValue​(Procedure2<? super K,​? super V> procedure2)
        Description copied from interface: MapIterable
        Calls the procedure with each key-value pair of the map.
             final Collection<String> collection = new ArrayList<String>();
             MutableMap<Integer, String> map = this.newMapWithKeysValues(1, "One", 2, "Two", 3, "Three");
             map.forEachKeyValue((Integer key, String value) -> collection.add(String.valueOf(key) + value));
             Verify.assertContainsAll(collection, "1One", "2Two", "3Three");
         
        Specified by:
        forEachKeyValue in interface MapIterable<K,​V>
      • detect

        public Pair<K,​V> detect​(Predicate2<? super K,​? super V> predicate)
        Description copied from interface: MapIterable
        Return the first key and value of the map for which the predicate evaluates to true when they are given as arguments. The predicate will only be evaluated until such pair is found or until all the keys and values of the map have been used as arguments. That is, there may be keys and values of the map that are never used as arguments to the predicate. The result is null if predicate does not evaluate to true for any key/value combination.
         Pair<City, Person> detected =
             peopleByCity.detect((City city, Person person) -> city.getName().equals("Anytown") && person.getLastName().equals("Smith"));
         
        Specified by:
        detect in interface MapIterable<K,​V>
      • detectOptional

        public java.util.Optional<Pair<K,​V>> detectOptional​(Predicate2<? super K,​? super V> predicate)
        Description copied from interface: MapIterable
        Return the first key and value of the map as an Optional for which the predicate evaluates to true when they are given as arguments. The predicate will only be evaluated until such pair is found or until all the keys and values of the map have been used as arguments. That is, there may be keys and values of the map that are never used as arguments to the predicate.
         Optional<Pair<City, Person>> detected =
             peopleByCity.detectOptional((city, person)
                  -> city.getName().equals("Anytown") && person.getLastName().equals("Smith"));
         
        Specified by:
        detectOptional in interface MapIterable<K,​V>
      • getIfAbsentPut

        public V getIfAbsentPut​(K key,
                                Function0<? extends V> function)
        Description copied from interface: MutableMapIterable
        Get and return the value in the Map at the specified key. Alternatively, if there is no value in the map at the key, return the result of evaluating the specified Function0, and put that value in the map at the specified key.
        Specified by:
        getIfAbsentPut in interface MutableMapIterable<K,​V>
      • getIfAbsentPut

        public V getIfAbsentPut​(K key,
                                V value)
        Description copied from interface: MutableMapIterable
        Get and return the value in the Map at the specified key. Alternatively, if there is no value in the map at the key, return the specified value, and put that value in the map at the specified key.
        Specified by:
        getIfAbsentPut in interface MutableMapIterable<K,​V>
      • getIfAbsentPutWithKey

        public V getIfAbsentPutWithKey​(K key,
                                       Function<? super K,​? extends V> function)
        Description copied from interface: MutableMapIterable
        Get and return the value in the Map at the specified key. Alternatively, if there is no value in the map for that key return the result of evaluating the specified Function using the specified key, and put that value in the map at the specified key.
        Specified by:
        getIfAbsentPutWithKey in interface MutableMapIterable<K,​V>
      • getIfAbsentPutWith

        public <P> V getIfAbsentPutWith​(K key,
                                        Function<? super P,​? extends V> function,
                                        P parameter)
        Description copied from interface: MutableMapIterable
        Get and return the value in the Map at the specified key. Alternatively, if there is no value in the map for that key return the result of evaluating the specified Function using the specified parameter, and put that value in the map at the specified key.
        Specified by:
        getIfAbsentPutWith in interface MutableMapIterable<K,​V>
      • put

        public V put​(K key,
                     V value)
        Specified by:
        put in interface java.util.Map<K,​V>
      • remove

        public V remove​(java.lang.Object key)
        Specified by:
        remove in interface java.util.Map<K,​V>
      • removeKey

        public V removeKey​(K key)
        Description copied from interface: MutableMapIterable
        Remove an entry from the map at the specified key.
        Specified by:
        removeKey in interface MutableMapIterable<K,​V>
        Returns:
        The value removed from entry at key, or null if not found.
        See Also:
        Map.remove(Object)
      • removeAllKeys

        public boolean removeAllKeys​(java.util.Set<? extends K> keys)
        Description copied from interface: MutableMapIterable
        Remove entries from the map at the specified keys.
        Specified by:
        removeAllKeys in interface MutableMapIterable<K,​V>
        Returns:
        true if this map changed as a result of the call
      • removeIf

        public boolean removeIf​(Predicate2<? super K,​? super V> predicate)
        Description copied from interface: MutableMapIterable
        Remove an entry from the map if the predicate evaluates to true.
        Specified by:
        removeIf in interface MutableMapIterable<K,​V>
        Returns:
        true if any entry is removed.
      • putAll

        public void putAll​(java.util.Map<? extends K,​? extends V> map)
        Specified by:
        putAll in interface java.util.Map<K,​V>
      • clear

        public void clear()
        Specified by:
        clear in interface java.util.Map<K,​V>
      • putPair

        public V putPair​(Pair<? extends K,​? extends V> keyValuePair)
        Description copied from interface: MutableMapIterable
        This method allows mutable map the ability to add an element in the form of Pair<? extends K, ? extends V>.
        Specified by:
        putPair in interface MutableMapIterable<K,​V>
        See Also:
        Map.put(Object, Object)
      • add

        public V add​(Pair<? extends K,​? extends V> keyValuePair)
        Description copied from interface: MutableMapIterable
        This method allows mutable map the ability to add an element in the form of Pair<? extends K, ? extends V>.
        Specified by:
        add in interface MutableMapIterable<K,​V>
        Returns:
        previous value in the map for the key, or null if no value exists for the key.
        See Also:
        Map.put(Object, Object)
      • updateValue

        public V updateValue​(K key,
                             Function0<? extends V> factory,
                             Function<? super V,​? extends V> function)
        Description copied from interface: MutableMapIterable
        Looks up the value associated with key, applies the function to it, and replaces the value. If there is no value associated with key, starts it off with a value supplied by factory.
        Specified by:
        updateValue in interface MutableMapIterable<K,​V>
      • aggregateBy

        public <KK,​VV> MutableMapIterable<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 MutableMapIterable<K,​V>
        Specified by:
        aggregateBy in interface RichIterable<K>
        Overrides:
        aggregateBy in class AbstractSynchronizedRichIterable<V>
      • aggregateBy

        public <K1,​V1,​V2> MutableMapIterable<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 MutableMapIterable<K,​V>
      • keysView

        public RichIterable<K> keysView()
        Description copied from interface: MapIterable
        Returns an unmodifiable lazy iterable wrapped around the keySet for the map.
        Specified by:
        keysView in interface MapIterable<K,​V>
      • valuesView

        public RichIterable<V> valuesView()
        Description copied from interface: MapIterable
        Returns an unmodifiable lazy iterable wrapped around the values for the map.
        Specified by:
        valuesView in interface MapIterable<K,​V>
      • keyValuesView

        public RichIterable<Pair<K,​V>> keyValuesView()
        Description copied from interface: MapIterable
        Returns an unmodifiable lazy iterable of key/value pairs wrapped around the entrySet for the map.
        Specified by:
        keyValuesView in interface MapIterable<K,​V>
      • groupBy

        public <V1> MutableMultimap<V1,​V> groupBy​(Function<? super V,​? extends V1> function)
        Description copied from interface: RichIterable
        For each element of the iterable, the function is evaluated and the results of these evaluations are collected into a new multimap, where the transformed value is the key and the original values are added to the same (or similar) species of collection as the source iterable.

        Example using a Java 8 method reference:

         Multimap<String, Person> peopleByLastName =
             people.groupBy(Person::getLastName);
         

        Example using an anonymous inner class:

         Multimap<String, Person> peopleByLastName =
             people.groupBy(new Function<Person, String>()
             {
                 public String valueOf(Person person)
                 {
                     return person.getLastName();
                 }
             });
         
        Specified by:
        groupBy in interface MutableMapIterable<K,​V>
        Specified by:
        groupBy in interface RichIterable<K>
        Overrides:
        groupBy in class AbstractSynchronizedRichIterable<V>
        Since:
        9.0
      • zip

        public <S> MutableCollection<Pair<V,​S>> zip​(java.lang.Iterable<S> that)
        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 MutableMapIterable<K,​V>
        Specified by:
        zip in interface RichIterable<K>
        Overrides:
        zip in class AbstractSynchronizedRichIterable<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.
        Since:
        9.0
      • select

        public MutableCollection<V> select​(Predicate<? super V> predicate)
        Description copied from interface: RichIterable
        Returns all elements of the source collection that return true when evaluating the predicate. This method is also commonly called filter.

        Example using a Java 8 lambda expression:

         RichIterable<Person> selected =
             people.select(person -> person.getAddress().getCity().equals("London"));
         

        Example using an anonymous inner class:

         RichIterable<Person> selected =
             people.select(new Predicate<Person>()
             {
                 public boolean accept(Person person)
                 {
                     return person.getAddress().getCity().equals("London");
                 }
             });
         
        Specified by:
        select in interface MutableMapIterable<K,​V>
        Specified by:
        select in interface RichIterable<K>
        Overrides:
        select in class AbstractSynchronizedRichIterable<V>
        Since:
        9.0
      • selectWith

        public <P> MutableCollection<V> selectWith​(Predicate2<? super V,​? super P> predicate,
                                                   P parameter)
        Description copied from interface: RichIterable
        Similar to RichIterable.select(Predicate), except with an evaluation parameter for the second generic argument in Predicate2.

        E.g. return a Collection of Person elements where the person has an age greater than or equal to 18 years

        Example using a Java 8 lambda expression:

         RichIterable<Person> selected =
             people.selectWith((Person person, Integer age) -> person.getAge()>= age, Integer.valueOf(18));
         

        Example using an anonymous inner class:

         RichIterable<Person> selected =
             people.selectWith(new Predicate2<Person, Integer>()
             {
                 public boolean accept(Person person, Integer age)
                 {
                     return person.getAge()>= age;
                 }
             }, Integer.valueOf(18));
         
        Specified by:
        selectWith in interface MutableMapIterable<K,​V>
        Specified by:
        selectWith in interface RichIterable<K>
        Overrides:
        selectWith in class AbstractSynchronizedRichIterable<V>
        Parameters:
        predicate - a Predicate2 to use as the select criteria
        parameter - a parameter to pass in for evaluation of the second argument P in predicate
        Since:
        9.0
        See Also:
        RichIterable.select(Predicate)
      • rejectWith

        public <P> MutableCollection<V> rejectWith​(Predicate2<? super V,​? super P> predicate,
                                                   P parameter)
        Description copied from interface: RichIterable
        Similar to RichIterable.reject(Predicate), except with an evaluation parameter for the second generic argument in Predicate2.

        E.g. return a Collection of Person elements where the person has an age greater than or equal to 18 years

        Example using a Java 8 lambda expression:

         RichIterable<Person> rejected =
             people.rejectWith((Person person, Integer age) -> person.getAge() < age, Integer.valueOf(18));
         

        Example using an anonymous inner class:

         MutableList<Person> rejected =
             people.rejectWith(new Predicate2<Person, Integer>()
             {
                 public boolean accept(Person person, Integer age)
                 {
                     return person.getAge() < age;
                 }
             }, Integer.valueOf(18));
         
        Specified by:
        rejectWith in interface MutableMapIterable<K,​V>
        Specified by:
        rejectWith in interface RichIterable<K>
        Overrides:
        rejectWith in class AbstractSynchronizedRichIterable<V>
        Parameters:
        predicate - a Predicate2 to use as the select criteria
        parameter - a parameter to pass in for evaluation of the second argument P in predicate
        Since:
        9.0
        See Also:
        RichIterable.select(Predicate)
      • partition

        public PartitionMutableCollection<V> partition​(Predicate<? super V> predicate)
        Description copied from interface: RichIterable
        Filters a collection into a PartitionedIterable based on the evaluation of the predicate.

        Example using a Java 8 lambda expression:

         PartitionIterable<Person> newYorkersAndNonNewYorkers =
             people.partition(person -> person.getAddress().getState().getName().equals("New York"));
         

        Example using an anonymous inner class:

         PartitionIterable<Person> newYorkersAndNonNewYorkers =
             people.partition(new Predicate<Person>()
             {
                 public boolean accept(Person person)
                 {
                     return person.getAddress().getState().getName().equals("New York");
                 }
             });
         
        Specified by:
        partition in interface MutableMapIterable<K,​V>
        Specified by:
        partition in interface RichIterable<K>
        Overrides:
        partition in class AbstractSynchronizedRichIterable<V>
        Since:
        9.0
      • reject

        public MutableCollection<V> reject​(Predicate<? super V> predicate)
        Description copied from interface: RichIterable
        Returns all elements of the source collection that return false when evaluating of the predicate. This method is also sometimes called filterNot and is the equivalent of calling iterable.select(Predicates.not(predicate)).

        Example using a Java 8 lambda expression:

         RichIterable<Person> rejected =
             people.reject(person -> person.person.getLastName().equals("Smith"));
         

        Example using an anonymous inner class:

         RichIterable<Person> rejected =
             people.reject(new Predicate<Person>()
             {
                 public boolean accept(Person person)
                 {
                     return person.person.getLastName().equals("Smith");
                 }
             });
         
        Specified by:
        reject in interface MutableMapIterable<K,​V>
        Specified by:
        reject in interface RichIterable<K>
        Overrides:
        reject in class AbstractSynchronizedRichIterable<V>
        Parameters:
        predicate - a Predicate to use as the reject criteria
        Returns:
        a RichIterable that contains elements that cause Predicate.accept(Object) method to evaluate to false
        Since:
        9.0