【问题标题】:Sort a list of two different class objects对两个不同类对象的列表进行排序
【发布时间】:2020-11-06 09:06:49
【问题描述】:

我有两个不同的类对象列表 TrackerPet 并将这两个列表组合成一个类对象列表 PetManagement。现在我想对我的 PetManagement 列表进行排序,例如,与 Pets 链接的 Tracker 在列表的开头,所有未链接的都在后面。所以最后我的列表应该是这样的:

位置。追踪器 - 宠物

  1. tracker_one - pet_one
  2. tracker_two - ...
  3. ...-pet_two

所以我的列表中有空对象。我现在编码了一段时间并最终进入了很多 for 循环,所以我认为有更好的解决方案来获得相同的结果。我担心我的代码知道不是真正可读并且容易产生问题。

注意:在我的 TrackerPet 类中,我使用唯一的 id 来让两者知道哪些是链接的,我也有一个布尔值(hasTracker/hasPet) .

public class PetManagement {

    private Pet pet;
    private Tracker tracker;

    public PetManagement(Pet pet) {
        this.pet = pet;
    }

    public PetManagement(Tracker tracker) {
        this.tracker = tracker;
    }

    public PetManagement(Pet pet, Tracker tracker) {
        this.pet = pet;
        this.tracker = tracker;
    }

    public Pet getPet() {
        return pet;
    }

    public Tracker getTracker() {
        return tracker;
    }
}

public class Pet {

    private Double id;
    private String name;
    private ImageView picture;
    private String race;
    private float weight;
    private float height;
    private Sex gender;
    private boolean isCastrated;
    private boolean isSterilized;
    private int chipId;
    private boolean hasTracker;
    private Double trackerId;

    public Pet(Double id, String name, ImageView picture, String race, float weight, float height, Sex gender, boolean isCastrated, boolean isSterilized, int chipId, boolean hasTracker, Double trackerId) {
        this.id = id;
        this.name = name;
        this.picture = picture;
        this.race = race;
        this.weight = weight;
        this.height = height;
        this.gender = gender;
        this.isCastrated = isCastrated;
        this.isSterilized = isSterilized;
        this.chipId = chipId;
        this.hasTracker = hasTracker;
        this.trackerId = trackerId;
    }

   getter and setter...
}

public class Tracker {

    private Double id;

    private String serialNum;

    private boolean hasPet;

    private Double petId;

    public Tracker(double id, String serialNum) {
        this.id = id;
        this.serialNum = serialNum;
    }

    public Tracker(Double id, String serialNum, boolean hasPet, Double petId) {
        this.id = id;
        this.serialNum = serialNum;
        this.hasPet = hasPet;
        this.petId = petId;
    }
   
  getter and setter...
}

【问题讨论】:

  • 能否请您发布您的追踪器和宠物模型
  • 添加到我的编辑中
  • 宠物管理感觉很多余,每个Tracker都有或没有宠物,这(在我看来)意味着Tracker应该有一个宠物列表,然后宠物管理就变得多余了
  • 感觉就像您正在尝试根据数据库设计对其进行建模,这使事情变得更加困难,但这只是我的看法:)
  • 没有一个追踪者拥有一只宠物。或者一只宠物有一个追踪器 ^^ 我这样做是因为我想使用排序列表让 RecyclerView 对用户可见。但我明白你的意思,也许我应该尝试在 Tracker 类中添加一个 Pet 字段,

标签: java android list sorting compare


【解决方案1】:

我只会让你的班级具有可比性:

public class PetManagement implements Comparable<PetManagement> {

    ...

    @Override
    public int compareTo(PetManagement other) {
        boolean thisLinked = this.hasTracker() && this.hasPet();
        boolean otherLinked = other.hasTracker() && other.hasPet();

        if (thisLinked && !otherLinked)
            return -1;
        else if (!thisLinked && otherLinked)
            return 1;

        return 0;
    }
}

【讨论】:

  • 我稍微改了一下,但现在对我有用:)
猜你喜欢
  • 2012-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-07
  • 1970-01-01
  • 1970-01-01
  • 2013-07-20
相关资源
最近更新 更多