Class MapWrapper<K,V>

java.lang.Object
com.luna.common.map.MapWrapper<K,V>
Type Parameters:
K - 键类型
V - 值类型
All Implemented Interfaces:
Serializable, Cloneable, Iterable<Map.Entry<K,V>>, Map<K,V>
Direct Known Subclasses:
BiMap

public class MapWrapper<K,V> extends Object implements Map<K,V>, Iterable<Map.Entry<K,V>>, Serializable, Cloneable
Map包装类,通过包装一个已有Map实现特定功能。例如自定义Key的规则或Value规则
Since:
4.3.3
Author:
looly
See Also:
  • Field Details

    • DEFAULT_LOAD_FACTOR

      protected static final float DEFAULT_LOAD_FACTOR
      默认增长因子
      See Also:
    • DEFAULT_INITIAL_CAPACITY

      protected static final int DEFAULT_INITIAL_CAPACITY
      默认初始大小
      See Also:
  • Constructor Details

    • MapWrapper

      public MapWrapper(java.util.function.Supplier<Map<K,V>> mapFactory)
      构造
      通过传入一个Map从而确定Map的类型,子类需创建一个空的Map,而非传入一个已有Map,否则值可能会被修改
      Parameters:
      mapFactory - 空Map创建工厂
      Since:
      5.8.0
    • MapWrapper

      public MapWrapper(Map<K,V> raw)
      构造
      Parameters:
      raw - 被包装的Map
  • Method Details

    • inverse

      public static <K, V> Map<V,K> inverse(Map<K,V> map)
      Map的键和值互换
      互换键值对不检查值是否有重复,如果有则后加入的元素替换先加入的元素
      值的顺序在HashMap中不确定,所以谁覆盖谁也不确定,在有序的Map中按照先后顺序覆盖,保留最后的值
      Type Parameters:
      K - 键和值类型
      V - 键和值类型
      Parameters:
      map - Map对象,键值类型必须一致
      Returns:
      互换后的Map
      Since:
      5.2.6
    • getRaw

      public Map<K,V> getRaw()
      获取原始的Map
      Returns:
      Map
    • size

      public int size()
      Specified by:
      size in interface Map<K,V>
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Map<K,V>
    • containsKey

      public boolean containsKey(Object key)
      Specified by:
      containsKey in interface Map<K,V>
    • containsValue

      public boolean containsValue(Object value)
      Specified by:
      containsValue in interface Map<K,V>
    • get

      public V get(Object key)
      Specified by:
      get in interface Map<K,V>
    • put

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

      public V remove(Object key)
      Specified by:
      remove in interface Map<K,V>
    • putAll

      public void putAll(Map<? extends K,? extends V> m)
      Specified by:
      putAll in interface Map<K,V>
    • clear

      public void clear()
      Specified by:
      clear in interface Map<K,V>
    • values

      public Collection<V> values()
      Specified by:
      values in interface Map<K,V>
    • keySet

      public Set<K> keySet()
      Specified by:
      keySet in interface Map<K,V>
    • entrySet

      public Set<Map.Entry<K,V>> entrySet()
      Specified by:
      entrySet in interface Map<K,V>
    • iterator

      public Iterator<Map.Entry<K,V>> iterator()
      Specified by:
      iterator in interface Iterable<K>
    • equals

      public boolean equals(Object o)
      Specified by:
      equals in interface Map<K,V>
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Specified by:
      hashCode in interface Map<K,V>
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • forEach

      public void forEach(java.util.function.BiConsumer<? super K,? super V> action)
      Specified by:
      forEach in interface Map<K,V>
    • replaceAll

      public void replaceAll(java.util.function.BiFunction<? super K,? super V,? extends V> function)
      Specified by:
      replaceAll in interface Map<K,V>
    • putIfAbsent

      public V putIfAbsent(K key, V value)
      Specified by:
      putIfAbsent in interface Map<K,V>
    • remove

      public boolean remove(Object key, Object value)
      Specified by:
      remove in interface Map<K,V>
    • replace

      public boolean replace(K key, V oldValue, V newValue)
      Specified by:
      replace in interface Map<K,V>
    • replace

      public V replace(K key, V value)
      Specified by:
      replace in interface Map<K,V>
    • computeIfAbsent

      public V computeIfAbsent(K key, java.util.function.Function<? super K,? extends V> mappingFunction)
      Specified by:
      computeIfAbsent in interface Map<K,V>
    • getOrDefault

      public V getOrDefault(Object key, V defaultValue)
      Specified by:
      getOrDefault in interface Map<K,V>
    • computeIfPresent

      public V computeIfPresent(K key, java.util.function.BiFunction<? super K,? super V,? extends V> remappingFunction)
      Specified by:
      computeIfPresent in interface Map<K,V>
    • compute

      public V compute(K key, java.util.function.BiFunction<? super K,? super V,? extends V> remappingFunction)
      Specified by:
      compute in interface Map<K,V>
    • merge

      public V merge(K key, V value, java.util.function.BiFunction<? super V,? super V,? extends V> remappingFunction)
      Specified by:
      merge in interface Map<K,V>
    • clone

      public MapWrapper<K,V> clone() throws CloneNotSupportedException
      Overrides:
      clone in class Object
      Throws:
      CloneNotSupportedException