如何将充满if语句的for循环更改为更优雅/高效的代码



这是我的字符串:

String field = "first=true, second=true"

我的方法对这个字符串进行操作,并识别它是否包含first=second=子字符串,如果是,则基于后面的true/false调用其他方法。然而,第一子串和第二子串可以是可选的。这就是我目前所拥有的:

void method(String field) {
      String[] splittedField = field.split(", ");
      for (String substring : splittedField) {
          if (substring.contains("first") {
              if (substring.contains("true") {
                  otherMethod("first", "true");
              } else if (substring.contains("false") {
                  otherMethod("first", "false");
              }
          } else if (substring.contains("second") {
              if (substring.contains("true") {
                  otherMethod("second", "true");
              } else if (substring.contains("false") {
                  otherMethod("second", "false");
              }
          }
      }
}

但也许有一种更好/更有效(更优雅?(的方法来解决这个问题?

考虑:

if (substring.contains("first") {
    if (substring.contains("true") {
        otherMethod("first", "true");
    } else if (substring.contains("false") {
        otherMethod("first", "false");
    }
 } 

以上if可编码为:

if (substring.contains("first") {
    String[] valueString = substring.split("=");            
    otherMethod("first", valueString[1]);
 }

您可以简单地按如下方式执行:

String[] splittedField = field.split(", ");
for (String substring : splittedField) {
    String[] parts = substring.split("=");
    otherMethod(parts[0], parts[1]);
}

您不需要所有的if语句。

对于检查,您可以创建几个集合,对于调用该方法,您可以通过=:再次拆分每个子字符串

void method(String field) {
    Set<String> firstSecond = Set.of("first", "second");
    Set<String> trueFalse = Set.of("true", "false");
    String[] splittedField = field.split(", ");
    for (String substring : splittedField) {
        String[] args = substring.split("=");
        if (firstSecond.contains(args[0]) && trueFalse.contains(args[1])) {
            otherMethod(args[0], args[1]);
        }
    }
}

注意:如果你还没有使用Java9+,你可以重写Set.of如下:

Set<String> firstSecond = new HashSet<>(Arrays.asList("first", "second"));

根据OP的问题和他的评论,我认为这就足够了?:

void method(String field) {
      Arrays.stream(field.split(", "))
            .forEach(v -> {
                 String[] args = v.split("=");
                 otherMethod(args[0], args[1]);
            })
}

这里是代码的改进版本。你应该记住的一些要点。1.您可以直接调用otherMethod中的contains方法,因为它也需要布尔值。

void method(String field) {
  String[] splittedField = field.split(", ");
  for (String substring : splittedField) {
    if (substring.contains("first") {
        otherMethod("first", substring.contains("true"));
    } else if (substring.contains("second") {
        otherMethod("second", substring.contains("true"));
    }
  }
}

在使用Stream的解决方案中,您可以执行以下操作:

void invokeOtherMethod(String field) {
    Map<String, Boolean> mapFromString = Arrays.stream(field.split(","))
            .map(s -> s.trim().split("="))
            .collect(Collectors.toMap(a -> a[0], a -> Boolean.valueOf(a[1])));
    for (Map.Entry<String, Boolean> entry : mapFromString.entrySet()) {
        otherMethod(entry.getKey(), entry.getValue());
    }
}

注意:有意使用的otherMethod签名的更改:

void otherMethod(String key, boolean value)

我认为这样做可以消除复杂的if结构。

public class Main {
    public static void method(String field) {
        int i = 0;
        // Make the flags false by default, 
        // in case you don't supply either or both of them in field.
        String[] flags = {"false", "false"};
        String[] splittedField = field.split(", ");
        for (String substring : splittedField) {
            String[] args = substring.split("=");
            flags[i] = args[1];
            ++i;
        }
        othermethod(flags[0], flags[1]);
    }
    public static void main(String[] args) {
            String field = "first=true, second=false";
            method(field);
    }
}

最新更新