MultiKeyMap

MultiKeyMap-混合key的map: 

MultiKeyMap map = new MultiKeyMap();
map.put(1,1,"demo");

 

 

 

org.apache.commons.collections4.map

Class MultiKeyMap<K,V>

  • Type Parameters:

    K - the type of the keys in this map

    V - the type of the values in this map

    All Implemented Interfaces:

    SerializableCloneableMap<MultiKey<? extends K>,V>, Get<MultiKey<? extends K>,V>, IterableGet<MultiKey<? extends K>,V>, IterableMap<MultiKey<? extends K>,V>, Put<MultiKey<? extends K>,V>


     
    public class MultiKeyMap<K,V>
    extends AbstractMapDecorator<MultiKey<? extends K>,V>
    implements Serializable, Cloneable
    Map implementation that uses multiple keys to map the value.

    This class is the most efficient way to uses multiple keys to map to a value. The best way to use this class is via the additional map-style methods. These provide getcontainsKeyput and remove for individual keys which operate without extra object creation.

    The additional methods are the main interface of this map. As such, you will not normally hold this map in a variable of type Map.

    The normal map methods take in and return a MultiKey. If you try to use put() with any other object type a ClassCastException is thrown. If you try to use null as the key in put() a NullPointerException is thrown.

    This map is implemented as a decorator of a AbstractHashedMap which enables extra behaviour to be added easily.

    • MultiKeyMap.decorate(new LinkedMap()) creates an ordered map.
    • MultiKeyMap.decorate(new LRUMap()) creates an least recently used map.
    • MultiKeyMap.decorate(new ReferenceMap()) creates a garbage collector sensitive map.
    Note that IdentityMap and ReferenceIdentityMap are unsuitable for use as the key comparison would work on the whole MultiKey, not the elements within.

    As an example, consider a least recently used cache that uses a String airline code and a Locale to lookup the airline's name:

     private MultiKeyMap cache = MultiKeyMap.multiKeyMap(new LRUMap(50));
    
     public String getAirlineName(String code, String locale) {
       String name = (String) cache.get(code, locale);
       if (name == null) {
         name = getAirlineNameFromDB(code, locale);
         cache.put(code, locale, name);
       }
       return name;
     }
     

    Note that MultiKeyMap is not synchronized and is not thread-safe. If you wish to use this map from multiple threads concurrently, you must use appropriate synchronization. This class may throw exceptions when accessed by concurrent threads without synchronization.

    Since:

    3.1

    See Also:

    Serialized Form

    • Constructor Detail

      • MultiKeyMap

        public MultiKeyMap()

        Constructs a new MultiKeyMap that decorates a HashedMap.

    • Method Detail

      • multiKeyMap

        public static <K,V> MultiKeyMap<K,V> multiKeyMap(AbstractHashedMap<MultiKey<? extends K>,V> map)

        Decorates the specified map to add the MultiKeyMap API and fast query. The map must not be null and must be empty.

        Type Parameters:

        K - the key type

        V - the value type

        Parameters:

        map - the map to decorate, not null

        Returns:

        a new multi key map

        Throws:

        NullPointerException - if map is null

        IllegalArgumentException - if the map is not empty

        Since:

        4.0

      • get

        public V get(Object key1,
                     Object key2)

        Gets the value mapped to the specified multi-key.

        Parameters:

        key1 - the first key

        key2 - the second key

        Returns:

        the mapped value, null if no match

      • containsKey

        public boolean containsKey(Object key1,
                                   Object key2)

        Checks whether the map contains the specified multi-key.

        Parameters:

        key1 - the first key

        key2 - the second key

        Returns:

        true if the map contains the key

      • put

        public V put(K key1,
                     K key2,
                     V value)

        Stores the value against the specified multi-key.

        Parameters:

        key1 - the first key

        key2 - the second key

        value - the value to store

        Returns:

        the value previously mapped to this combined key, null if none

      • removeMultiKey

        public V removeMultiKey(Object key1,
                                Object key2)

        Removes the specified multi-key from this map.

        Parameters:

        key1 - the first key

        key2 - the second key

        Returns:

        the value mapped to the removed key, null if key not in map

        Since:

        4.0 (previous name: remove(Object, Object))

      • hash

        protected int hash(Object key1,
                           Object key2)

        Gets the hash code for the specified multi-key.

        Parameters:

        key1 - the first key

        key2 - the second key

        Returns:

        the hash code

      • get

        public V get(Object key1,
                     Object key2,
                     Object key3)

        Gets the value mapped to the specified multi-key.

        Parameters:

        key1 - the first key

        key2 - the second key

        key3 - the third key

        Returns:

        the mapped value, null if no match

      • containsKey

        public boolean containsKey(Object key1,
                                   Object key2,
                                   Object key3)

        Checks whether the map contains the specified multi-key.

        Parameters:

        key1 - the first key

        key2 - the second key

        key3 - the third key

        Returns:

        true if the map contains the key

      • put

        public V put(K key1,
                     K key2,
                     K key3,
                     V value)

        Stores the value against the specified multi-key.

        Parameters:

        key1 - the first key

        key2 - the second key

        key3 - the third key

        value - the value to store

        Returns:

        the value previously mapped to this combined key, null if none

      • removeMultiKey

        public V removeMultiKey(Object key1,
                                Object key2,
                                Object key3)

        Removes the specified multi-key from this map.

        Parameters:

        key1 - the first key

        key2 - the second key

        key3 - the third key

        Returns:

        the value mapped to the removed key, null if key not in map

        Since:

        4.0 (previous name: remove(Object, Object, Object))

      • hash

        protected int hash(Object key1,
                           Object key2,
                           Object key3)

        Gets the hash code for the specified multi-key.

        Parameters:

        key1 - the first key

        key2 - the second key

        key3 - the third key

        Returns:

        the hash code

      • isEqualKey

        protected boolean isEqualKey(AbstractHashedMap.HashEntry<MultiKey<? extends K>,V> entry,
                                     Object key1,
                                     Object key2,
                                     Object key3)

        Is the key equal to the combined key.

        Parameters:

        entry - the entry to compare to

        key1 - the first key

        key2 - the second key

        key3 - the third key

        Returns:

        true if the key matches

      • get

        public V get(Object key1,
                     Object key2,
                     Object key3,
                     Object key4)

        Gets the value mapped to the specified multi-key.

        Parameters:

        key1 - the first key

        key2 - the second key

        key3 - the third key

        key4 - the fourth key

        Returns:

        the mapped value, null if no match

      • containsKey

        public boolean containsKey(Object key1,
                                   Object key2,
                                   Object key3,
                                   Object key4)

        Checks whether the map contains the specified multi-key.

        Parameters:

        key1 - the first key

        key2 - the second key

        key3 - the third key

        key4 - the fourth key

        Returns:

        true if the map contains the key

      • put

        public V put(K key1,
                     K key2,
                     K key3,
                     K key4,
                     V value)

        Stores the value against the specified multi-key.

        Parameters:

        key1 - the first key

        key2 - the second key

        key3 - the third key

        key4 - the fourth key

        value - the value to store

        Returns:

        the value previously mapped to this combined key, null if none

      • removeMultiKey

        public V removeMultiKey(Object key1,
                                Object key2,
                                Object key3,
                                Object key4)

        Removes the specified multi-key from this map.

        Parameters:

        key1 - the first key

        key2 - the second key

        key3 - the third key

        key4 - the fourth key

        Returns:

        the value mapped to the removed key, null if key not in map

        Since:

        4.0 (previous name: remove(Object, Object, Object, Object))

      • hash

        protected int hash(Object key1,
                           Object key2,
                           Object key3,
                           Object key4)

        Gets the hash code for the specified multi-key.

        Parameters:

        key1 - the first key

        key2 - the second key

        key3 - the third key

        key4 - the fourth key

        Returns:

        the hash code

      • isEqualKey

        protected boolean isEqualKey(AbstractHashedMap.HashEntry<MultiKey<? extends K>,V> entry,
                                     Object key1,
                                     Object key2,
                                     Object key3,
                                     Object key4)

        Is the key equal to the combined key.

        Parameters:

        entry - the entry to compare to

        key1 - the first key

        key2 - the second key

        key3 - the third key

        key4 - the fourth key

        Returns:

        true if the key matches

      • get

        public V get(Object key1,
                     Object key2,
                     Object key3,
                     Object key4,
                     Object key5)

        Gets the value mapped to the specified multi-key.

        Parameters:

        key1 - the first key

        key2 - the second key

        key3 - the third key

        key4 - the fourth key

        key5 - the fifth key

        Returns:

        the mapped value, null if no match

      • containsKey

        public boolean containsKey(Object key1,
                                   Object key2,
                                   Object key3,
                                   Object key4,
                                   Object key5)

        Checks whether the map contains the specified multi-key.

        Parameters:

        key1 - the first key

        key2 - the second key

        key3 - the third key

        key4 - the fourth key

        key5 - the fifth key

        Returns:

        true if the map contains the key

      • put

        public V put(K key1,
                     K key2,
                     K key3,
                     K key4,
                     K key5,
                     V value)

        Stores the value against the specified multi-key.

        Parameters:

        key1 - the first key

        key2 - the second key

        key3 - the third key

        key4 - the fourth key

        key5 - the fifth key

        value - the value to store

        Returns:

        the value previously mapped to this combined key, null if none

      • removeMultiKey

        public V removeMultiKey(Object key1,
                                Object key2,
                                Object key3,
                                Object key4,
                                Object key5)

        Removes the specified multi-key from this map.

        Parameters:

        key1 - the first key

        key2 - the second key

        key3 - the third key

        key4 - the fourth key

        key5 - the fifth key

        Returns:

        the value mapped to the removed key, null if key not in map

        Since:

        4.0 (previous name: remove(Object, Object, Object, Object, Object))

      • hash

        protected int hash(Object key1,
                           Object key2,
                           Object key3,
                           Object key4,
                           Object key5)

        Gets the hash code for the specified multi-key.

        Parameters:

        key1 - the first key

        key2 - the second key

        key3 - the third key

        key4 - the fourth key

        key5 - the fifth key

        Returns:

        the hash code

      • isEqualKey

        protected boolean isEqualKey(AbstractHashedMap.HashEntry<MultiKey<? extends K>,V> entry,
                                     Object key1,
                                     Object key2,
                                     Object key3,
                                     Object key4,
                                     Object key5)

        Is the key equal to the combined key.

        Parameters:

        entry - the entry to compare to

        key1 - the first key

        key2 - the second key

        key3 - the third key

        key4 - the fourth key

        key5 - the fifth key

        Returns:

        true if the key matches

      • removeAll

        public boolean removeAll(Object key1)
        Removes all mappings where the first key is that specified.

        This method removes all the mappings where the MultiKey has one or more keys, and the first matches that specified.

        Parameters:

        key1 - the first key

        Returns:

        true if any elements were removed

      • removeAll

        public boolean removeAll(Object key1,
                                 Object key2)
        Removes all mappings where the first two keys are those specified.

        This method removes all the mappings where the MultiKey has two or more keys, and the first two match those specified.

        Parameters:

        key1 - the first key

        key2 - the second key

        Returns:

        true if any elements were removed

      • removeAll

        public boolean removeAll(Object key1,
                                 Object key2,
                                 Object key3)
        Removes all mappings where the first three keys are those specified.

        This method removes all the mappings where the MultiKey has three or more keys, and the first three match those specified.

        Parameters:

        key1 - the first key

        key2 - the second key

        key3 - the third key

        Returns:

        true if any elements were removed

      • removeAll

        public boolean removeAll(Object key1,
                                 Object key2,
                                 Object key3,
                                 Object key4)
        Removes all mappings where the first four keys are those specified.

        This method removes all the mappings where the MultiKey has four or more keys, and the first four match those specified.

        Parameters:

        key1 - the first key

        key2 - the second key

        key3 - the third key

        key4 - the fourth key

        Returns:

        true if any elements were removed

      • checkKey

        protected void checkKey(MultiKey<?> key)

        Check to ensure that input keys are valid MultiKey objects.

        Parameters:

        key - the key to check

      • clone

        public MultiKeyMap<K,Vclone()

        Clones the map without cloning the keys or values.

        Overrides:

        clone in class Object

        Returns:

        a shallow clone

      • mapIterator

        public MapIterator<MultiKey<? extends K>,VmapIterator()

        Description copied from class: AbstractIterableMap

        Obtains a MapIterator over the map.

        A map iterator is an efficient way of iterating over maps. There is no need to access the entry set or use Map Entry objects.

         IterableMap<String,Integer> map = new HashedMap<String,Integer>();
         MapIterator<String,Integer> it = map.mapIterator();
         while (it.hasNext()) {
           String key = it.next();
           Integer value = it.getValue();
           it.setValue(value + 1);
         }
         

        Specified by:

        mapIterator in interface IterableGet<MultiKey<? extends K>,V>

        Overrides:

        mapIterator in class AbstractIterableMap<MultiKey<? extends K>,V>

        Returns:

        a map iterator

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值