如何根据两个参数对对象列表进行排序以在 Java 中进行比较? [英] How to sort a list of objects according to two parameters to compare at Java?

查看:23
本文介绍了如何根据两个参数对对象列表进行排序以在 Java 中进行比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堂这样的课:

public class Zern extends Something{
 private int costA;
 private int costB;

 public int getcostA() {
     return costA;
 }

 public void setcostA(int costA) {
     this.costA = costA;
 }

 public int getcostB() {
     return costB;
 }

 public void setcostB(int costB) {
     this.costB = costB;
 }
}

我有一个包含此类对象的列表:

I have a list that holds that kind of objects:

private List<Zern> zerns = new ArrayList<Zern>(MAX_ZERN_SIZE);

我会将新对象添加到我的列表中,但是我总是希望根据成本 a 有一个有序列表,如果列表中有一个对象与我要添加的对象具有相同的成本,我想添加根据成本确定对象 B.

I will add new objects to my list however I always want to have a ordered list according to cost a and if there is an object at list which has the same cost with my object that I want to add I want to add that object according to their costB.

我的意思是:

Index of objects at list   0    1    2    3    4   5
CostA                     10   15   22   22   25  36
CostB                     26   12   17   19   23  44

If I want to add an object that has a costA 22 and costB 18, 
it will locate at index 3.

我怎样才能有效地做到这一点(因为我将一个对象添加到一个排序列表中,所以这意味着我可以使用二分搜索 - 如果可能的话,我想根据那个找到一个解决方案) 与 比较器 或类似的东西?

How can I do it effectively (because I will add an object to a sorted list so it means that I can use binary search - if it is possible I want to find a solution according to that) with Comparator or something like that?

推荐答案

使用 Collections.sort 和以下比较器:

Use Collections.sort with the following comparator:

Collections.sort(zerns, new Comparator<Zern>() {

    @Override
    public int compare(Zern z1, Zern z2) {
        if (z1.getcostA() == z2.getcostA()) {
            return z1.getcostB() == z2.getcostB() ? 0 : 
                z1.getcostB() < z2.getcostB() ? -1 : 1;
        } else {
            return z1.getcostA() < z2.getcostA() ? -1 : 1;
        }
    }
});

更新:如果您不需要索引访问您的项目,您可能希望从一开始就使用带有自定义比较器的排序集实现:

Update: If you do not need indexed access to your items you may want to use a sorted set implementation from the first place with a custom comparator:

TreeSet<Zern> zerns = new TreeSet<Zern>(new Comparator<Zern>() {

    @Override
    public int compare(Zern z1, Zern z2) {
        if (z1.getcostA() == z2.getcostA()) {
            return z1.getcostB() == z2.getcostB() ? 0 : 
                z1.getcostB() < z2.getcostB() ? -1 : 1;
        } else {
            return z1.getcostA() < z2.getcostA() ? -1 : 1;
        }
    }
});

现在可以添加对象并且您的集合将始终保持排序(注意:我在您的 Zern 类中添加了一个构造函数和 toString):

Now objects can be added and your set will always remain sorted (note: I added a constructor and toString to your Zern class):

zerns.add(new Zern(10, 26));
System.out.println(zerns);     // => [(10,26)]
zerns.add(new Zern(22, 19));
System.out.println(zerns);     // => [(10,26), (22,19)]
zerns.add(new Zern(22, 17));
System.out.println(zerns);     // => [(10,26), (22,17), (22,19)]
zerns.add(new Zern(15, 12));
System.out.println(zerns);     // => [(10,26), (15,12), (22,17), (22,19)]

您可以删除项目

zerns.remove(new Zern(22, 17));
System.out.println(zerns);     // => [(10,26), (15,12), (22,19)]

或删除最坏成本项

zerns.remove(zerns.last());
System.out.println(zerns);     // => [(10,26), (15,12)]

或通过

System.out.println(zerns.first());    // => (10,26)

这篇关于如何根据两个参数对对象列表进行排序以在 Java 中进行比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆