Class CsvStats
Provides count, numeric aggregate, distinct, and frequency operations
on columns of a CsvDocument. All methods are static and the class
cannot be instantiated.
提供对 CsvDocument 列的计数、数值聚合、去重和频率操作。
所有方法均为静态方法,该类不可实例化。
Features | 主要功能:
- Count operations (non-blank, total rows) - 计数操作(非空白、总行数)
- Numeric aggregation (sum, avg, min, max) - 数值聚合(求和、平均、最小、最大)
- String operations (distinct, frequency) - 字符串操作(去重、频率)
- Column summary (all stats at once) - 列摘要(一次性获取所有统计)
Usage Examples | 使用示例:
CsvDocument doc = CsvDocument.builder()
.header("name", "score")
.addRow("Alice", "95")
.addRow("Bob", "87")
.build();
long count = CsvStats.count(doc, "name"); // 2
BigDecimal total = CsvStats.sum(doc, "score"); // 182
BigDecimal average = CsvStats.avg(doc, "score"); // 91.000000
List<String> names = CsvStats.distinct(doc, "name"); // [Alice, Bob]
Security | 安全性:
- Thread-safe: Yes (stateless utility) - 线程安全: 是(无状态工具类)
- Null-safe: All null params throw NullPointerException - 空值安全: 所有null参数抛出NullPointerException
- Since:
- JDK 25, opencode-base-csv V1.0.3
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic BigDecimalavg(CsvDocument doc, String column) Computes the average of numeric values in a column 计算列中数值的平均值static longcount(CsvDocument doc, String column) Counts non-blank values in a column 计算列中非空白值的数量static longcountAll(CsvDocument doc) Counts all rows in the document 计算文档中所有行的数量distinct(CsvDocument doc, String column) Returns distinct non-null values in a column, preserving insertion order 返回列中去重的非null值,保留插入顺序frequency(CsvDocument doc, String column) Computes frequency of each value in a column, ordered by count descending 计算列中每个值的频率,按计数降序排列static BigDecimalmax(CsvDocument doc, String column) Finds the maximum numeric value in a column 查找列中的最大数值static BigDecimalmin(CsvDocument doc, String column) Finds the minimum numeric value in a column 查找列中的最小数值static BigDecimalsum(CsvDocument doc, String column) Computes the sum of numeric values in a column 计算列中数值的总和static CsvColumnStatssummary(CsvDocument doc, String column) Computes a full statistical summary for a column 计算列的完整统计摘要
-
Method Details
-
count
Counts non-blank values in a column 计算列中非空白值的数量- Parameters:
doc- the document | 文档column- the column name | 列名- Returns:
- the count of non-blank values | 非空白值数量
- Throws:
NullPointerException- if any parameter is null | 如果任何参数为nullOpenCsvException- if the column is not found | 如果列未找到
-
countAll
Counts all rows in the document 计算文档中所有行的数量- Parameters:
doc- the document | 文档- Returns:
- the total row count | 总行数
- Throws:
NullPointerException- if doc is null | 如果doc为null
-
sum
Computes the sum of numeric values in a column 计算列中数值的总和Non-numeric and blank values are silently skipped. Returns
BigDecimal.ZEROif no numeric values are found.非数值和空白值被静默跳过。 如果没有找到数值,返回
BigDecimal.ZERO。- Parameters:
doc- the document | 文档column- the column name | 列名- Returns:
- the sum | 总和
- Throws:
NullPointerException- if any parameter is null | 如果任何参数为nullOpenCsvException- if the column is not found | 如果列未找到
-
avg
Computes the average of numeric values in a column 计算列中数值的平均值Non-numeric and blank values are silently skipped. Returns null if no numeric values are found. Uses HALF_UP rounding with scale of 6.
非数值和空白值被静默跳过。 如果没有找到数值,返回null。 使用HALF_UP舍入模式,精度为6。
- Parameters:
doc- the document | 文档column- the column name | 列名- Returns:
- the average, or null if no numeric values | 平均值,无数值时为null
- Throws:
NullPointerException- if any parameter is null | 如果任何参数为nullOpenCsvException- if the column is not found | 如果列未找到
-
min
Finds the minimum numeric value in a column 查找列中的最小数值Non-numeric and blank values are silently skipped. Returns null if no numeric values are found.
非数值和空白值被静默跳过。 如果没有找到数值,返回null。
- Parameters:
doc- the document | 文档column- the column name | 列名- Returns:
- the minimum value, or null if no numeric values | 最小值,无数值时为null
- Throws:
NullPointerException- if any parameter is null | 如果任何参数为nullOpenCsvException- if the column is not found | 如果列未找到
-
max
Finds the maximum numeric value in a column 查找列中的最大数值Non-numeric and blank values are silently skipped. Returns null if no numeric values are found.
非数值和空白值被静默跳过。 如果没有找到数值,返回null。
- Parameters:
doc- the document | 文档column- the column name | 列名- Returns:
- the maximum value, or null if no numeric values | 最大值,无数值时为null
- Throws:
NullPointerException- if any parameter is null | 如果任何参数为nullOpenCsvException- if the column is not found | 如果列未找到
-
distinct
Returns distinct non-null values in a column, preserving insertion order 返回列中去重的非null值,保留插入顺序- Parameters:
doc- the document | 文档column- the column name | 列名- Returns:
- unmodifiable list of distinct values | 不可修改的去重值列表
- Throws:
NullPointerException- if any parameter is null | 如果任何参数为nullOpenCsvException- if the column is not found | 如果列未找到
-
frequency
Computes frequency of each value in a column, ordered by count descending 计算列中每个值的频率,按计数降序排列- Parameters:
doc- the document | 文档column- the column name | 列名- Returns:
- unmodifiable map of value to count, sorted by count descending | 不可修改的值到计数Map,按计数降序排列
- Throws:
NullPointerException- if any parameter is null | 如果任何参数为nullOpenCsvException- if the column is not found | 如果列未找到
-
summary
Computes a full statistical summary for a column 计算列的完整统计摘要- Parameters:
doc- the document | 文档column- the column name | 列名- Returns:
- the column statistics | 列统计
- Throws:
NullPointerException- if any parameter is null | 如果任何参数为nullOpenCsvException- if the column is not found | 如果列未找到
-