Class MapUtil

java.lang.Object
cloud.opencode.base.collections.MapUtil

public final class MapUtil extends Object
MapUtil - Map Utility Class MapUtil - 映射工具类

Provides factory methods and operations for Map implementations including HashMap, LinkedHashMap, TreeMap, and ConcurrentMap.

提供 Map 实现的工厂方法和操作,包括 HashMap、LinkedHashMap、TreeMap 和 ConcurrentMap。

Features | 主要功能:

  • Factory methods for map creation - Map 创建工厂方法
  • Map difference calculation - Map 差异计算
  • Transformation views - 转换视图
  • Filtering views - 过滤视图

Usage Examples | 使用示例:

// Create HashMap - 创建 HashMap
Map<String, Integer> map = MapUtil.newHashMap();

// Unique index - 唯一索引
Map<String, User> byId = MapUtil.uniqueIndex(users, User::getId);

// Map difference - Map 差异
MapDifference<String, Integer> diff = MapUtil.difference(map1, map2);

// Transform values - 转换值
Map<String, String> stringValues = MapUtil.transformValues(map, Object::toString);

Performance | 性能特性:

  • Factory methods: O(1) or O(n) - 工厂方法: O(1) 或 O(n)
  • uniqueIndex: O(n) - uniqueIndex: O(n)
  • difference: O(n + m) - difference: O(n + m)

Security | 安全性:

  • Thread-safe: No (except ConcurrentMap) - 线程安全: 否(ConcurrentMap 除外)
  • Null-safe: Yes - 空值安全: 是
Since:
JDK 25, opencode-base-collections V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • newHashMap

      public static <K,V> HashMap<K,V> newHashMap()
      Create a new HashMap 创建 HashMap
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Returns:
      new HashMap | 新的 HashMap
    • newHashMap

      public static <K,V> HashMap<K,V> newHashMap(Map<? extends K, ? extends V> map)
      Create a new HashMap from an existing Map 从 Map 创建 HashMap
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      map - source map | 源 Map
      Returns:
      new HashMap | 新的 HashMap
    • newHashMapWithExpectedSize

      public static <K,V> HashMap<K,V> newHashMapWithExpectedSize(int expectedSize)
      Create a new HashMap with expected size 创建指定容量的 HashMap
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      expectedSize - expected size | 预期大小
      Returns:
      new HashMap | 新的 HashMap
    • newLinkedHashMap

      public static <K,V> LinkedHashMap<K,V> newLinkedHashMap()
      Create a new LinkedHashMap 创建 LinkedHashMap
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Returns:
      new LinkedHashMap | 新的 LinkedHashMap
    • newLinkedHashMapWithExpectedSize

      public static <K,V> LinkedHashMap<K,V> newLinkedHashMapWithExpectedSize(int expectedSize)
      Create a new LinkedHashMap with expected size 创建指定容量的 LinkedHashMap
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      expectedSize - expected size | 预期大小
      Returns:
      new LinkedHashMap | 新的 LinkedHashMap
    • newTreeMap

      public static <K extends Comparable<K>, V> TreeMap<K,V> newTreeMap()
      Create a new TreeMap 创建 TreeMap
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Returns:
      new TreeMap | 新的 TreeMap
    • newTreeMap

      public static <K,V> TreeMap<K,V> newTreeMap(Comparator<? super K> comparator)
      Create a new TreeMap with comparator 带比较器的 TreeMap
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      comparator - the comparator | 比较器
      Returns:
      new TreeMap | 新的 TreeMap
    • newConcurrentMap

      public static <K,V> ConcurrentMap<K,V> newConcurrentMap()
      Create a new ConcurrentMap 创建 ConcurrentMap
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Returns:
      new ConcurrentMap | 新的 ConcurrentMap
    • newIdentityHashMap

      public static <K,V> IdentityHashMap<K,V> newIdentityHashMap()
      Create a new IdentityHashMap 创建 IdentityHashMap
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Returns:
      new IdentityHashMap | 新的 IdentityHashMap
    • uniqueIndex

      public static <K,V> Map<K,V> uniqueIndex(Iterable<V> values, Function<? super V, K> keyFunction)
      Create a map indexed by unique keys extracted from values 按唯一键索引
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      values - the values | 值
      keyFunction - the key extractor | 键提取器
      Returns:
      indexed map | 索引后的 Map
      Throws:
      OpenCollectionException - if duplicate keys | 如果有重复键
    • uniqueIndex

      public static <K,V> Map<K,V> uniqueIndex(Iterator<V> values, Function<? super V, K> keyFunction)
      Create a map indexed by unique keys extracted from values 按唯一键索引(Iterator)
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      values - the values | 值
      keyFunction - the key extractor | 键提取器
      Returns:
      indexed map | 索引后的 Map
      Throws:
      OpenCollectionException - if duplicate keys | 如果有重复键
    • difference

      public static <K,V> MapDifference<K,V> difference(Map<? extends K, ? extends V> left, Map<? extends K, ? extends V> right)
      Compute the difference between two maps 计算两个 Map 的差异
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      left - left map | 左 Map
      right - right map | 右 Map
      Returns:
      map difference | Map 差异
    • transformValues

      public static <K,V1,V2> Map<K,V2> transformValues(Map<K,V1> fromMap, Function<? super V1, V2> function)
      Return a view with transformed values 转换值视图
      Type Parameters:
      K - key type | 键类型
      V1 - original value type | 原值类型
      V2 - transformed value type | 转换后值类型
      Parameters:
      fromMap - source map | 源 Map
      function - transform function | 转换函数
      Returns:
      transformed view | 转换视图
    • transformEntries

      public static <K,V1,V2> Map<K,V2> transformEntries(Map<K,V1> fromMap, MapUtil.EntryTransformer<? super K, ? super V1, V2> transformer)
      Return a view with transformed entries 转换条目视图
      Type Parameters:
      K - key type | 键类型
      V1 - original value type | 原值类型
      V2 - transformed value type | 转换后值类型
      Parameters:
      fromMap - source map | 源 Map
      transformer - entry transformer | 条目转换器
      Returns:
      transformed view | 转换视图
    • filterKeys

      public static <K,V> Map<K,V> filterKeys(Map<K,V> unfiltered, Predicate<? super K> keyPredicate)
      Return a view filtered by keys 过滤键视图
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      unfiltered - source map | 源 Map
      keyPredicate - key predicate | 键谓词
      Returns:
      filtered view | 过滤视图
    • filterValues

      public static <K,V> Map<K,V> filterValues(Map<K,V> unfiltered, Predicate<? super V> valuePredicate)
      Return a view filtered by values 过滤值视图
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      unfiltered - source map | 源 Map
      valuePredicate - value predicate | 值谓词
      Returns:
      filtered view | 过滤视图
    • filterEntries

      public static <K,V> Map<K,V> filterEntries(Map<K,V> unfiltered, Predicate<? super Map.Entry<K,V>> entryPredicate)
      Return a view filtered by entries 过滤条目视图
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      unfiltered - source map | 源 Map
      entryPredicate - entry predicate | 条目谓词
      Returns:
      filtered view | 过滤视图
    • fromProperties

      public static Map<String,String> fromProperties(Properties properties)
      Create a Properties object to Map 从属性创建 Map
      Parameters:
      properties - the properties | 属性
      Returns:
      map | Map
    • immutableEntry

      public static <K,V> Map.Entry<K,V> immutableEntry(K key, V value)
      Create an immutable entry 创建不可变条目
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      key - the key | 键
      value - the value | 值
      Returns:
      immutable entry | 不可变条目
    • capacity

      public static int capacity(int expectedSize)
      Calculate capacity for expected size 计算 HashMap 所需容量
      Parameters:
      expectedSize - expected size | 预期大小
      Returns:
      capacity | 容量
    • synchronizedBiMap

      public static <K,V> BiMap<K,V> synchronizedBiMap(BiMap<K,V> bimap)
      Create a synchronized BiMap wrapper. 创建同步的 BiMap 包装器。
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      bimap - the BiMap to synchronize | 要同步的 BiMap
      Returns:
      synchronized BiMap | 同步的 BiMap
    • unmodifiableBiMap

      public static <K,V> BiMap<K,V> unmodifiableBiMap(BiMap<? extends K, ? extends V> bimap)
      Create an unmodifiable BiMap wrapper. 创建不可修改的 BiMap 包装器。
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      bimap - the BiMap to wrap | 要包装的 BiMap
      Returns:
      unmodifiable BiMap | 不可修改的 BiMap
    • difference

      public static <K,V> MapDifference<K,V> difference(Map<? extends K, ? extends V> left, Map<? extends K, ? extends V> right, Equivalence<? super V> valueEquivalence)
      Calculate the difference between two maps using custom value equivalence. 使用自定义值等价关系计算两个 Map 的差异。
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      left - left map | 左 Map
      right - right map | 右 Map
      valueEquivalence - value equivalence | 值等价关系
      Returns:
      map difference | Map 差异