Class MapAdapter<K,​V>

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Cloneable, java.lang.Iterable<V>, java.util.Map<K,​V>, InternalIterable<V>, MapIterable<K,​V>, MutableMap<K,​V>, MutableMapIterable<K,​V>, UnsortedMapIterable<K,​V>, RichIterable<V>

    public class MapAdapter<K,​V>
    extends AbstractMutableMap<K,​V>
    implements java.io.Serializable
    This class provides a MutableMap wrapper around a JDK Collections Map interface instance. All the MutableMap interface methods are supported in addition to the JDK Map interface methods.

    To create a new wrapper around an existing Map instance, use the adapt(Map) factory method.

    See Also:
    Serialized Form
    • Field Detail

      • delegate

        protected final java.util.Map<K,​V> delegate
    • Constructor Detail

      • MapAdapter

        protected MapAdapter​(java.util.Map<K,​V> newDelegate)
    • Method Detail

      • adapt

        public static <K,​V> MutableMap<K,​V> adapt​(java.util.Map<K,​V> map)
      • toString

        public java.lang.String toString()
        Description copied from class: AbstractRichIterable
        Returns a string with the elements of the iterable separated by commas with spaces and enclosed in square brackets.
         Assert.assertEquals("[]", Lists.mutable.empty().toString());
         Assert.assertEquals("[1]", Lists.mutable.with(1).toString());
         Assert.assertEquals("[1, 2, 3]", Lists.mutable.with(1, 2, 3).toString());
         
        Specified by:
        toString in interface MapIterable<K,​V>
        Specified by:
        toString in interface RichIterable<K>
        Overrides:
        toString in class AbstractRichIterable<V>
        Returns:
        a string representation of this collection.
        See Also:
        AbstractCollection.toString()
      • newEmpty

        public <K,​V> MutableMap<K,​V> newEmpty​(int capacity)
        Description copied from class: AbstractMutableMap
        Creates a new instance of the same type, using the given capacity and the default growth parameters.
        Specified by:
        newEmpty in class AbstractMutableMap<K,​V>
      • size

        public int size()
        Description copied from interface: RichIterable
        Returns the number of items in this iterable.
        Specified by:
        size in interface java.util.Map<K,​V>
        Specified by:
        size in interface RichIterable<K>
      • remove

        public V remove​(java.lang.Object key)
        Specified by:
        remove in interface java.util.Map<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>
      • clear

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

        public void forEachKeyValue​(Procedure2<? super K,​? super V> procedure)
        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>
      • 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)
      • put

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

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

        public <E> MutableMap<K,​V> collectKeysAndValues​(java.lang.Iterable<E> iterable,
                                                              Function<? super E,​? extends K> keyFunction,
                                                              Function<? super E,​? extends V> valueFunction)
        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>
      • 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)
      • 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)
      • equals

        public boolean equals​(java.lang.Object o)
        Description copied from interface: MapIterable
        Follows the same general contract as Map.equals(Object).
        Specified by:
        equals in interface java.util.Map<K,​V>
        Specified by:
        equals in interface MapIterable<K,​V>
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Description copied from interface: MapIterable
        Follows the same general contract as Map.hashCode().
        Specified by:
        hashCode in interface java.util.Map<K,​V>
        Specified by:
        hashCode in interface MapIterable<K,​V>
        Overrides:
        hashCode in class java.lang.Object