Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find maximum value of an attribute in a array of objects [duplicate]

Tags:

java

max

In a Java program, I have a class WaterBody in which one of the attributes is a double for electric power output. How can I find the maximum electric power output value in an array of WaterBody instances?

Here's my prototype:

public static WaterBody mostPowerful(WaterBody[] waterBodies) {

}

Attribute is electricPower and I have the getter method getElectricPower

Thanks in advance.

like image 451
rjpedrosa Avatar asked Dec 08 '22 23:12

rjpedrosa


2 Answers

If you're using Java 8, you can write this as a one-liner:

public static WaterBody mostPowerful(WaterBody[] waterBodies) {
    return Arrays.stream(waterBodies)
            .max(Comparator.comparingDouble(WaterBody::getElectricPower))
            .orElseThrow(NoSuchElementException::new);
}

The orElseThrow(NoSuchElementException::new) causes a NoSuchElementException to be thrown if the incoming array is empty (and thus there is no maximum value). If you want to return null instead, use orElse(null).

like image 149
Chris Jester-Young Avatar answered Apr 15 '23 03:04

Chris Jester-Young


I am afraid you'll have to do a linear search:

public static WaterBody mostPowerful(WaterBody[] waterBodies) {
    double maxValue = -1;
    int indexOfMaxValue = -1;
    for(int i = 0; i < waterBodies.length; i++) {
        if(waterBodies[i].getElectricPower() > maxValue) {
            indexOfMaxValue = i;
        }
    }
    return waterBodies[indexOfMaxValue];
}

A more elegant solution can be achieved by using a Comparator. It defines an Interface, which tells if one object is "bigger" or "smaller" than another. You can define a Comparator like that:

Comparator<String> cmp = new Comparator<WaterBody>() {
    @Override
    public int compare(WaterBody o1, WaterBody o2) {
        return Double.compare(o1.getElectricPower(), o2.getElectricPower());
    }
};

If you'll need do operations like these often, it is advised to have a collection which always keeps it's content sorted, like a TreeSet It would be a lot smarter to use a collection, which keeps it's content sorted.

TreeSet<WaterBody> waterBodiesTree = new TreeSet<>(cmp);
Collections.addAll(waterBodiesTree, waterBodies);
WaterBody smallest = waterBodiesTree.first();
WaterBody biggest = waterBodiesTree.last();

If you only need to sort your array, the first example I used turns into a one-liner (using the same Comparator as defined earlier):

public static WaterBody mostPowerful(WaterBody[] waterBodies) {
    return Collections.max(waterbodies, cmp);
}
like image 43
Neuron Avatar answered Apr 15 '23 02:04

Neuron