Convert CSV values to a HashMap key value pairs in JAVA

前端 未结 8 1009
刺人心
刺人心 2020-12-03 03:33

HI I have a csv called test.csv . I am trying to read the csv line by line and convert the values into a hash key value pairs . Here is the code :-



        
8条回答
  •  萌比男神i
    2020-12-03 04:36

    Using FasterXML's CSV package: https://github.com/FasterXML/jackson-dataformats-text/tree/master/csv

    public static List> read(File file) throws JsonProcessingException, IOException {
        List> response = new LinkedList>();
        CsvMapper mapper = new CsvMapper();
        CsvSchema schema = CsvSchema.emptySchema().withHeader();
        MappingIterator> iterator = mapper.reader(Map.class)
                .with(schema)
                .readValues(file);
        while (iterator.hasNext()) {
            response.add(iterator.next());
        }
        return response;
    }
    

提交回复
热议问题