简体   繁体   English

创建lambda二维数组

[英]Create lambda two dimensional array

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. 我的代码有效,我的编程指导者建议我将链接的if/else if改为使用lambda表。 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. 我问有关使用哈希表的问题,他说仅对9个项目使用哈希(实际程序中有9个if / else语句)会很浪费。

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. 我将同时使用if/else ifhash table来发布工作代码,将其限制为3个项目,以使代码简短而优美。

Here is the code for if/else if... 这是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. 现在... 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): 请记住,我不能使用扩展,因为我的应用程序已经扩展了另一个类(Java不支持多重继承),这是到目前为止我想到的(不起作用):

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. 我真的是lambda算法的新手。

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: 我建议对所有内容使用单个HashMap,并摆脱原来的numArray,而使用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. 顺便说一句,所谓的“ lambda表”并不一定意味着它不能是HashMap。 In fact, the above can be called a lambda table. 实际上,以上可以称为lambda表。 The () -> printStringXXX(); () -> printStringXXX(); are lambda expressions, and the map is a table of strings to lambdas. 是lambda表达式,并且映射是lambda的字符串表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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