简体   繁体   English

在java中实现Comparator比较方法的最佳方法?

[英]Best way to implement compare method of Comparator in java?

I have written a comparator which sorts by ascending order as below. 我写了一个比较器,按升序排序,如下所示。 which is working well. 哪个运作良好。

Collections.sort(resultList,new Comparator<MyModelClass>() {
            @Override
            public int compare(MyModelClass o1, MyModelClass o2) {
                Integer id1= o1.getId();
                Integer id2= o2.getId();
                if(id1 == null && id2 == null) {
                    return 0;               
                }else if(id1 != null && id2 == null) {
                    return -1;
                } else if (id1 == null && id2 != null) {
                    return 1;
                } else {                
                    return id1.compareTo(id2);
                }
            }
        });

is it good to implement like this? 这样实施好吗? Please help me? 请帮我?

Thanks! 谢谢!

It looks good for readability, but a slightly more efficient way might be: 它的可读性看起来不错,但更有效的方法可能是:

public int compare(MyModelClass o1, MyModelClass o2) {
    Integer id1= o1.getId();
    Integer id2= o2.getId();
    if (id1 == null) {
        return id2 == null ? 0 : 1;
    }
    if (id2 == null) {
        return -1;
    }
    return id1.compareTo(id2);
}

or even: 甚至:

public int compare(MyModelClass o1, MyModelClass o2) {
    Integer id1= o1.getId();
    Integer id2= o2.getId();
    if (id1 == null) {
        return id2 == null ? 0 : 1;
    }

    return id2 == null ? -1 : id1.compareTo(id2);
}

If you need the null-safe comparison logic in several comparators then I would suggest using a static helper in a utility class like this: 如果你需要几个比较器中的空安全比较逻辑,那么我建议在实用程序类中使用静态帮助器,如下所示:

public static int compare(Comparable c1, Comparable c2) {
    return c1 == null
               ? (c2 == null ? 0 : 1)
               : (c2 == null ? -1 : c1.compareTo(c2));
}

The Comparator could then be simplified to: 然后可以将比较器简化为:

public int compare(MyModelClass o1, MyModelClass o2) {
    return CompareHelper.compare(o1.getId(), o2.getId());
}

If getId() returns an int, you can simply go with return id1.compareTo(id2) , this will give you the right result. 如果getId()返回一个int,你可以简单地使用return id1.compareTo(id2) ,这将给你正确的结果。 Hope this helps. 希望这可以帮助。

Yes it is, I sort of do the same thing. 是的,我有点做同样的事情。 One remark would be that you can use so nullsafe comparison tools such as Apache Commons Collections's NullComparator to ditch all those null checkings in your code: 有一点需要注意的是,您可以使用诸如Apache Commons Collections的NullComparator之类的nullsafe比较工具来放弃代码中的所有空检查:

http://commons.apache.org/collections/api-2.1.1/org/apache/commons/collections/comparators/NullComparator.html#compare(java.lang.Object , java.lang.Object) http://commons.apache.org/collections/api-2.1.1/org/apache/commons/collections/comparators/NullComparator.html#compare(java.lang.Object,java.lang.Object

No, it is not a good implementation. 不,这不是一个好的实施。

The java.util.List specification says you can have nulls in a list and in some cases you can have multiple nulls. java.util.List规范说明你可以在列表中有空值,在某些情况下你可以有多个空值。 Your Comparator will fail with a NullPointerException as soon as it tries to do o?.getId() on a null element. 只要尝试在null元素上执行o?.getId() ,Comparator就会失败并出现NullPointerException。

What I generally do is make my class implement java.lang.Comparable , then I can use a Map to sort the elements as I add them. 我通常做的是让我的类implement java.lang.Comparable ,然后我可以使用Map在添加它们时对元素进行排序。 One generally has to build the list so why not build a TreeMap instead? 通常需要构建列表,为什么不构建一个TreeMap呢?

If you are reusing your class and want to sort it in different ways you can create a TreeMap with a Comparator in the constructor so that there is no need to explicitly sort it. 如果您正在重用您的类并希望以不同的方式对其进行排序,则可以在构造函数中使用Comparator创建TreeMap ,以便不需要对其进行显式排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM