Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/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
Java树集比较器_Java_Comparator_Comparable - Fatal编程技术网

Java树集比较器

Java树集比较器,java,comparator,comparable,Java,Comparator,Comparable,我有一个带有字段的Vehicle类:baseVehicleId、subModelId、notes和partNo 如果是相同的baseVehicleId、subModelId和partNo,我希望保留注释最长的对象(并删除其他注释) 我有这个: Comparator<Vehicle> comparator = new Comparator<Vehicle>() { @Override public int compare(Ve

我有一个带有字段的Vehicle类:baseVehicleId、subModelId、notes和partNo

如果是相同的baseVehicleId、subModelId和partNo,我希望保留注释最长的对象(并删除其他注释)

我有这个:

Comparator<Vehicle> comparator = new Comparator<Vehicle>() {
            @Override
            public int compare(Vehicle a, Vehicle b) { 
                if(a.baseVehicleId().equals(b.baseVehicleId()) && String.valueOf(a.subModelId ()).equals(String.valueOf(b.subModelId ()))
                        && String.valueOf(a.partNo ()).equals(String.valueOf(b.partNo ())) 
                ))
                    return a.getOurnotes().compareTo(b.getOurnotes());
                // not good I know
                 return 1;
            }
        };

        TreeSet<Vehicle> treeSet = new TreeSet<Vehicle>(comparator);
比较器比较器=新比较器(){ @凌驾 公共int比较(车辆a、车辆b){ 如果(a.baseVehicleId().equals(b.baseVehicleId())&String.valueOf(a.subModelId()).equals(String.valueOf(b.subModelId())) &&String.valueOf(a.partNo()).equals(String.valueOf(b.partNo())) )) 返回a.getOurnotes().compareTo(b.getOurnotes()); //不太好,我知道 返回1; } }; TreeSet TreeSet=新的TreeSet(比较器); 如何修改此代码:/?
例如,如果所有字段相等且注释长度大于其他对象,则删除其他对象。

只需在集合中循环,然后使用
it.remove()
删除所有不符合要求的内容


您可能还想看看Java 8 collections功能,它允许您执行筛选集合之类的操作,因为它也可以执行您正在查找的操作。

我认为您应该使用该方法

maximum = Collections.max(collection, comparator);
用于搜索元素的最大值,然后使用

collection.remove();

在comparator内部,您不能从集合中删除。comparator的工作只是说明两个对象中的哪一个先到—在比较过程中,没有任何机制来操纵基础集合。我认为您应该以稍微不同的方式来考虑这个问题:在向集合中添加对象之前执行过滤逻辑

为了简单起见,我建议只使用标准列表,比如
ArrayList
。此外,您还需要重写Vehicle类中的
equals()
方法,如果两辆车共享相同的ID和零件号,该方法将返回
true

在添加新车时,您可以执行以下操作:

int vehicleIndex = myvehicles.indexOf(vehicle) // -1 if missing, otherwise 0 or greater to represent the index position

if (vehicleIndex == -1) {
    // No item in the list shares the ids/model/part numbers so can add
    myvehicles.add(vehicle)
} else {
    // There's another similar vehicle, better compare their notes:
    Vehicle existingVehicle = myvehicles.get(vehicleIndex); // retrieve the object from list so that we can compare it

    // check if the new vehicle has longer notes
    if (vehicle.getOurNotes().length > existingVehicle.getOurNotes().length) {
        // if it does, remove the old one from the list
        myvehicles.remove(vehicleIndex);
        // and add the new one. Effectively we've done a replacement
        myvehicles.add(vehicle)
    }
}

显然,它不是自动排序的,但是在添加一批项目后,可以通过运行同样接受比较器的方法来进行排序,但这一次,您的比较器可以专注于详细说明排序的具体任务,因为您知道列表已预筛选,因此您不必担心尝试筛选项目。

有一个解决方案,但需要番石榴。。。