如果在 java8 中如何转换 else
how to convert else if in java8
如何更改以下代码以删除 if-else 并使用 Java8
List<String> list1;
List<String> list2;
List<String> list3;
String str;
if(list1.contains(str)){
event.getMessage().setInvocationProperty("ABC","ABC1");
}
else if(list2.contains(str)){
event.getMessage().setInvocationProperty("ABC2","ABC3");
}
else if(list3.contains(str)){
event.getMessage().setInvocationProperty("ABC4","ABC5");
}
不用 if-else
也可以做到,但对于这种情况,if-else
仍然比使用流更好。
List<String> list1;
List<String> list2;
List<String> list3;
String str;
Map<List<String>, List<Param>> paramMap = new HashMap<>();
paramMap.put(list1,List.of(ABC,ABC1));
paramMap.put(list2,List.of(ABC2,ABC3));
paramMap.put(list3,List.of(ABC4,ABC5));
List.of(list1,list2,list3)
.stream()
.filter(list -> list.contains(str))
.findFirst()
.ifPresent(list -> event.getMessage().setInvocationProperty(paramMap.get(list).get(0),paramMap.get(list).get(1)));
不使用列表作为 paramMap
中的键的另一种解决方案:
Map<Integer, List<Param>> paramMap = new HashMap<>();
paramMap.put(1,List.of(ABC,ABC1));
paramMap.put(2,List.of(ABC2,ABC3));
paramMap.put(3,List.of(ABC4,ABC5));
List<List<String>> lists = List.of(list1,list2,list3);
List<String> mList = lists.stream()
.filter(list -> list.contains(str))
.findFirst()
.ifPresent(list -> {
Integer index = Integer.valueOf(lists.indexOf(list));
event.getMessage()
.setInvocationProperty(paramMap.get(index).get(0),paramMap.get(index).get(1))
});
以下是如何创建我称之为 HoldingObject
的内容,但您可以使用更贴近您的业务的名称来命名。
我正在使用 Lombok 的 @Value
注释以及 java-9 的 List#of
工厂方法
@Value
public static class HoldingObject {
List<String> list;
String invocationProperty1;
String invocationProperty2;
public void setInvocationPropertyFor(Event event) {
event.getMessage().setInvocationProperty(invocationProperty1, invocationProperty2);
}
}
请注意,如果通过多个线程访问事件,重复执行 event.getMessage()
可能不是线程安全的
HoldingObject firstObject = new HoldingObject(list1, ABC, ABC1);
HoldingObject secondObject = new HoldingObject(list1, ABC2, ABC3);
HoldingObject thirdObject = new HoldingObject(list1, ABC4, ABC5);
List.of(firstObject, secondObject, thirdObject)
.stream()
.filter(object -> object.getList().contains(str))
.findFirst()
.ifPresent(h -> h.setInvocationPropertyFor(event));
如何更改以下代码以删除 if-else 并使用 Java8
List<String> list1;
List<String> list2;
List<String> list3;
String str;
if(list1.contains(str)){
event.getMessage().setInvocationProperty("ABC","ABC1");
}
else if(list2.contains(str)){
event.getMessage().setInvocationProperty("ABC2","ABC3");
}
else if(list3.contains(str)){
event.getMessage().setInvocationProperty("ABC4","ABC5");
}
不用 if-else
也可以做到,但对于这种情况,if-else
仍然比使用流更好。
List<String> list1;
List<String> list2;
List<String> list3;
String str;
Map<List<String>, List<Param>> paramMap = new HashMap<>();
paramMap.put(list1,List.of(ABC,ABC1));
paramMap.put(list2,List.of(ABC2,ABC3));
paramMap.put(list3,List.of(ABC4,ABC5));
List.of(list1,list2,list3)
.stream()
.filter(list -> list.contains(str))
.findFirst()
.ifPresent(list -> event.getMessage().setInvocationProperty(paramMap.get(list).get(0),paramMap.get(list).get(1)));
不使用列表作为 paramMap
中的键的另一种解决方案:
Map<Integer, List<Param>> paramMap = new HashMap<>();
paramMap.put(1,List.of(ABC,ABC1));
paramMap.put(2,List.of(ABC2,ABC3));
paramMap.put(3,List.of(ABC4,ABC5));
List<List<String>> lists = List.of(list1,list2,list3);
List<String> mList = lists.stream()
.filter(list -> list.contains(str))
.findFirst()
.ifPresent(list -> {
Integer index = Integer.valueOf(lists.indexOf(list));
event.getMessage()
.setInvocationProperty(paramMap.get(index).get(0),paramMap.get(index).get(1))
});
以下是如何创建我称之为 HoldingObject
的内容,但您可以使用更贴近您的业务的名称来命名。
我正在使用 Lombok 的 @Value
注释以及 java-9 的 List#of
工厂方法
@Value
public static class HoldingObject {
List<String> list;
String invocationProperty1;
String invocationProperty2;
public void setInvocationPropertyFor(Event event) {
event.getMessage().setInvocationProperty(invocationProperty1, invocationProperty2);
}
}
请注意,如果通过多个线程访问事件,重复执行 event.getMessage()
可能不是线程安全的
HoldingObject firstObject = new HoldingObject(list1, ABC, ABC1);
HoldingObject secondObject = new HoldingObject(list1, ABC2, ABC3);
HoldingObject thirdObject = new HoldingObject(list1, ABC4, ABC5);
List.of(firstObject, secondObject, thirdObject)
.stream()
.filter(object -> object.getList().contains(str))
.findFirst()
.ifPresent(h -> h.setInvocationPropertyFor(event));