0

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

10
  • Why do you not want to use if/else, when it's probably the most efficient approach, and not particularly complex or obscure? Commented Aug 6, 2014 at 21:34
  • 2
    @OP read my comment: split the current line by = or first white space Commented Aug 6, 2014 at 21:39
  • 1
    @prashantthakre lots of if-else don't reduce performance. What reduce performance is what you evaluate inside the tons of if-else conditions. But since we don't know if the behavior per if follows a common pattern, then we cannot provide a useful code refactoring. Commented Aug 6, 2014 at 21:41
  • 1
    As per best code practices developers should always avoid any list of supposed "best code practices". I've been programming 40 years, 17 with Java, and there is nothing wrong with the above cascading if/else statements, within reason. If it gets to more than 8 or 10 levels you might want to switch to a loop and array approach, but that would be for maintainability, not efficiency. Commented Aug 6, 2014 at 21:42
  • 1
    Of course, whether you should be using contains vs startsWith, or parsing the line into parts is another question, but orthogonal to the cascading if/else issue. Commented Aug 6, 2014 at 21:45

1 Answer 1

2

I suggest you define a Map whose keys are "METRO", "TOYOTA", etc... and its values are the methods you wish to execute for each key. The methods can be represented by a functional interface (even if you are not using Java 8).

Now, in order to use the map, you'll need a way to extract the key of a given line, so it would be best if each line would start with the key followed by some delimiter (Example : TOYOTA,field2,field3,...).

Example :

public interface ProcessLineInterface
{
    public void processLine (String line);
}

Map<String, ProcessLineInterface> map = new HashMap<String, Runnable>();

map.put ("METRO", new ProcessLineInterface () {
           public void processLine (String line)
           {
               myPrivateMethodToHandleThisLine1(line);
           }
         });
map.put ("TOYOTA", new ProcessLineInterface () {
           public void processLine (String line)
           {
               myPrivateMethodToHandleThisLine2(line);
           }
         });
map.put (...);
...

while ((line = br.readLine()) != null) 
{
    ProcessLineInterface pline = map.get(extractKey(line));
    if (pline != null) {
        pline.processLine(line);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I usually recommend doing this refactor but seems like more tedious code to work with and doesn't really improve the performance that much in this case. Reduce from 0.017ms to 0.001ms seems more like a micro optimization (and we're not counting on the JIT bytecode optimization for this code).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.