Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

How to change the below code to remove if-else and use 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");

Here is how to do it by creating what I'll call a HoldingObject, but you can name it with something more close to your business.

I'm using Lombok's @Value annotation and also 's List#of factory method

@Value
public static class HoldingObject {
    List<String> list;
    String invocationProperty1;
    String invocationProperty2;
    public void setInvocationPropertyFor(Event event) {
        event.getMessage().setInvocationProperty(invocationProperty1, invocationProperty2);

Note that doing event.getMessage() repeatedly might not be thread-safe if event is accessed through multiple threads

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));
                That is better. Would it be a good idea to move event.getMessage().setInvocationProperty(holdingObject.getInvocationProperty1(), holdingObject.getInvocationProperty2()) as a method in HoldingObject so that the ifPresent part looks neat ?
– Gautham M
                May 5, 2021 at 12:03

It is possible to do it without if-else, but for this case, if-else would still be better than using streams.

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)));

Another solution without using the list as key in 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))
                It is not advised to use lists as keys for a map as they're not immutable, and if they are, their children are usually not
– Yassin Hajaj
                May 5, 2021 at 11:30
                @YassinHajaj You're right, but IdentityHashMap would be fine here. Or a list of tuples. Same thing
– Michael
                May 5, 2021 at 11:31
                @GauthamM Ah, right... Yeah, that's unfortunate. List<Pair<List<String>, List<Param>>> would work too. Ugly though.
– Michael
                May 5, 2021 at 11:33
                @GauthamM There are many different Pair implementations, not really any one definitive one. Usually when I write an answer that relies on Pair, I just note that it doesn't matter which specific one you use. Usually people will have something on their classpath for Apache or Guava or something. Worse case, core Java has SimpleEntry which is effectively the same thing.
– Michael
                May 5, 2021 at 11:50
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.