Javatpoint Logo
Javatpoint Logo

CSV to List Java

Tabular data can be stored in a popular format called Comma-Separated Values (CSV). But sometimes, we need to convert this CSV data into List form. In order to achieve the same Java provides various method to convert CSV data into List form. In this section, we will discuss how to covert CSV data into List form.

1. Using BufferedReader and FileReader

One of the simplest ways to read a CSV file is by using BufferedReader in combination with FileReader. The method uses the CSVReader class and the OpenCSV library to transform CSV files into lists in Java. The required imports, CSVReader, FileReader, ArrayList, and List, make it easier to integrate key features. First, the CSVReader must be instantiated.

CSVReader.java

Output:

679096 2017-01-02 16:43:49.237940 treatment new_page 0 
691699 2017-01-09 23:42:35.963486 treatment new_page 0 
807595 2017-01-22 10:43:09.285426 treatment new_page 0 
924816 2017-01-20 10:59:03.481635 control old_page 0 
846225 2017-01-16 15:24:46.705903 treatment new_page 0 
740310 2017-01-10 17:22:19.762612 control old_page 0

ab_data.csv

679096,2017-01-02 16:43:49.237940,treatment,new_page,0
691699,2017-01-09 23:42:35.963486,treatment,new_page,0
807595,2017-01-22 10:43:09.285426,treatment,new_page,0
924816,2017-01-20 10:59:03.481635,control,old_page,0
846225,2017-01-16 15:24:46.705903,treatment,new_page,0
740310,2017-01-10 17:22:19.762612,control,old_page,0

Explanation

To efficiently read the contents of the file, utilize BufferedReader. To establish a connection with the CSV file, utilize FileReader. The values are stored in a ListString[]> by the readCSV() method that reads every line of the CSV file and separates it using commas as delimiters. The main() method walks you through how to process the data and use the readCSV() method.

2. Utilizing Scanner and LinkedList

In this method, we effectively transform CSV data into lists in Java by utilizing a LinkedList and the Scanner class. To handle file input and store the data structure, import Scanner, File, FileNotFoundException, LinkedList, and List. Starting the procedure involves passing a File initialized with the path to the CSV file to establish a Scanner instance. The translated CSV data is then dynamically stored in a LinkedList called csvData.

CSVToListConverter.java

Output:

679096	2017-01-02 16:43:49.237940	treatment	new_page	0	
691699	2017-01-09 23:42:35.963486	treatment	new_page	0	
807595	2017-01-22 10:43:09.285426	treatment	new_page	0	
924816	2017-01-20 10:59:03.481635	control	old_page	0	
846225	2017-01-16 15:24:46.705903	treatment	new_page	0	
740310	2017-01-10 17:22:19.762612	control	old_page	0

Explanation

The convertUsingScanner() function accepts a file path as input, uses Scanner to read the CSV file, and outputs a LinkedList with string arrays for each row.

The CSV file path is sent to convertUsingScanner() in the main() method that prints the resultant list.

3. Using Stream API and Files

The method reads and parses a CSV file using the Files class and the Stream API, which were introduced in Java 8. The file path and delimiter are arguments that are accepted by the convertUsingStreamAPI() method. It creates a Stream of lines by reading every line from the CSV file using the Files.lines() method. Subsequently, each line is divided into a string array according to the designated delimiter using the mapping procedure. The outcomes are finally compiled into a List of string arrays by the collect procedure.

CSVToListConverter.java

Output:

679096	2017-01-02 16:43:49.237940	treatment	new_page	0	
691699	2017-01-09 23:42:35.963486	treatment	new_page	0	
807595	2017-01-22 10:43:09.285426	treatment	new_page	0	
924816	2017-01-20 10:59:03.481635	control	old_page	0	
846225	2017-01-16 15:24:46.705903	treatment	new_page	0	
740310	2017-01-10 17:22:19.762612	control	old_page	0

Explanation

To open a Stream of lines from the given file path (Paths.get(filePath)), use the Files.lines() function. The map operation transforms each line by being split using the split(delimiter) method into a string array. After that, a List is created from the resultant Stream of string arrays by using Collectors.toList()'s collect function.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA