Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Java 8 Lambda Function to Java 7

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;
            });
like image 345
Vladislav Varadinov Avatar asked Jul 25 '26 08:07

Vladislav Varadinov


1 Answers

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

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

enter image description here

like image 89
Micer Avatar answered Jul 27 '26 21:07

Micer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!