将Java 8 Lambda函数转换为Java 7 [英] Convert Java 8 Lambda Function to Java 7

查看:150
本文介绍了将Java 8 Lambda函数转换为Java 7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hey I'm new to coding and I've kind of gotten a hold of Java 8's Lambda functions but I'm trying to convert some of the code I wrote to Java 7 for a project in school and I can't wrap my head around how to make this piece of code identical in functionality but in java 7. Sorry if this is a dumb questions but I can't seem to figure it out. Do I write a custom method and then apply it to the my 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类:

Try to use anonymous Comparator class here:

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

您可以在Intellij Idea中自动执行此操作.将光标放在->上并按 Alt + Enter :

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

这篇关于将Java 8 Lambda函数转换为Java 7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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