简体   繁体   English

使用自定义比较器从自定义对象的树集中获取子集

[英]Getting a subset from a TreeSet of custom objects with custom comparator

I have an issue with an operation I'm trying to do in Java by using the class TreeSet. 我通过使用类TreeSet在Java中尝试执行的操作存在问题。 I have a class with my custom object, here is the code: 我有一个带有自定义对象的类,下面是代码:

package soluzione;

class Building_event {

    //campi
    private String type;
    private int y;
    private String p;
    private String l;
    private int d;
    private int b;
    private int h;
    private int lateral_profile;

    //costruttore build
    public Building_event(String type,int y, String p, String l, int d, int b, int h) {
        this.type = type;
        this.y = y;
        this.p = p;
        this.l = l;
        this.d = d;
        this.b = b;
        this.h = h;
        this.lateral_profile = b + d;
    }

    //costruttore demolish
    public Building_event(String type, int y, String p) {
        this.type = type;
        this.y = y;
        this.p = p;
        }

    //setter e getter
    public int getY() {
        return y;
    }
    public void setY(int y) {
        this.y = y;
    }
    public String getP() {
        return p;
    }
    public void setP(String p) {
        this.p = p;
    }
    public String getL() {
        return l;
    }
    public void setL(String l) {
        this.l = l;
    }
    public int getD() {
        return d;
    }
    public void setD(int d) {
        this.d = d;
    }
    public int getB() {
        return b;
    }
    public void setB(int b) {
        this.b = b;
    }
    public int getH() {
        return h;
    }
    public void setH(int h) {
        this.h = h;
    }
    public int getLateral_profile() {
        return lateral_profile;
    }
    public void setLateral_profile(int lateral_profile) {
        this.lateral_profile = lateral_profile;
    }
    public String getType() {
        return type;
    }

    public void setType (String type) {
        this.type = type;
    }

    @Override
    public String toString() {
        String res = "";
        if (type.equals("build")) {
             res = "Building_event [type=" + type + ", y=" + y + ", p=" + p + ", l=" + l + ", d=" + d + ", b=" + b + ", h="
                    + h + ", lateral_profile=" + lateral_profile + "]";}
        else if (type.equals("demolish")) {
            res = "Building_event [type=" + type + ", y=" + y + ", p=" + p  + "]";
        }
        return res;
    }
}

I have also written a method which builds the trees with a custom comparator: 我还编写了一种使用自定义比较器构建树的方法:

public class LateralProfileComp implements Comparator<Building_event> {

    @Override
    public int compare(Building_event b1, Building_event b2) {
        if (b1.getLateral_profile() >= b2.getLateral_profile())
            return 1;
        else if (b1.getLateral_profile() < b2.getLateral_profile())
            return -1;
        else return 0;
    }

}

The tree is built correctly, but my question is: since I ordered my elements in the tree according to lateral_profile, how can i get a subset of elements which have a lateral_profile greater than a certain value I specify? 这棵树是正确构建的,但是我的问题是:由于我是根据lateral_profile对树中的元素进行排序的,所以我如何才能获得其中lateral_profile大于指定值的元素子集? I know TreeSet class have a tailset ad subset methods but they require an element to do the comparison. 我知道TreeSet类有一个tailset广告子集方法,但是它们需要一个元素来进行比较。 Should I build an element solely for this purpose? 我是否应该为此目的专门构建一个元素? Thanks in andvance. 感谢andvance。

You could create a LateralProfile interface and have the TreeSet and comparator take any instance of LateralProfile , which would allow you to create a simpler ConstantLateralProfile instance with just a constant lateral profile to search against. 您可以创建一个LateralProfile接口,并让TreeSet和比较器采用LateralProfile任何实例,这将使您可以创建一个更简单的ConstantLateralProfile实例,并仅使用一个恒定的横向轮廓进行搜索。

Something like this would work: 这样的事情会起作用:

import java.util.*;

interface LateralProfile {
    public int getLateral_profile();
}

class Building_event implements LateralProfile {
    public int getLateral_profile() {
        return 123; // or whatever complicated calculation…
    }
}

class ConstantLateralProfile implements LateralProfile {
    private int lateral_profile;

    ConstantLateralProfile(int lateral_profile) { 
        this.lateral_profile = lateral_profile; 
    }

    public int getLateral_profile() { 
        return lateral_profile; 
    }
}

class LateralProfileComp implements Comparator<LateralProfile> {
    @Override
    public int compare(LateralProfile lp1, LateralProfile lp2) {
        if (lp1.getLateral_profile() >= lp2.getLateral_profile())
            return 1;
        else if (lp2.getLateral_profile() < lp1.getLateral_profile())
            return -1;
        else
            return 0;
    }
}

public class Main {

    public static void main(String[] args) {
        var set = new TreeSet<LateralProfile>(new LateralProfileComp());
        set.add(new Building_event());
        System.out.println("grater than 123 lateral profile: " + set.tailSet(new ConstantLateralProfile(123)));
    }
}

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

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