简体   繁体   中英

How to convert CSVRecord to List?

I need to convert a CSVRecord to a List to get the position of an element.

So I used this:

List<CSVRecord> records = csvParser.getRecords();
int i = records.get(0).indexOf(element);

However, records.get(0) returns a List<CSVRecord> and not a List<String> ...

Moreover I cannot use csvParser.getHeaderNames(); because after that I need to copy the file with the header.

Given the below function that I found on geeksforgeeks.com

public static <T> List<T> getListFromIterator(Iterator<T> iterator) { 
    Iterable<T> iterable = () -> iterator; 

    return StreamSupport 
              .stream(iterable.spliterator(), false) 
              .collect(Collectors.toList()); 
} 

and the fact that you can get an iterator from CVSRecords you can do

List<String> list = getListFromIterator(records.get(0).iterator());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM