如何使用 lambda 简化多个 if 语句。

huangapple go评论72阅读模式
英文:

How to simplify multiple ifs statements with lambda

问题

private static String resolveTypes(String messageType, String accountType) {
    if (messageType.equals("PDF") && accountType.equals("NEXT")) {
        return "2015";
    }
    if (messageType.equals("CSV") && accountType.equals("NEXT")) {
        return "2016";
    }
    if (messageType.equals("CSV") && accountType.equals("BEFORE")) {
        return "2017";
    }
    if (messageType.equals("PDF") && accountType.equals("BEFORE")) {
        return "2018";
    }
    return "";
}
英文:

How can I simplify this example code to be more elegant, for example in functional style if it possible.

  private static String resolveTypes(String messageType, String accountType) {
    String result = "";
    if(messageType.equals("PDF") && accountType.equals("NEXT")){
      result = "2015";
    }
    if(messageType.equals("CSV") && accountType.equals("NEXT")){
      result = "2016";
    }
    if(messageType.equals("CSV") && accountType.equals("BEFORE")){
      result = "2017";
    }
    if(messageType.equals("PDF") && accountType.equals("BEFORE")){
      result = "2018";
    }
    return result;
  }

答案1

得分: 1

@Value
public static class MessageAccountKey {
   // this class needs a better name;
   // you'd know better what it represents
   String messageType, accountType;
}

private static final Map<MessageAccountKey, Integer> MAP = Map.of(
    new MessageAccountKey("PDF", "NEXT"), 2015,
    new MessageAccountKey("CSV", "NEXT"), 2016,
    new MessageAccountKey("CSV", "BEFORE"), 2017,
    new MessageAccountKey("PDF", "BEFORE"), 2018);

private static int resolveType(String messageType, String accountType) {
 // consider changing to a single arg of type Key.
  return MAP.getOrDefault(new MessageAccountKey(messageType, accountType), 0);
}

The 'key' class needs functional equals and hashCode impls, here done with lombok's @Value.

The MAP can also be built up from an input text file (use getResourceAsStream and a static initializer if you prefer.

英文:

How would lambdas help at all?

@Value
public static class MessageAccountKey {
   // this class needs a better name;
   // you&#39;d know better what it represents
   String messageType, accountType;
}

private static final Map&lt;MessageAccountKey, Integer&gt; MAP = Map.of(
    new MessageAccountKey(&quot;PDF&quot;, &quot;NEXT&quot;), 2015,
    new MessageAccountKey(&quot;CSV&quot;, &quot;NEXT&quot;), 2016,
    new MessageAccountKey(&quot;CSV&quot;, &quot;BEFORE&quot;), 2017,
    new MessageAccountKey(&quot;PDF&quot;, &quot;BEFORE&quot;), 2018);

private static int resolveType(String messageType, String accountType) {
 // consider changing to a single arg of type Key.
  return MAP.getOrDefault(new MessageAccountKey(messageType, accountType), 0);
}

The 'key' class needs functional equals and hashCode impls, here done with lombok's @Value.

The MAP can also be built up from an input text file (use getResourceAsStream and a static initializer if you prefer.

答案2

得分: 1

如果您有一组有限的值,映射也可以是一种替代方法:

private static String resolveTypes(String messageType, String accountType) {
    Map<String, Map<String, String>> map = Map.of(
        "PDF", Map.of("NEXT", "2015", "BEFORE", "2016"),
        "CSV", Map.of("NEXT", "2017", "BEFORE", "2018"));
    return map.getOrDefault(messageType, new HashMap<>()).getOrDefault(accountType, "");
}
英文:

If you have a limited number of values a map could also be an alternative:

private static String resolveTypes(String messageType, String accountType) {
    Map&lt;String,Map&lt;String,String&gt;&gt; map = Map.of( 
            &quot;PDF&quot;,Map.of(&quot;NEXT&quot;, &quot;2015&quot;, &quot;BEFORE&quot;,&quot;2016&quot;),
            &quot;CSV&quot;,Map.of(&quot;NEXT&quot;, &quot;2017&quot;, &quot;BEFORE&quot;,&quot;2018&quot;));
    return map.getOrDefault(messageType, new HashMap&lt;&gt;()).getOrDefault(accountType, &quot;&quot;);
}

答案3

得分: 0

你可以将这些字符串组合起来,用一个分隔符,然后使用一个 switch 语句:

private static String resolveTypes(String messageType, String accountType) {
	switch (messageType + ':' + accountType) {
		case "PDF:NEXT": return "2015";
		case "CSV:NEXT": return "2016";
		case "CSV:BEFORE": return "2017";
		case "PDF:BEFORE": return "2018";
		default: return "";
	}
}
英文:

You could combine the strings, with a separator, then use a switch statement:

private static String resolveTypes(String messageType, String accountType) {
	switch (messageType + &#39;:&#39; + accountType) {
		case &quot;PDF:NEXT&quot;: return &quot;2015&quot;;
		case &quot;CSV:NEXT&quot;: return &quot;2016&quot;;
		case &quot;CSV:BEFORE&quot;: return &quot;2017&quot;;
		case &quot;PDF:BEFORE&quot;: return &quot;2018&quot;;
		default: return &quot;&quot;;
	}
}

huangapple
  • 本文由 发表于 2020年10月13日 22:32:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/64337271.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定