My text file has this:
Apple IS A fruit
BANABA=fruit
HERO=fruit
TOYOTA 784YUT
USAIRWAYS canada
METROBUS=newyork
TOYOTA 784YUT is the only text in that line.
FileReader file = new FileReader("C:\\FILEREADER\\MockupData.txt");
BufferedReader br = new BufferedReader(file);
String line;
try {
while ((line = br.readLine()) != null) {
if (line.contains("METRO")) {
String Id = myPrivateMethodToHandleThisLine1(line);
//do something with this id.
} else if (line.contains("TOYOTA")) {
String Id2 = myPrivateMethodToHandleThisLine2(line);
//do something with this id.
} else if (line.contains("HERO")) {
String Id3 = myPrivateMethodToHandleThisLine3(line);
//do something with this id. } .
.
.(some more if/else conditions)
.
.
.
} catch (IOException e) {
e.printStackTrace();
}
.....remaing code.
My Problem is simple, I am reading text from a file line by line. Based on the input line I get, I'll call handling method.
I want to implement this code best in performance. I don't want to keep multiple if/else conditions. Need suggestions.
NOT USING JAVA 1.7
=or first white spaceif-elsedon't reduce performance. What reduce performance is what you evaluate inside the tons ofif-elseconditions. But since we don't know if the behavior per if follows a common pattern, then we cannot provide a useful code refactoring.containsvsstartsWith, or parsing the line into parts is another question, but orthogonal to the cascading if/else issue.