简体   繁体   English

比较arraylist中不同的对象值

[英]compare varying object values from arraylist

Depending on user preferences I want to compare object values of an array list and get the max values. 根据用户首选项,我想比较数组列表的对象值并获取最大值。 My first approach was to define a function for object property and iterate through the array list. 我的第一种方法是为对象属性定义一个函数并迭代数组列表。

    private MyModel getMaxValueA(ArrayList<MyModel> myModelList) {
    MyModel res = null;
    for (MyModel myModel : myModelList) {
        if (myModel != null) {
            if (myModel.valueA() > res.valueA()) {
                res = myModel;
            }
        } else {
            res = myModel;
        }
    }
    return res;
}

The problem is, i have 4 different values i want to compare, and to define 4 similar funktion does not seem to be the right was, so i tried it to combine all functions and added a switch/case 问题是,我有4个不同的值,我想比较,并定义4个类似的funktion似乎不是正确的,所以我尝试它结合所有功能,并添加了一个开关/案例

    private MyModel getMaxValueA(ArrayList<MyModel> myModelList, Setting mySetting) {
    MyModel res = null;
    for (MyModel myModel : myModelList) {
        if (myModel != null) {
            switch (mySetting) {
            case settingA:
                if (myModel.valueA() > res.valueA()) {
                     res = myModel;
                    }
                break;
            case settingB:
                if (myModel.valueB() > res.valueB()) {
                     res = myModel;
                    }

                break;
            ........
        } else {
            res = myModel;
        }
    }
    return res;
}

This is a bit shorter and only 1 instead of 4 functions, but it doesnt make me happy, too. 这有点短,只有1个而不是4个功能,但它也不会让我开心。 Do you have any ideas to improve it? 你有什么想法来改善吗?

Thanks. 谢谢。

Implement all your different needs like this: 实现您的所有不同需求,如下所示:

class SettingsAComparator extends Comparator<MyModel> {
    @Override
    public int compare(MyModel m1, MyModel m2) {
         return m1.valueA() - m2.valueA();
    }
}

class SettingsBComparator extends Comparator<MyModel> {
    // Please use better names.
    // you can implement as many Comparators as necessary.
}

and change your method to 并将您的方法更改为

private MyModel getMaxValue(ArrayList<MyModel> myModelList, Comparator<MyModel> comparator) {
    return Collections.max(myModelList, comparator);
}

This way, you can always add different comparators, if your class gets a new attribute, but you'll never have to worry about changing getMaxValue() again. 这样,如果您的类获得了新属性,您可以随时添加不同的比较器,但您永远不必担心再次更改getMaxValue() Also you can implement complex comparators, that take more than one attribute value into account. 您还可以实现复杂的比较器,将多个属性值考虑在内。

Implement a Comparator 实施比较器

 class MyComparator extends Comparator<MyModel> {
     Setting s;
     MyComparator(Setting s ){
        this.setting=s;
     } 
     public int compare(MyModel model, MyModel model2) {
        //do the comparison utilizing setting

     }

Edited looking at another answer: Then use Collections.max(listToBeSorted, new MyComparator(setting)) to get max value 编辑看另一个答案:然后使用Collections.max(listToBeSorted, new MyComparator(setting))获取最大值

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

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