如何使用 lambda 简化多个 ifs 语句
How to simplify multiple ifs statements with lambda
如何简化此示例代码以使其更优雅,例如,如果可能,采用函数式样式。
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;
}
lambda 有什么用?
@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);
}
'key' class 需要功能性的 equals 和 hashCode 实现,这里用 lombok's @Value
.
完成
MAP 也可以从输入文本文件构建(如果您愿意,可以使用 getResourceAsStream
和静态初始值设定项。
您可以使用分隔符组合字符串,然后使用 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 "";
}
}
如果您的值数量有限,地图也可以作为替代方案:
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, "");
}
如何简化此示例代码以使其更优雅,例如,如果可能,采用函数式样式。
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;
}
lambda 有什么用?
@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);
}
'key' class 需要功能性的 equals 和 hashCode 实现,这里用 lombok's @Value
.
MAP 也可以从输入文本文件构建(如果您愿意,可以使用 getResourceAsStream
和静态初始值设定项。
您可以使用分隔符组合字符串,然后使用 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 "";
}
}
如果您的值数量有限,地图也可以作为替代方案:
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, "");
}