Comparator with double type

前端 未结 10 1575
盖世英雄少女心
盖世英雄少女心 2020-11-28 11:50

I have written the following code:

public class NewClass2 implements Comparator
{
    public int compare(Point p1, Point p2)
    {
        retur         


        
10条回答
  •  清歌不尽
    2020-11-28 12:30

    I just want to expand on Peter Lawrey answer on JDK 8, if you do it like this:

    public class NewClass2 implements Comparator {
        public int compare(Point p1, Point p2) {
            return Double.compare(p1.getY(), p2.gety());
        }    
    }
    

    You could define this comparator using a lambda expression pretty easily

    (Point p1,Point p2) -> Double.compare(p1.getY(), p2.gety())  
    

    Better yet, you could use a member reference like this:

    Double::compare
    

提交回复
热议问题