创建lambda二维数组 [英] Create lambda two dimensional array

查看:122
本文介绍了创建lambda二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我已经搜索了多个网站以及与此有关的其他问题,但似乎没有一个答案适合我.我的代码有效,我的编程指导者建议我将链接的if/else if更改为使用lambda表.我问有关使用哈希表的问题,他说仅对9个项目使用哈希(实际程序中有9个if/else语句)会很浪费.

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.

我将同时使用if/else ifhash table发布工作代码,并将其限制为3个项目,以使代码简短而优美.

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 if ...的代码...

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

系统打印结果...

1
3
2

这是哈希表的代码

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

系统打印结果...

3
1
2

现在是lambda.根据我的阅读,需要一个接口.请记住,我不能使用扩展,因为我的应用程序已经扩展了另一个类(java不支持多重继承),这是到目前为止我想到的(不起作用):

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

这将导致编译错误.谁能告诉我我在做什么错?我真的是lambda算法的新手.

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

推荐答案

编译错误归因于以下语句:

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

但是,这依赖于使两个独立的数组保持同步,因此容易出错.我建议对所有内容使用单个HashMap,并摆脱原来的numArray,而使用HashMap:

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
    }

顺便说一句,所谓的"lambda表"并不一定意味着它不能是HashMap.实际上,以上可以称为lambda表. () -> printStringXXX();是lambda表达式,而映射是lambda的字符串表.

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.

这篇关于创建lambda二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆