Class OpenMap

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

public final class OpenMap extends Object
OpenMap - Map Facade Utility Class OpenMap - Map 门面工具类

Provides comprehensive map operations including creation, transformation, filtering, and querying.

提供全面的 Map 操作,包括创建、转换、过滤和查询。

Features | 主要功能:

  • Factory methods for map creation - Map 创建工厂方法
  • Map transformation - Map 转换
  • Map filtering - Map 过滤
  • Map difference computation - Map 差异计算

Usage Examples | 使用示例:

// Create map - 创建 Map
Map<String, Integer> map = OpenMap.of("a", 1, "b", 2);

// Filter by keys - 按键过滤
Map<String, Integer> filtered = OpenMap.filterKeys(map, k -> k.startsWith("a"));

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

Performance | 性能特性:

  • Most operations: O(n) - 大多数操作: O(n)
  • Single key operations: O(1) - 单键操作: O(1)

Security | 安全性:

  • Thread-safe: No (use ConcurrentMap methods for thread safety) - 线程安全: 否
  • 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 an empty HashMap. 创建空的 HashMap。
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Returns:
      new HashMap | 新的 HashMap
    • of

      public static <K,V> HashMap<K,V> of(K k1, V v1)
      Create a HashMap with one entry. 创建包含一个条目的 HashMap。
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      k1 - key | 键
      v1 - value | 值
      Returns:
      new HashMap | 新的 HashMap
    • of

      public static <K,V> HashMap<K,V> of(K k1, V v1, K k2, V v2)
      Create a HashMap with two entries. 创建包含两个条目的 HashMap。
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      k1 - first key | 第一个键
      v1 - first value | 第一个值
      k2 - second key | 第二个键
      v2 - second value | 第二个值
      Returns:
      new HashMap | 新的 HashMap
    • of

      public static <K,V> HashMap<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3)
      Create a HashMap with three entries. 创建包含三个条目的 HashMap。
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      k1 - first key | 第一个键
      v1 - first value | 第一个值
      k2 - second key | 第二个键
      v2 - second value | 第二个值
      k3 - third key | 第三个键
      v3 - third value | 第三个值
      Returns:
      new HashMap | 新的 HashMap
    • from

      public static <K,V> HashMap<K,V> from(Map<? extends K, ? extends V> map)
      Create a HashMap from another map. 从另一个 Map 创建 HashMap。
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      map - source map | 源 Map
      Returns:
      new HashMap | 新的 HashMap
    • withExpectedSize

      public static <K,V> HashMap<K,V> withExpectedSize(int expectedSize)
      Create a 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 an empty LinkedHashMap. 创建空的 LinkedHashMap。
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Returns:
      new LinkedHashMap | 新的 LinkedHashMap
    • newTreeMap

      public static <K extends Comparable<K>, V> TreeMap<K,V> newTreeMap()
      Create an empty 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 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 an empty ConcurrentHashMap. 创建空的 ConcurrentHashMap。
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Returns:
      new ConcurrentHashMap | 新的 ConcurrentHashMap
    • newIdentityHashMap

      public static <K,V> IdentityHashMap<K,V> newIdentityHashMap()
      Create an empty IdentityHashMap. 创建空的 IdentityHashMap。
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Returns:
      new IdentityHashMap | 新的 IdentityHashMap
    • transformValues

      public static <K,V1,V2> Map<K,V2> transformValues(Map<K,V1> map, Function<? super V1, V2> function)
      Transform values in the map. 转换 Map 中的值。
      Type Parameters:
      K - key type | 键类型
      V1 - input value type | 输入值类型
      V2 - output value type | 输出值类型
      Parameters:
      map - the map | Map
      function - the transform function | 转换函数
      Returns:
      new map with transformed values | 包含转换后值的新 Map
    • transformEntries

      public static <K,V1,V2> Map<K,V2> transformEntries(Map<K,V1> map, EntryTransformer<? super K, ? super V1, V2> transformer)
      Transform entries in the map. 转换 Map 中的条目。
      Type Parameters:
      K - key type | 键类型
      V1 - input value type | 输入值类型
      V2 - output value type | 输出值类型
      Parameters:
      map - the map | Map
      transformer - the entry transformer | 条目转换器
      Returns:
      new map with transformed entries | 包含转换后条目的新 Map
    • filterKeys

      public static <K,V> Map<K,V> filterKeys(Map<K,V> map, Predicate<? super K> predicate)
      Filter map by keys. 按键过滤 Map。
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      map - the map | Map
      predicate - the key predicate | 键谓词
      Returns:
      filtered map | 过滤后的 Map
    • filterValues

      public static <K,V> Map<K,V> filterValues(Map<K,V> map, Predicate<? super V> predicate)
      Filter map by values. 按值过滤 Map。
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      map - the map | Map
      predicate - the value predicate | 值谓词
      Returns:
      filtered map | 过滤后的 Map
    • filterEntries

      public static <K,V> Map<K,V> filterEntries(Map<K,V> map, Predicate<? super Map.Entry<K,V>> predicate)
      Filter map by entries. 按条目过滤 Map。
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      map - the map | Map
      predicate - the entry predicate | 条目谓词
      Returns:
      filtered map | 过滤后的 Map
    • uniqueIndex

      public static <K,V> Map<K,V> uniqueIndex(Iterable<V> values, Function<? super V, K> keyFunction)
      Create a map indexed by unique key. 按唯一键创建索引 Map。
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      values - the values | 值
      keyFunction - the key function | 键函数
      Returns:
      indexed map | 索引 Map
      Throws:
      IllegalArgumentException - if duplicate keys | 如果有重复键
    • getOrDefault

      public static <K,V> V getOrDefault(Map<K,V> map, K key, V defaultValue)
      Get value safely with default. 安全获取值,带默认值。
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      map - the map | Map
      key - the key | 键
      defaultValue - default value | 默认值
      Returns:
      value or default | 值或默认值
    • containsAllKeys

      @SafeVarargs public static <K> boolean containsAllKeys(Map<K,?> map, K... keys)
      Check if map contains all keys. 检查 Map 是否包含所有键。
      Type Parameters:
      K - key type | 键类型
      Parameters:
      map - the map | Map
      keys - the keys | 键
      Returns:
      true if all keys present | 如果所有键都存在则返回 true
    • 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 | 不可变条目