Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于具有多个字段的对象的Java Comparator_Java_Sorting_Object_Comparator - Fatal编程技术网

用于具有多个字段的对象的Java Comparator

用于具有多个字段的对象的Java Comparator,java,sorting,object,comparator,Java,Sorting,Object,Comparator,我有一个对象集合,包含5个字段: id; entityType; entityId; brandId; productId; 为了对集合的ArrayList进行排序,我编写了以下compariator Comparator<Collection> collectionComparator = new Comparator<Collection>() { @Override public int compare(Collection collection

我有一个对象
集合
,包含5个字段:

id;
entityType;
entityId;
brandId;
productId;
为了对
集合的
ArrayList
进行排序,我编写了以下
compariator

Comparator<Collection> collectionComparator = new Comparator<Collection>() {

    @Override
    public int compare(Collection collection1, Collection collection2) {
        if(collection1.getId().equals(collection2.getId())) {
            if(collection1.getEntityType().equals(collection2.getEntityType())) {
                if(collection1.getEntityId().equals(collection2.getEntityId())) {
                    if(collection1.getBrandId().equals(collection2.getBrandId())) {
                        return collection1.getProductId().compareTo(collection2.getProductId());
                    } else {
                        return collection1.getBrandId().compareTo(collection2.getBrandId());
                    }
                } else {
                    return collection1.getEntityId().compareTo(collection2.getEntityId());
                }
            } else {
                return collection1.getEntityType().compareTo(collection2.getEntityType());
            }
        } 

        return collection1.getId().compareTo(collection2.getId());
    }
};
比较器集合比较器=新比较器(){ @凌驾 公共整数比较(集合集合1、集合2){ 如果(collection1.getId().equals(collection2.getId())){ if(collection1.getEntityType().equals(collection2.getEntityType())){ if(collection1.getEntityId().equals(collection2.getEntityId())){ 如果(collection1.getBrandId().equals(collection2.getBrandId())){ 返回collection1.getProductId().compareTo(collection2.getProductId()); }否则{ 返回collection1.getBrandId().compareTo(collection2.getBrandId()); } }否则{ 返回collection1.getEntityId().compareTo(collection2.getEntityId()); } }否则{ 返回collection1.getEntityType().compareTo(collection2.getEntityType()); } } 返回collection1.getId().compareTo(collection2.getId()); } };

这是在有多个字段要比较的对象上实现
Comparator
的正确方法吗?

首先,
Collection
java.util
包中的一个类,因此命名您自己的类
Collection
可能不是最好的方法,尽管这当然是可能的

其次,JDK8有一些创建比较器的简洁方法,请检查这里:

特别是第6节和第9节

编辑:不带JKD8:


当通过5个不同的属性进行比较时,我不会像那样硬编码比较,您可以创建自己的比较器链接器(类似于上一个链接中的第9点),并将5个单独的比较器链接在一起。

您的方法可能是正确的,但它效率低下(不必要地调用equals)且难以阅读。可以这样重写:

public int compare(Collection c1, Collection c2)
{
    int n;
    n = c1.id.compareTo(c2.id);
    if (n != 0) return n;
    n = c1.entityType.compareTo(c2.entityType);
    if (n != 0) return n;
    n = c1.brandId.compareTo(c2.brandId);
    if (n != 0) return n;
    return c1.productId.compareTo(c2.productId);
}
更好的方法是使用一个库方法,它将所有这些逻辑抽象出来,这样您就不必考虑它了。例如使用


“对吗”?你在问什么?为什么不直接测试您的代码呢?这是错误的,因为集合中没有getId,除非您自己实现了它,这可能是不必要的。这个问题似乎离题了,因为用户要求我们编写测试用例,而用户懒得自己编写。这不是Java集合,而是一个流行的集合?@djechlin对不起,但我必须说,你的回答对我没有帮助。我以错误的方式问了这个问题。我已经编辑过了。在说“用户要求我们编写用户懒得自己编写的测试用例”之前,您应该查看该用户的配置文件,看看他是否懒散。我没有使用Jdk8,集合与我的项目相关。此外,如果您需要对特定字段进行反向排序,请将c2中的字段与c1中的字段进行比较,然后追加,
.append(c2.brandId,c1.brandId)
public int compare(Collection c1, Collection c2)
{
    return new CompareToBuilder()
            .append(c1.id, c2.id)
            .append(c1.entityType, c2.entityType)
            .append(c1.brandId, c2.brandId)
            .append(c1.productId, c2.productId)
            .toComparison();
}