将 Java 8 Lambda 函数转换为 Java 7

2024-04-30

嘿,我是编码新手,我已经掌握了 Java 8 的 Lambda 函数,但我正在尝试将我为学校项目编写的一些代码转换为 Java 7,但我无法理解关于如何使这段代码在功能上相同,但在 java 7 中。很抱歉,如果这是一个愚蠢的问题,但我似乎无法弄清楚。我是否编写一个自定义方法,然后将其应用到我的 PriorityQueue。

open = new PriorityQueue<>((Object o1, Object o2) -> {
                Cell c1 = (Cell)o1;
                Cell c2 = (Cell)o2;

                return c1.endCost<c2.endCost?-1:
                        c1.endCost>c2.endCost?1:0;
            });

尝试使用匿名Comparator在这里上课:

open = new PriorityQueue<Cell>(new Comparator<Cell>() {
            @Override
            public int compare(Cell o1, Cell o2) {
                return c1.endCost < c2.endCost ? -1 :
                        c1.endCost > c2.endCost ? 1 : 0;
            }
        });

You can do this automatically in Intellij Idea. Place cursor on -> and hit Alt+Enter:

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将 Java 8 Lambda 函数转换为 Java 7 的相关文章

随机推荐