What is the best approach for sorting 2 large text files in Java?

I am creating a simple java application that includes reading information from a csv file. The information in the csv file goes to this form:

"ID","Description"
"AB","Some sort of information for AB"
"AC","Some sort of information for AC"

I must allow the user to print the description, identifier, or both, in a console sorted by identifier. The simplest solution would be to parse the files using a CSV library, such as opencsv, and put this line in TreeMap and print the contents of TreeMap. The key in TreeMap will be the identifier, and the value will be the description.

However, the CSV file can be huge. It can be 5 GB, and loading 5 GB of rows in TreeMap will result in an error from memory. To handle large files, I could sort the files using the merge look. Once I get a sorted file, I can print the contents of the file in the console just by reading the file.

The appearance of the merge will definitely be much slower than loading the contents of the file into TreeMap. I am considering determining file size. If the file size is larger than the available memory, I will use the merge look. Otherwise, I upload the contents of the file to TreeMap.

, , 2 . , . , , csv csv . , ?

?

.

+5
2

csv , ID TreeMap, . RandomAccessFile, . , MapDB. TreeMap, .

+3

/, . , , , 2 : .

, : , , , ? - , , :) - , .

, : , 1 ..

+1

All Articles