Table
# 功能介绍
Table是Hutool中定义的表格结构接口,用于表示行列结构的数据。它支持行和列的操作,允许通过行键和列键来访问单元格的值,类似于数据库中的二维表结构。Table提供了丰富的方法来操作表格数据,包括添加、删除、修改行和列,以及获取和设置单元格值等。
# 主要特性
- 支持行列结构,通过行键和列键访问单元格值
- 支持添加、删除、修改行和列
- 支持获取和设置单元格值
- 支持遍历表格数据
- 支持转换为其他数据结构(如List、Map等)
- 支持行列的排序和过滤
- 提供多种实现类(如HashBasedTable、TreeBasedTable等)
- 接口设计简洁,易于扩展
- 支持null值,但不支持null行键和列键
- 线程不安全,建议在单线程环境下使用
# 适用场景
- 需要表示行列结构数据的场景
- 需要通过行键和列键访问数据的场景
- 实现二维表数据结构
- 处理Excel表格数据
- 实现矩阵运算
- 处理交叉表数据
- 任何需要二维数据结构的场景
# 使用示例
# 示例1:基本用法
// 创建Table实例(使用HashBasedTable实现)
Table<String, String, Object> table = HashBasedTable.create();
// 设置单元格值
table.put("row1", "col1", "value11");
table.put("row1", "col2", "value12");
table.put("row2", "col1", "value21");
table.put("row2", "col2", "value22");
// 获取单元格值
Object value11 = table.get("row1", "col1");
// 输出: value11
// 获取整行数据
Map<String, Object> row1 = table.row("row1");
// 输出: {col1=value11, col2=value12}
// 获取整列数据
Map<String, Object> col1 = table.column("col1");
// 输出: {row1=value11, row2=value21}
// 获取所有行键
Set<String> rowKeys = table.rowKeySet();
// 输出: [row1, row2]
// 获取所有列键
Set<String> columnKeys = table.columnKeySet();
// 输出: [col1, col2]
# 示例2:添加和删除行/列
Table<String, String, Object> table = HashBasedTable.create();
table.put("row1", "col1", "value11");
table.put("row1", "col2", "value12");
table.put("row2", "col1", "value21");
table.put("row2", "col2", "value22");
// 添加新行
table.put("row3", "col1", "value31");
table.put("row3", "col2", "value32");
// 添加新列
table.put("row1", "col3", "value13");
table.put("row2", "col3", "value23");
table.put("row3", "col3", "value33");
// 删除行
table.removeRow("row1");
// 删除列
table.removeColumn("col1");
// 此时表格内容:
// row2: {col2=value22, col3=value23}
// row3: {col2=value32, col3=value33}
# 示例3:遍历表格数据
Table<String, String, Object> table = HashBasedTable.create();
table.put("row1", "col1", "value11");
table.put("row1", "col2", "value12");
table.put("row2", "col1", "value21");
table.put("row2", "col2", "value22");
// 遍历所有单元格
table.cellSet().forEach(cell -> {
String rowKey = cell.getRowKey();
String columnKey = cell.getColumnKey();
Object value = cell.getValue();
Console.log(rowKey + "," + columnKey + "=" + value);
});
// 输出:
// row1,col1=value11
// row1,col2=value12
// row2,col1=value21
// row2,col2=value22
// 遍历所有行
for (String rowKey : table.rowKeySet()) {
Map<String, Object> row = table.row(rowKey);
Console.log(rowKey + ": " + row);
}
// 输出:
// row1: {col1=value11, col2=value12}
// row2: {col2=value22, col1=value21}
# 示例4:转换为其他数据结构
Table<String, String, Object> table = HashBasedTable.create();
table.put("row1", "col1", "value11");
table.put("row1", "col2", "value12");
table.put("row2", "col1", "value21");
table.put("row2", "col2", "value22");
// 转换为行Map(行键到列键值Map的映射)
Map<String, Map<String, Object>> rowMap = table.rowMap();
// 转换为列Map(列键到行键值Map的映射)
Map<String, Map<String, Object>> columnMap = table.columnMap();
// 转换为List<Map>(每行转换为一个Map)
List<Map<String, Object>> list = new ArrayList<>();
for (String rowKey : table.rowKeySet()) {
Map<String, Object> row = table.row(rowKey);
// 添加行键到Map中
row.put("rowKey", rowKey);
list.add(row);
}
// 输出
Console.log(rowMap);
// 输出: {row1={col1=value11, col2=value12}, row2={col1=value21, col2=value22}}
Console.log(list);
// 输出: [{col1=value11, col2=value12, rowKey=row1}, {col1=value21, col2=value22, rowKey=row2}]
# 注意事项
- Table是一个接口,需要使用具体的实现类(如HashBasedTable、TreeBasedTable等)来创建实例
- 不同的实现类有不同的特性:
- HashBasedTable:基于HashMap实现,无序,性能优异
- TreeBasedTable:基于TreeMap实现,行键和列键有序
- 不支持null行键和列键,但支持null值
- 线程不安全,在多线程环境下使用需要额外同步
put()方法会覆盖已有单元格的值- 支持行和列的添加,但不支持动态扩展列数(需要手动添加列)
- 性能与底层Map实现相当,适合大多数场景
- 可以通过构造函数指定底层Map的实现类
# 方法列表
| 方法名 | 描述 | 参数 | 返回值 |
|---|---|---|---|
| put(R rowKey, C columnKey, V value) | 设置单元格值 | rowKey: 行键, columnKey: 列键, value: 单元格值 | V |
| get(R rowKey, C columnKey) | 获取单元格值 | rowKey: 行键, columnKey: 列键 | V |
| remove(R rowKey, C columnKey) | 删除单元格 | rowKey: 行键, columnKey: 列键 | V |
| removeRow(R rowKey) | 删除整行 | rowKey: 行键 | Map<C, V> |
| removeColumn(C columnKey) | 删除整列 | columnKey: 列键 | Map<R, V> |
| row(R rowKey) | 获取整行数据 | rowKey: 行键 | Map<C, V> |
| column(C columnKey) | 获取整列数据 | columnKey: 列键 | Map<R, V> |
| rowMap() | 获取行映射(行键到列键值Map的映射) | 无 | Map<R, Map<C, V>> |
| columnMap() | 获取列映射(列键到行键值Map的映射) | 无 | Map<C, Map<R, V>> |
| rowKeySet() | 获取所有行键 | 无 | Set |
| columnKeySet() | 获取所有列键 | 无 | Set |
| cellSet() | 获取所有单元格 | 无 | Set<Cell<R, C, V>> |
| size() | 获取单元格数量 | 无 | int |
| isEmpty() | 判断表格是否为空 | 无 | boolean |
| contains(R rowKey, C columnKey) | 判断单元格是否存在 | rowKey: 行键, columnKey: 列键 | boolean |
| containsRow(R rowKey) | 判断行是否存在 | rowKey: 行键 | boolean |
| containsColumn(C columnKey) | 判断列是否存在 | columnKey: 列键 | boolean |
| containsValue(V value) | 判断值是否存在 | value: 单元格值 | boolean |
| clear() | 清空表格 | 无 | void |