Class ImmutableTable<R,C,V>

java.lang.Object
cloud.opencode.base.collections.immutable.ImmutableTable<R,C,V>
Type Parameters:
R - row key type | 行键类型
C - column key type | 列键类型
V - value type | 值类型
All Implemented Interfaces:
Serializable

public final class ImmutableTable<R,C,V> extends Object implements Serializable
ImmutableTable - Immutable Table (Row-Column-Value) Implementation ImmutableTable - 不可变表格(行-列-值)实现

A two-dimensional map-like data structure that uses row and column keys to store values. Cannot be modified after creation.

使用行和列键存储值的二维类映射数据结构。创建后不能修改。

Features | 主要功能:

  • Immutable - 不可变
  • Thread-safe - 线程安全
  • Null-safe (nulls not allowed) - 空值安全(不允许空值)
  • Two-dimensional structure - 二维结构
  • O(1) cell access - O(1) 单元格访问
  • Row and column views - 行和列视图

Usage Examples | 使用示例:

// Create table - 创建表格
ImmutableTable<String, String, Integer> table = ImmutableTable.<String, String, Integer>builder()
    .put("row1", "col1", 1)
    .put("row1", "col2", 2)
    .put("row2", "col1", 3)
    .put("row2", "col2", 4)
    .build();

// Access cell - 访问单元格
Integer value = table.get("row1", "col1"); // Returns 1 - 返回 1

// Check existence - 检查存在性
boolean exists = table.contains("row1", "col1"); // Returns true - 返回 true

// Get row - 获取行
Map<String, Integer> row = table.row("row1"); // Returns {col1=1, col2=2}

// Get column - 获取列
Map<String, Integer> column = table.column("col1"); // Returns {row1=1, row2=3}

// Get all rows/columns - 获取所有行/列
Set<String> rowKeys = table.rowKeySet();
Set<String> columnKeys = table.columnKeySet();

Performance | 性能特性:

  • get(row, column): O(1) - get(行, 列): O(1)
  • contains: O(1) - contains: O(1)
  • row/column: O(1) map lookup - row/column: O(1) 映射查找
  • rowKeySet/columnKeySet: O(1) - rowKeySet/columnKeySet: O(1)

Security | 安全性:

  • Thread-safe: Yes (immutable) - 线程安全: 是(不可变)
  • Null-safe: Yes (nulls not allowed) - 空值安全: 是(不允许空值)
Since:
JDK 25, opencode-base-collections V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • of

      public static <R,C,V> ImmutableTable<R,C,V> of()
      Return an empty immutable table. 返回空不可变表格。
      Type Parameters:
      R - row key type | 行键类型
      C - column key type | 列键类型
      V - value type | 值类型
      Returns:
      empty immutable table | 空不可变表格
    • of

      public static <R,C,V> ImmutableTable<R,C,V> of(R rowKey, C colKey, V value)
      Return an immutable table containing the given cell. 返回包含给定单元格的不可变表格。
      Type Parameters:
      R - row key type | 行键类型
      C - column key type | 列键类型
      V - value type | 值类型
      Parameters:
      rowKey - the row key | 行键
      colKey - the column key | 列键
      value - the value | 值
      Returns:
      immutable table | 不可变表格
    • builder

      public static <R,C,V> ImmutableTable.Builder<R,C,V> builder()
      Return a new builder. 返回新构建器。
      Type Parameters:
      R - row key type | 行键类型
      C - column key type | 列键类型
      V - value type | 值类型
      Returns:
      builder | 构建器
    • get

      public V get(Object rowKey, Object colKey)
      Return the value at the given row and column. 返回给定行和列处的值。
      Parameters:
      rowKey - the row key | 行键
      colKey - the column key | 列键
      Returns:
      the value, or null if not present | 值,如果不存在则返回 null
    • contains

      public boolean contains(Object rowKey, Object colKey)
      Check if the table contains the given row and column. 检查表格是否包含给定的行和列。
      Parameters:
      rowKey - the row key | 行键
      colKey - the column key | 列键
      Returns:
      true if present | 如果存在则返回 true
    • containsRow

      public boolean containsRow(Object rowKey)
      Check if the table contains the given row key. 检查表格是否包含给定的行键。
      Parameters:
      rowKey - the row key | 行键
      Returns:
      true if present | 如果存在则返回 true
    • containsColumn

      public boolean containsColumn(Object colKey)
      Check if the table contains the given column key. 检查表格是否包含给定的列键。
      Parameters:
      colKey - the column key | 列键
      Returns:
      true if present | 如果存在则返回 true
    • containsValue

      public boolean containsValue(Object value)
      Check if the table contains the given value. 检查表格是否包含给定的值。
      Parameters:
      value - the value | 值
      Returns:
      true if present | 如果存在则返回 true
    • row

      public Map<C,V> row(R rowKey)
      Return the row map for the given row key. 返回给定行键的行映射。
      Parameters:
      rowKey - the row key | 行键
      Returns:
      unmodifiable map of columns to values | 列到值的不可修改映射
    • column

      public Map<R,V> column(C colKey)
      Return the column map for the given column key. 返回给定列键的列映射。
      Parameters:
      colKey - the column key | 列键
      Returns:
      unmodifiable map of rows to values | 行到值的不可修改映射
    • rowMap

      public Map<R,Map<C,V>> rowMap()
      Return the map of all rows. 返回所有行的映射。
      Returns:
      unmodifiable map of row keys to row maps | 行键到行映射的不可修改映射
    • columnMap

      public Map<C,Map<R,V>> columnMap()
      Return the map of all columns. 返回所有列的映射。
      Returns:
      unmodifiable map of column keys to column maps | 列键到列映射的不可修改映射
    • rowKeySet

      public Set<R> rowKeySet()
      Return the set of all row keys. 返回所有行键的集合。
      Returns:
      unmodifiable set of row keys | 行键的不可修改集合
    • columnKeySet

      public Set<C> columnKeySet()
      Return the set of all column keys. 返回所有列键的集合。
      Returns:
      unmodifiable set of column keys | 列键的不可修改集合
    • values

      public Collection<V> values()
      Return the collection of all values. 返回所有值的集合。
      Returns:
      unmodifiable collection of values | 值的不可修改集合
    • cellSet

      public Set<ImmutableTable.Cell<R,C,V>> cellSet()
      Return the set of all cells. 返回所有单元格的集合。
      Returns:
      unmodifiable set of cells | 单元格的不可修改集合
    • size

      public int size()
      Return the number of cells in the table. 返回表格中的单元格数。
      Returns:
      the size | 大小
    • isEmpty

      public boolean isEmpty()
      Check if the table is empty. 检查表格是否为空。
      Returns:
      true if empty | 如果为空则返回 true
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object