Class CsvMerge
Provides vertical merge (row concatenation) and horizontal merge (join) operations for CSV documents. All methods are static and the class cannot be instantiated.
提供CSV文档的垂直合并(行连接)和水平合并(连接)操作。 所有方法均为静态方法,该类不可实例化。
Features | 主要功能:
- Vertical merge (concat) - appends rows from multiple documents - 垂直合并(concat)- 追加多个文档的行
- Inner join - matches rows by key column, both sides required - 内连接 - 按键列匹配行,双方都需要存在
- Left join - matches rows by key column, all left rows preserved - 左连接 - 按键列匹配行,保留所有左表行
Usage Examples | 使用示例:
CsvDocument a = CsvDocument.builder()
.header("id", "name").addRow("1", "Alice").build();
CsvDocument b = CsvDocument.builder()
.header("id", "name").addRow("2", "Bob").build();
CsvDocument merged = CsvMerge.concat(a, b); // 2 rows
CsvDocument joined = CsvMerge.innerJoin(a, b, "id"); // join by id
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 CsvDocumentconcat(CsvDocument... docs) Concatenates multiple CSV documents vertically (appends rows) 垂直连接多个CSV文档(追加行)static CsvDocumentconcat(List<CsvDocument> docs) Concatenates multiple CSV documents vertically from a list 从列表垂直连接多个CSV文档static CsvDocumentinnerJoin(CsvDocument left, CsvDocument right, String keyColumn) Performs an inner join of two CSV documents on a key column 对两个CSV文档按键列执行内连接static CsvDocumentleftJoin(CsvDocument left, CsvDocument right, String keyColumn) Performs a left join of two CSV documents on a key column 对两个CSV文档按键列执行左连接
-
Method Details
-
concat
Concatenates multiple CSV documents vertically (appends rows) 垂直连接多个CSV文档(追加行)Uses headers from the first non-empty document. Columns are aligned to the header: missing columns are filled with empty string, extra columns are ignored.
使用第一个非空文档的标题。列对齐到标题:缺少的列用空字符串填充, 多余的列被忽略。
- Parameters:
docs- the documents to concatenate | 要连接的文档- Returns:
- the concatenated document | 连接后的文档
- Throws:
NullPointerException- if docs or any element is null | 如果docs或任何元素为nullOpenCsvException- if no documents are provided | 如果没有提供文档
-
concat
Concatenates multiple CSV documents vertically from a list 从列表垂直连接多个CSV文档Uses headers from the first non-empty document. Columns are aligned to the header: missing columns are filled with empty string, extra columns are ignored.
使用第一个非空文档的标题。列对齐到标题:缺少的列用空字符串填充, 多余的列被忽略。
- Parameters:
docs- the documents to concatenate | 要连接的文档- Returns:
- the concatenated document | 连接后的文档
- Throws:
NullPointerException- if docs or any element is null | 如果docs或任何元素为nullOpenCsvException- if the list is empty | 如果列表为空
-
innerJoin
Performs an inner join of two CSV documents on a key column 对两个CSV文档按键列执行内连接Both documents must have the key column in their headers. The result contains all columns from the left document followed by all columns from the right document (excluding the key column). Only rows where the key exists in both documents are included.
两个文档的标题中都必须包含键列。 结果包含左文档的所有列,然后是右文档的所有列(排除键列)。 仅包含键在两个文档中都存在的行。
- Parameters:
left- the left document | 左文档right- the right document | 右文档keyColumn- the key column name | 键列名- Returns:
- the joined document | 连接后的文档
- Throws:
NullPointerException- if any parameter is null | 如果任何参数为nullOpenCsvException- if keyColumn is not found in either document | 如果键列在任一文档中未找到
-
leftJoin
Performs a left join of two CSV documents on a key column 对两个CSV文档按键列执行左连接Both documents must have the key column in their headers. The result contains all columns from the left document followed by all columns from the right document (excluding the key column). All left rows are included; if no match in right, right columns are filled with empty string.
两个文档的标题中都必须包含键列。 结果包含左文档的所有列,然后是右文档的所有列(排除键列)。 包含所有左表行;如果右表没有匹配,右列用空字符串填充。
- Parameters:
left- the left document | 左文档right- the right document | 右文档keyColumn- the key column name | 键列名- Returns:
- the joined document | 连接后的文档
- Throws:
NullPointerException- if any parameter is null | 如果任何参数为nullOpenCsvException- if keyColumn is not found in either document | 如果键列在任一文档中未找到
-