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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19338686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 16:15:28  来源:igfitidea点击:

Java - getting max value from an arraylist of objects?

java

提问by user797963

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。

Any help is appreciated. Thanks!

任何帮助表示赞赏。谢谢!

采纳答案by Jigar Joshi

Use a Comparatorwith Collections.max()to let it know which is greater in comparison.

使用ComparatorwithCollections.max()让它知道哪个更大。



Also See

另见

回答by Cratylus

You should iterate over the list comparing/finding the max value O(N). If you need to do this often replace the list with a PriorityQueueO(1)to find the max.

您应该遍历列表比较/找到最大值O(N)。如果您需要这样做,请经常用PriorityQueue替换列表O(1)以找到最大值。

回答by Paul Fournel

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 streamfeature. 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 访问它们。

回答by M. Schena

With Java 8 you can use stream()together with it's predefined max()function and Comparator.comparing()functionality with lambda expression:

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

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

回答by Akash Namdev

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;
    }