I have a text file that has text on each line, for example:
1245 Dog Husky
2356 Cat Tabby
3476 Dog Pug
with a huge amount of kind of arbitrary data repeated per line, about 10,000 lines, for the sake of argument, lets say it tends to infinity.
I have code that reads this data and stores it in an object, pseudo code follows;
Pet P;
lineInput = reader.readLine(); //where reader is reading the above mentionedfile
P.id = lineInput.split('\t')[0]
P.type = lineInput.split('\t')[1] //Assigning the parts of the line to it's relevant data members
P.breed = lineInput.split('\t')[2]
Now here's the problem, considering I need to be able to sort, search and display these values as fast as possible, I don't know what my best option is, I came up with two methods which can be seen below
Method 1: Store all the Objects in an array list based on their starting id number
ArrayList<Pet> idStartsWith1;
if(P.id starts with 1)
idStartsWith1.add(P); // "1245 Dog Husky" will be added here
ArrayList<Pet> idStartsWith2;
if(P.id starts with 2)
idStartsWith2.add(P); // "2356 Cat Tabby" will be added here
ArrayList<Pet> idStartsWith3;
if(P.id starts with 3)
idStartsWith3.add(P); // "3476 Dog Pug" will be added here
I think this would be the faster method, as these arraylists are already in the process memory, but I fear that it would overload the memory, and cause issues. (Remember, the number of lines in the text file tends to infinite)
Method 2:Write all the Objects to a .dat file based on their starting id number
Writer writer1 = new Writer("idStartsWith1.dat"); //writer1 will write to file "idStartsWith1.dat"
if(P.id starts with 1)
writer1.write(P); // "1245 Dog Husky" will be writen to this file
Writer writer2 = new Writer("idStartsWith2.dat"); //writer2 will write to file "idStartsWith2.dat"
if(P.id starts with 2)
writer2.write(P);
Writer writer3 = new Writer("idStartsWith3.dat"); //writer3 will write to file "idStartsWith3.dat"
if(P.id starts with 3)
writer3.write(P);
This will prevent the process memory from being overloaded but I fear that having to open, then read, then close the file each time I need to search and display a Pet, will add significant delays to the runtime.
Which of these two methods would work better? or is there another more efficient method that would not occur to a java novice like me?