【问题标题】:How to read queue instead arrayList by csv-spring?如何通过csv-spring读取队列而不是arrayList?
【发布时间】:2019-12-11 11:16:03
【问题描述】:

我使用 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?

【问题讨论】:

    标签: java spring list csv queue


    【解决方案1】:
    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) 也可以返回队列..

    【讨论】:

      猜你喜欢
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-10
      • 2020-04-20
      • 2012-03-18
      相关资源
      最近更新 更多