Skip to content

Commit 8e4b51a

Browse files
author
Rajeev Kumar Singh
committed
Memory Optimizations
1 parent 7064c42 commit 8e4b51a

File tree

6 files changed

+40
-14
lines changed

6 files changed

+40
-14
lines changed

java-csv-file-handling-with-apache-commons-csv/src/main/java/BasicCSVReader.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ public static void main(String[] args) throws IOException {
1818
Reader reader = Files.newBufferedReader(Paths.get(SAMPLE_CSV_FILE_PATH));
1919
CSVParser csvParser = new CSVParser(reader, CSVFormat.DEFAULT);
2020
) {
21-
Iterable<CSVRecord> csvRecords = csvParser.getRecords();
22-
23-
for (CSVRecord csvRecord : csvRecords) {
21+
for (CSVRecord csvRecord : csvParser) {
2422
// Accessing Values by Column Index
2523

2624
String name = csvRecord.get(0);

java-csv-file-handling-with-apache-commons-csv/src/main/java/CSVReaderWithHeaderAutoDetection.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ public static void main(String[] args) throws IOException {
2222
.withIgnoreHeaderCase()
2323
.withTrim());
2424
) {
25-
Iterable<CSVRecord> csvRecords = csvParser.getRecords();
26-
27-
for (CSVRecord csvRecord : csvRecords) {
25+
for (CSVRecord csvRecord : csvParser) {
2826
// Accessing values by Header names
2927

3028
String name = csvRecord.get("Name");

java-csv-file-handling-with-apache-commons-csv/src/main/java/CSVReaderWithManualHeader.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static void main(String[] args) throws IOException {
2323
.withTrim());
2424
) {
2525

26-
for (CSVRecord csvRecord : csvParser.getRecords()) {
26+
for (CSVRecord csvRecord : csvParser) {
2727
// Accessing values by the names assigned to each column
2828

2929
String name = csvRecord.get("Name");

java-csv-file-handling-with-opencsv/src/main/java/OpenCSVParseToBeanWithoutAnnotation.java

+18-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.io.Reader;
77
import java.nio.file.Files;
88
import java.nio.file.Paths;
9+
import java.util.Iterator;
910
import java.util.List;
1011

1112
/**
@@ -24,15 +25,16 @@ public static void main(String[] args) throws IOException {
2425
String[] memberFieldsToBindTo = {"name", "email", "phoneNo", "country"};
2526
strategy.setColumnMapping(memberFieldsToBindTo);
2627

27-
CsvToBean csvToBean = new CsvToBeanBuilder(reader)
28+
CsvToBean<MyUser> csvToBean = new CsvToBeanBuilder(reader)
2829
.withMappingStrategy(strategy)
2930
.withSkipLines(1)
3031
.withIgnoreLeadingWhiteSpace(true)
3132
.build();
3233

33-
List<MyUser> myUsers = csvToBean.parse();
34+
Iterator<MyUser> myUserIterator = csvToBean.iterator();
3435

35-
for (MyUser myUser : myUsers) {
36+
while (myUserIterator.hasNext()) {
37+
MyUser myUser = myUserIterator.next();
3638
System.out.println("Name : " + myUser.getName());
3739
System.out.println("Email : " + myUser.getEmail());
3840
System.out.println("PhoneNo : " + myUser.getPhoneNo());
@@ -41,6 +43,19 @@ public static void main(String[] args) throws IOException {
4143
}
4244
}
4345
}
46+
47+
// Reads all CSV contents into memory (Not suitable for large CSV files)
48+
private static void readAllBeansAtOnce(CsvToBean csvToBean) {
49+
List<MyUser> myUsers = csvToBean.parse();
50+
51+
for (MyUser myUser : myUsers) {
52+
System.out.println("Name : " + myUser.getName());
53+
System.out.println("Email : " + myUser.getEmail());
54+
System.out.println("PhoneNo : " + myUser.getPhoneNo());
55+
System.out.println("Country : " + myUser.getCountry());
56+
System.out.println("---------------------------");
57+
}
58+
}
4459
}
4560

4661

java-csv-file-handling-with-opencsv/src/main/java/OpenCSVReadAndParseToBean.java

+18-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.io.Reader;
66
import java.nio.file.Files;
77
import java.nio.file.Paths;
8+
import java.util.Iterator;
89
import java.util.List;
910

1011
/**
@@ -18,14 +19,15 @@ public static void main(String[] args) throws IOException {
1819
try (
1920
Reader reader = Files.newBufferedReader(Paths.get(SAMPLE_CSV_FILE_PATH));
2021
) {
21-
CsvToBean csvToBean = new CsvToBeanBuilder(reader)
22+
CsvToBean<CSVUser> csvToBean = new CsvToBeanBuilder(reader)
2223
.withType(CSVUser.class)
2324
.withIgnoreLeadingWhiteSpace(true)
2425
.build();
2526

26-
List<CSVUser> csvUsers = csvToBean.parse();
27+
Iterator<CSVUser> csvUserIterator = csvToBean.iterator();
2728

28-
for (CSVUser csvUser : csvUsers) {
29+
while (csvUserIterator.hasNext()) {
30+
CSVUser csvUser = csvUserIterator.next();
2931
System.out.println("Name : " + csvUser.getName());
3032
System.out.println("Email : " + csvUser.getEmail());
3133
System.out.println("PhoneNo : " + csvUser.getPhoneNo());
@@ -34,6 +36,19 @@ public static void main(String[] args) throws IOException {
3436
}
3537
}
3638
}
39+
40+
// Reads all CSV contents into memory (Not suitable for large CSV files)
41+
private static void readAllBeansAtOnce(CsvToBean csvToBean) {
42+
List<CSVUser> csvUsers = csvToBean.parse();
43+
44+
for(CSVUser csvUser: csvUsers) {
45+
System.out.println("Name : " + csvUser.getName());
46+
System.out.println("Email : " + csvUser.getEmail());
47+
System.out.println("PhoneNo : " + csvUser.getPhoneNo());
48+
System.out.println("Country : " + csvUser.getCountry());
49+
System.out.println("==========================");
50+
}
51+
}
3752
}
3853

3954

java-csv-file-handling-with-opencsv/src/main/java/OpenCSVWriter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ private static void writeFromListOfObjects() throws IOException,
5252
try (
5353
Writer writer = Files.newBufferedWriter(Paths.get(STRING_ARRAY_SAMPLE));
5454
) {
55-
StatefulBeanToCsv beanToCsv = new StatefulBeanToCsvBuilder(writer)
55+
StatefulBeanToCsv<MyUser> beanToCsv = new StatefulBeanToCsvBuilder(writer)
5656
.withQuotechar(CSVWriter.NO_QUOTE_CHARACTER)
5757
.build();
5858

0 commit comments

Comments
 (0)