Create lambda two dimensional array

2019-07-20 09:02发布

问题:

So I've searched through several websites and others questions on this, and none seem to have the answer that works for me. I have code that works, and my programming mentor suggested I change the chained if/else if to using a lambda table instead. I asked about using something of a hash table, and he said using a hash for only 9 items (the real program has 9 if/else statements) would be a waste.

I will post the working code using both the if/else if and the hash table, keeping it limited to 3 items to keep the code short and sweet.

Here is the code for if/else if...

public class testLambda {
    String[] numArray = {"One", "Two", "Three"};

    testLambda(String num){
        if (num.equals(numArray[0])){
            printStringOne();
        } else if (num.equals(numArray[1])){
            printStringTwo();
        } else if (num.equals(numArray[2])){
            printStringThree();
        }
    }

    private void printStringOne(){
        System.out.println("1");
    }

    private void printStringTwo(){
        System.out.println("2");
    }

    private void printStringThree(){
        System.out.println("3");
    }

    public static void main(String[] args) {
        new testLambda("One");
        new testLambda("Three");
        new testLambda("Two");
    }
}

results in system printing...

1
3
2

Here is the code for a hash table

import java.util.HashMap;
import java.util.Map;

public class testLambda {
    String[] numArray = {"One", "Two", "Three"};

    testLambda(String num){
        Map<String, Runnable> printNumber = new HashMap<>();

        printNumber.put(numArray[0], () -> printStringOne());
        printNumber.put(numArray[1], () -> printStringTwo());
        printNumber.put(numArray[2], () -> printStringThree());

        printNumber.get(num).run();
    }

    private void printStringOne(){
        System.out.println("1");
    }

    private void printStringTwo(){
        System.out.println("2");
    }

    private void printStringThree(){
        System.out.println("3");
    }

    public static void main(String[] args) {
        new testLambda("Three");
        new testLambda("One");
        new testLambda("Two");
    }
}

results in system printing...

3
1
2

and now... the lambda. From what I have read, an interface is required. Keep in mind, I can't use extends, because my application is already extending a different class(java doesn't support multiple inheritance) Here is what I have conjured so far (not working):

public class testLambda {
    String[] numArray = {"One", "Two", "Three"};

    public interface PrintNumber{
        void printNumber();
    }

    testLambda(String num){
        PrintNumber[] printNumbers = new PrintNumber[]{
                new PrintNumber() {public void printNumber(){printStringOne();}},
                new PrintNumber() {public void printNumber(){printStringTwo();}},
                new PrintNumber() {public void printNumber(){printStringThree();}}
        };

        for (int n = 0; n < numArray.length; n++){
            if (num.equals(numArray[n])){
                printNumbers[n];
            }
        }
    }

    private void printStringOne(){
        System.out.println("1");
    }

    private void printStringTwo(){
        System.out.println("2");
    }

    private void printStringThree(){
        System.out.println("3");
    }

    public static void main(String[] args) {
        new testLambda("Three");
        new testLambda("Two");
        new testLambda("One");
    }
}

This results in a compile error. Can anyone tell me what I'm doing wrong? I'm really new to the lambda algorithm.

回答1:

The compilation error is due to this statement:

numArray[n];

which is not a valid statement. What you wanted was this:

    for (int n = 0; n < numArray.length; n++){
        if (num.equals(numArray[n])){
            createCharacters[n].printNumber();
        }
    }

However, this relies on keeping two separate arrays in sync, and is therefore error prone. I suggest using a single HashMap for everything, and getting rid of your original numArray in favour of the HashMap:

public class testLambda {
    Map<String, Runnable> printNumber = new HashMap<>();
    static {
        printNumber.put("One", () -> printStringOne());
        printNumber.put("Two", () -> printStringTwo());
        printNumber.put("Three", () -> printStringThree());
    }

    testLambda(String num){
        printNumber.get(num).run();  // Add some checking here for robustness
    }

By the way, what you call a "lambda table" doesn't necessarily mean it can't be a HashMap. In fact, the above can be called a lambda table. The () -> printStringXXX(); are lambda expressions, and the map is a table of strings to lambdas.



标签: java lambda