Class CsvSupport.LineSplitter

java.lang.Object
io.jenetics.ext.util.CsvSupport.LineSplitter
Enclosing class:
CsvSupport

public static final class CsvSupport.LineSplitter extends Object
Splitting a CSV line into columns (records).

Examples

Simple usage
final var reader = new LineReader();
final var splitter = new LineSplitter(new Separator(','));
final List<String[]> rows;
try (Stream<String> lines = reader.read(new StringReader(csv))) {
	rows = lines
		.map(splitter::split)
		.toList();
}
Projecting and re-ordering columns
final var csv = """
	Country,City,AccentCity,Region,Population,Latitude,Longitude
	ad,aixas,AixĂ s,06,,42.4833333,1.4666667
	ad,aixirivali,Aixirivali,06,,42.4666667,1.5
	ad,aixirivall,Aixirivall,06,,42.4666667,1.5
	ad,aixirvall,Aixirvall,06,,42.4666667,1.5
	ad,aixovall,Aixovall,06,,42.4666667,1.4833333
	""";

final var reader = new LineReader();

// Only read three columns, in the specified order.
final var projection = new ColumnIndexes(
	// Read 'Region' as first column.
	3,
	// Read 'City' as second column.
	1,
	// Read 'Country' as third column.
	0
);

// Configure the splitter with default separator and quote character,
// and make it return only the specified columns in the defined order.
final var splitter = new LineSplitter(projection);

try (Stream<String> lines = reader.read(new StringReader(csv))) {
	final var result = lines
		.map(splitter::split)
		.map(Arrays::toString)
		.collect(Collectors.joining("\n", "", "\n"));

	assert result.equals("""
		[Region, City, Country]
		[06, aixas, ad]
		[06, aixirivali, ad]
		[06, aixirivall, ad]
		[06, aixirvall, ad]
		[06, aixovall, ad]
		"""
	);
}
Since:
8.1
Version:
8.1
API Note:
A line splitter ist not thread-safe and can't be shared between different threads.