Is performance gained when using continue in a for-loop with many if-statements?

后端 未结 4 1648
小蘑菇
小蘑菇 2021-01-19 11:12

I have a for loop in a java program which iterates through a set of maps.

Inside the loop I have around 10 different if-statements which checks the name of each key

4条回答
  •  无人共我
    2021-01-19 11:23

    If you want the current iteration to end after the first condition evaluates to true, you should use if-else-if-...-else. In my opinion, that's more clear than using continue, since that's what this syntax exists for.

    for ( map : object.entrySet()) {
      if (map.getKey().equals.("something") {
        do_something;  
      }
      else if (map.getKey().equals.("something_else") {
        do_something_else;
      }
      else if (...) {
          ...
      }
      ... else {
          ...
      }
    }
    

提交回复
热议问题