lundi 6 juin 2016

if statements alternative with Java 8

Have a function like this:

public void toDo(Req req)

        if(req.getSection().equals("A")) {
            return execA(req);
        }

        if(req.getSection().equals("B")) {
            return execB(req);
        }

        if(req.getSection().equals("N")) {
            return execN(req);
        }

How can I simplify it? The general idea, how to exclude if statements for the identification type of function - Strings - A, B, N. Any solutions with Java 8 like pattern matching with Scala?

Thanks.

Aucun commentaire:

Enregistrer un commentaire