简体   繁体   中英

Use different methods based on an integer value

this is something i cannot get my head around, now i have something like this:

boolean method1(int a){
//something
returns true;
}

boolean method2(int a){
//something
returns true;
}

for (int i; i<100; i++){
  switch (someInt){
  case 1: boolean x = method1(i);
  case 2: boolean x = method2(i);
  }


}

What i would like is to take the switch out of the loop, as the someInt will stay the same for every i, thus need to be decided only once, but i need x to be checked for every i, so i would need something like:

    switch (someInt){
          case 1: method1(); //will be used in loop below
          case 2: method2(); //will be used in loop below
          }

   for (int i; i<100; i++){
       boolean x = method the switch above picked
   }

You can use Java 8 method references.

Here is an example:

public class WithMethodRefs {
    interface MyReference {
        boolean method(int a);
    }

    boolean method1(int a) {
        return true;
    }

    boolean method2(int a) {
        return false;
    }

    public void doIt(int someInt) {
        MyReference p = null;
        switch (someInt) {
        case 1:
            p = this::method1;
            break;
        case 2:
            p = this::method2;
            break;
        }

        for (int i = 0; i < 100; i++) {
            p.method(i);
        }
    }
}

You can replace your conditional with polymorphism:

Some examples:

An example with your code:

    interface CallIt {
        boolean callMe(int a);
    }

    class Method1 implements CallIt {
        public boolean callMe(int a) {
            return true;
        }
    }

    class Method2 implements CallIt {
        public boolean callMe(int a) {
            return true;
        }
    }

    void doIt(int someInt) {
        CallIt callIt = null;
        switch (someInt) {
        case 1:
            callIt = new Method1();
            break;
        case 2:
            callIt = new Method2();
            break;
        }

        for (int i = 0; i < 100; i++) {
            boolean x = callIt.callMe(i);
        }
    }

My two cents. If we started to be functional let's do it till the end!

    IntFunction<IntFunction<Boolean>> basic = x -> i -> {
        switch (x) {
            case 1: return method1(i);
            case 2: return method2(i);
            default:
                throw new RuntimeException("No method for " + someInt);
        }
    };
    IntFunction<Boolean> f = basic.apply(someInt);
    IntStream.range(0, 100).forEach(i -> {
        boolean result = f.apply(i);
        //do something with the result
    });

Also I don't see any break statements in your switch. Maybe it's because this is just an example, but check if they are here. You will get method2() for all cases otherwise.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM