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

查看:461
本文介绍了如何根据两个参数对对象列表进行排序以在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.

我的意思是:

I mean:

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:

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

或通过以下方式获得成本最高的商品

or get the best cost item via

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

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

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