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

时间:2018-10-24 14:36:20

标签: java tree treeset

我试图通过使用类TreeSet在Java中进行的操作遇到问题。我的自定义对象有一个类,下面是代码:

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

我还写了一种使用自定义比较器构建树的方法:

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

}

树是正确构建的,但是我的问题是:由于我是根据lateral_profile对树中的元素进行排序的,所以我如何才能获得其中lateral_profile大于指定值的元素子集?我知道TreeSet类有一个tailset广告子集方法,但是它们需要一个元素来进行比较。我是否应该为此目的专门构建一个元素?感谢andvance。

1 个答案:

答案 0 :(得分:0)

您可以创建一个LateralProfile接口,并使TreeSet和比较器采用LateralProfile的任何实例,这将使您可以创建一个更简单的ConstantLateralProfile实例,只需一个要搜索的恒定侧面轮廓。

类似的事情会起作用:

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