20

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.

Example arraylist I want to get the max value for ValuePairs.mValue from.

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

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

dMax should be 50.00.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
user797963
  • 2,907
  • 9
  • 47
  • 88

5 Answers5

29

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

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

Instead of using a lambda expression, you can also use the method reference directly:

ValuePairs maxValue = values.stream().max(Comparator.comparing(ValuePairs::getMValue)).get();
M. Schena
  • 2,039
  • 1
  • 21
  • 29
  • 1
    Is there a way to get *all* the `ValuePairs` matching the `.max()`? – Nato Boram Aug 08 '19 at 23:59
  • That would be something like this after you have found out the max value: `List maxValues = ourValues.stream().filter(v -> v.mValue == maxValue.mValue).collect(Collectors.toList());` – M. Schena Aug 09 '19 at 07:07
24

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


Also See

Community
  • 1
  • 1
jmj
  • 237,923
  • 42
  • 401
  • 438
9

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.

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.

Paul Fournel
  • 10,807
  • 9
  • 40
  • 68
1

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 PriorityQueue O(1) to find the max.

Cratylus
  • 52,998
  • 69
  • 209
  • 339
0

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.

// for min value

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