简体   繁体   English

Java TreeSet比较器

[英]Java TreeSet Comparator

I have a Vehicle class with fields: baseVehicleId, subModelId, notes and partNo 我有一个包含以下字段的Vehicle类:baseVehicleId,subModelId,notes和partNo

If is the same baseVehicleId, subModelId and partNo , i want the object with the longest notes to remain( and delete the others) 如果是相同的baseVehicleId,subModelId和partNo,我希望保留最长注释的对象(并删除其他注释)

I have this: 我有这个:

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

How can i modify this code :/? 我如何修改此代码:/? So for example if i have all the fields equal and the length of notes are greater then delete the other objects. 因此,例如,如果我所有字段均相等且注释的长度较大,则删除其他对象。

The comparator's job is simply to say which of two objects comes first - there's no mechanism to manipulate the underlying collection during it's comparisons. 比较器的工作只是说两个对象中的哪个先出现-在比较过程中没有机制可以操纵基础集合。 I think you would want to think about this in a slightly different way: perform your filtering logic prior to adding an object to your collection. 我认为您可能会以一种稍微不同的方式来考虑:在将对象添加到集合之前执行过滤逻辑。

I'd recommend just using a standard list, say ArrayList for sake of simplicity. 为了简单起见,我建议仅使用标准列表,例如ArrayList Also you'd need to override the equals() method in your Vehicle class which returns true if two Vehicles share the same IDs and part numbers. 另外,您还需要重写Vehicle类中的equals()方法,如果两个Vehicle共享相同的ID和部件号,则该方法返回true

When it comes to adding new vehicles you can do something like this: 在添加新车辆时,您可以执行以下操作:

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

Obviously it's not automatically sorted but you can do that after adding a batch of items by running the Collections.sort() method which also accepts a Comparator, but this time your comparator can focus on the specific task of detailing the ordering now that you know the list is pre-filtered so you are not concerned with trying to filter items out. 显然,它不会自动排序,但是您可以通过运行还接受Comparator的Collections.sort()方法添加一批商品之后执行此操作,但这一次您的比较器现在可以专注于详细说明排序的特定任务,因为您知道该列表已预先过滤,因此您不必担心要过滤掉项目。

Just loop through the collection and remove everything that doesn't meet your requirements using it.remove() . 只需遍历集合,并使用它删除所有不符合您要求的it.remove()

You may also want to look at the Java 8 collections functionality that will allow you to do things like filter a collection as that will also do what you are looking for. 您可能还需要查看Java 8集合功能,该功能将允许您执行诸如过滤集合之类的事情,因为这也将满足您的需求。

I think you should use the method 我认为你应该使用该方法

maximum = Collections.max(collection, comparator);

for searching the maximum of the elements and then just delete the others with 用于搜索最多的元素,然后删除其他带有

collection.remove();

Inside the comparator you can't delete from the collection. 在比较器内部,您不能从集合中删除。

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

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