CSV to List Java10 Sept 2024 | 4 min read 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 FileReaderOne 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 LinkedListIn 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 FilesThe 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. Next TopicDifference Between Java Servlets and CGI |
Java is the developer's first choice to write code. It is a very popular and successful programming language to build applications. The count of the Java developer is increased day by day. It is mostly used to develop web and mobile applications. In order to become...
5 min read
In this section, we will learn how to create a mini-application for a banking system in Java. In this program, we will add some basic functionalities of a bank account like a deposit of amount, withdrawal of amount, etc. Initially, the program accepts the number of customers...
10 min read
In Java, the Character.isSurrogate() function determines if a specified character is a surrogate character. Surrogate pairs in UTF-16 encoding represent characters that are beyond the Basic Multilingual Plane (BMP). These pairs consist of high surrogates and low surrogates. A character is deemed a surrogate code unit if...
5 min read
In Java, the Callable interface was introduced in Java 5 as an alternative to existing Runnable interface. It wraps a task and pass it to a Thread or thread pool for asynchronous execution. The Callable represents an asynchronous computation, whose value is available through a Future...
4 min read
Hamming code is a special code in a Computer network, which is a set of error-correction codes. In Computer Graphics, it is mainly used for detecting and correcting errors that occur at the time of data transmission from sender to receiver. In Java, we can implement...
6 min read
A doubly linked list is a data structure where each node contains two references, one pointing to the ious node and another pointing to the node. This allows for efficient traversal in both directions. Inserting a node in a doubly linked list involves updating the...
8 min read
Java offers several ways to iterate over collections such as arrays, lists, sets, and maps. Two of the most commonly used methods are Iterator and foreach. Understanding the differences between these two methods is essential for writing efficient and readable Java code. Iterator The Iterator interface in...
4 min read
Finding the third maximum number in an array is a common problem in coding interviews and competitive programming. The problem can be approached in multiple ways, each with its own trade-offs in terms of time and space complexities. In this section, we will explore three...
6 min read
In the world of programming, manipulating arrays is a fundamental skill. An array can be shuffled, which includes randomly rearranging its elements, as one common process. This procedure is essential for things like building randomized gaming decks, running statistical simulations, or just displaying data more randomly....
5 min read
Java, a versatile and widely-used programming language, boasts a plethora of features that contribute to its popularity among developers. However, as with any sophisticated tool, it comes with its own set of challenges. One of these challenges is ambiguity - a concept that can leave even...
4 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India