简体   繁体   English

从对象数组列表中获取最大值?

[英]Getting max value from an arraylist of objects?

Is there an easy way to get the max value from one field of an object in an arraylist of objects?有没有一种简单的方法可以从对象数组列表中对象的一个​​字段中获取最大值? For example, out of the following object, I was hoping to get the highest value for the Value field.例如,在以下对象中,我希望获得 Value 字段的最高值。

Example arraylist I want to get the max value for ValuePairs.mValue from.示例 arraylist 我想从中获取 ValuePairs.mValue 的最大值。

ArrayList<ValuePairs> ourValues = new ArrayList<>();
outValues.add(new ValuePairs("descr1", 20.00));
outValues.add(new ValuePairs("descr2", 40.00));
outValues.add(new ValuePairs("descr3", 50.00));

Class to create objects stored in arraylist:创建存储在数组列表中的对象的类:

public class ValuePairs {

    public String mDescr;
    public double mValue;

    public ValuePairs(String strDescr, double dValue) {
        this.mDescr = strDescr;
        this.mValue = dValue;
    }
}

I'm trying to get the max value for mValue by doing something like (which I know is incorrect):我正在尝试通过执行以下操作来获得 mValue 的最大值(我知道这是不正确的):

double dMax = Collections.max(ourValues.dValue);

dMax should be 50.00. dMax 应为 50.00。

Use a Comparator with Collections.max() to let it know which is greater in comparison.使用带有Collections.max()Comparator让它知道哪个更大。


Also See另见

使用 Java 8,您可以将stream()与它的预定义max()函数和Comparator.comparing()功能与 lambda 表达式一起使用:

ValuePairs maxValue = values.stream().max(Comparator.comparing(v -> v.getMValue())).get();

This has been answered multiple time already, but since it's the first result on google I will give a Java 8 answer with an example.这已经被多次回答了,但由于它是谷歌上的第一个结果,我将给出一个带有示例的 Java 8 答案。

Take a look at the stream feature.看一下功能。 Then you can get the max form an List of Objects like this:然后你可以得到最大形式的对象列表,如下所示:

List<ValuePairs> ourValues = new ArrayList<>();

ourValues.stream().max(comparing(ValuePairs::getMValue)).get()

By the way in your example, the attributes should be private.顺便说一下,在您的示例中,属性应该是私有的。 You can then access them with a getter.然后您可以使用 getter 访问它们。

You should iterate over the list comparing/finding the max value O(N) .您应该遍历列表比较/找到最大值O(N) If you need to do this often replace the list with a PriorityQueue O(1) to find the max.如果您需要这样做,请经常用PriorityQueue O(1)替换列表以找到最大值。

Here fist and last is intervals between two indexes of arraylist you can also get for a complete list by removing them and i=0 upto size of float list.这里的 fist 和 last 是 arraylist 的两个索引之间的间隔,您也可以通过删除它们来获得完整的列表, i=0 直到浮点列表的大小。

// for min value // 最小值

public String getMinValue(ArrayList list, int first, int last) { public String getMinValue(ArrayList list, int first, int last) {

        List<Float> floatList = new ArrayList<>();

        for (int i = 0; i < list.size(); i++) {
            Float prova2 = ((Double) list.get(i)).floatValue();
            floatList.add(prova2);
        }
        float min = Float.MAX_VALUE;
        String minValue = "";

        for (int i = first; i < last; i++) {


            if (floatList.get(i) < min) {
                min = floatList.get(i);
            }
        }
        minValue = String.format("%.1f", min);
        return minValue;
    }

// for max value // 最大值

    public String getMaxValue(List<Object> list, int first, int last) {

        List<Float> floatList = new ArrayList<>();

        for (int i = 0; i < list.size(); i++) {
            Float prova2 = ((Double) list.get(i)).floatValue();
            floatList.add(prova2);
        }

        float max = Float.MIN_VALUE;
        String maxValue = "";

        for (int i = first; i < last; i++) {


            if (floatList.get(i) > max) {
                max = floatList.get(i);
            }
        }
        maxValue = String.format("%.1f", max);
        return maxValue;
    }

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

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