Mike
Mike

Reputation: 755

JAVA: datastructure like a table without using a database?

I have a couple of files. Each file is for a month. e.g.

file-januar.csv

ID, From, To
1234, 2022-01-01, 2022-01-02
1235, 2022-07-01, 2022-08-20

file-februar.csv

ID, From, To
1234, 2022-01-01, 2022-01-02
1235, 2022-08-21, 2022-08-30

file-march.csv ...

The id is unique in each file. If the record did not change in the file for january and february is the same entry:

1234, 2022-01-01, 2022-01-02

If the record changes the entry for januar and februar are different

1235, 2022-07-01, 2022-08-20
1235, 2022-08-21, 2022-08-30

I need to create a single file without any duplicates in chronological order. My problem: I can not use a database.

Has sombody an idea howto create a single file januar-dezember without any duplicates? Each file has about 10.000 rows. How can i handle it? And how can i sort it chronological?

Upvotes: 1

Views: 49

Answers (1)

cyberbrain
cyberbrain

Reputation: 5135

I would store each line in a Map<Integer, String> where the key is the unique ID (I suppose it is a number, but you also could use a string) and the value is the complete line.

Then read in each file in chronological order and store it into the map. Entries from later files will overwrite entries from earlier ones.

In the end you can write out the Map like this:

Set<Integer> keys = allValuesMap.keySet();
List<Integer> keyList = new ArrayList<>(keys);
Collections.sort(keyList);
for (Integer key: keyList) {
  System.out.println(allValuesMap.get(key));
}

(Of course you would probably replace the System.out with another file.)

If that doesn't work, you could try to use an in-memory database like H2.

Upvotes: 1

Related Questions