gpt4 book ai didi

java - 如何通过csv-spring读取队列而不是arrayList?

转载 作者:行者123 更新时间:2023-12-01 19:28:27 28 4
gpt4 key购买 nike

我使用 spring-csv 来读取 csv 文件,就像这里的示例

examples

对于这个目标,我创建了这个方法

 public static <T> List<T> loadObjectList(Class<T> type, File file) {
try {
CsvSchema bootstrapSchema = CsvSchema.emptySchema().withHeader().withColumnSeparator('$');
CsvMapper mapper = new CsvMapper();
MappingIterator<T> readValues = mapper
.reader(type)
.with(bootstrapSchema).read
.readValues(file);
List<T> values = readValues.readAll();

return values;
} catch (Exception e) {
System.err.format("Error occurred while loading object list from file %s", file.getName(), e);
return Collections.emptyList();
}
}

对于具有 50-60k 记录行的文件,它可以正常工作。 但是,现在我需要读取包含 1100 万条记录的文件并在任何线程中处理它。对于线程来说最好使用Queues,那么,如何将该方法转换为返回Queues而不是ArrayList呢?

最佳答案

public static <T> ConcurrentLinkedQueue<T> loadObjectList(Class<T> type, File file) {
try {
CsvSchema bootstrapSchema = CsvSchema.emptySchema().withHeader().withColumnSeparator('$');
CsvMapper mapper = new CsvMapper();
MappingIterator<T> readValues = mapper
.reader(type)
.with(bootstrapSchema)
.readValues(file);

ConcurrentLinkedQueue<T> queue = new ConcurrentLinkedQueue();
readValues.readAll(queue);

return queue;
} catch (Exception e) {
System.err.format("Error occurred while loading object list from file %s", file.getName(), e);
return new ConcurrentLinkedQueue();
}
}

readAll(Queue q) 也可以返回队列..

关于java - 如何通过csv-spring读取队列而不是arrayList?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59284595/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com