-1

I have 20 local parameters in a class and I want to know the efficient way to get the maximum value and total sum among all the parameters. Thank you in advance!

my code is something like

    public class EntityOfTheQuery{
    double value1;
    double value2;
    ...
    double value3;

    // this is the function I need 
    public void maxValue(){
    }
  }
Xiahao Wang
  • 45
  • 1
  • 3
  • 10
  • 5
    Why don't you use an array? `double[] value = new double[20];`. Other than your options are limited. – Absurd-Mind May 26 '14 at 12:20
  • one efficient method may be to use reflection – Macrosoft-Dev May 26 '14 at 12:21
  • I believe you have tried something on that front.. – TheLostMind May 26 '14 at 12:22
  • If you can add the values in an array you can watch [this](http://stackoverflow.com/questions/16325168/how-would-i-find-the-maximum-value-in-an-array) – Paolo Forgia May 26 '14 at 12:24
  • Find max or min in a a collection of objects it will be always O(n) in time even if you have not further informations about the collection. Therefore, no matter what approach you use because every element must be read and the cost is linear. – Emisilve86 May 26 '14 at 12:39

4 Answers4

1
public double maxValue(){
    double[] a = {value1,value2,...,value20};
    List b = Arrays.asList(ArrayUtils.toObject(a));
    return Collections.max(b);
}

You could do it.

Kabulan0lak
  • 2,116
  • 1
  • 19
  • 34
0

Take a array of size 20 instead of 20 different vars.

double[] value = new double[20];
public double maxValue(){
      Arrays.sort(value);
      return value[value.length-1];
    }
Deepu--Java
  • 3,742
  • 3
  • 19
  • 30
0

This can be done by reflection. The following code converts each property of the class into an double and compares it to the others.

public class MyClass {
    double value1;
    double value2;
    double value3;

    double getMax() throws IllegalArgumentException, IllegalAccessException {
        double max = Double.MIN_VALUE;

        for (Field field : this.getClass().getDeclaredFields()) {
            if(max < (double)field.get(this)) {
                max = (double)field.getDouble(this);
            }
        }

        return max;
    }
}

NOTE: You have to ensure that each property can be casted into a double value.

chresse
  • 5,486
  • 3
  • 30
  • 47
0

If the maxValue method is called very frequently I would suggest to build your logic in the setter ie define all fields as private/protected and then also define a variable maxValue. Your setter logic would look something like this.

setValue1(double value1){
    if( getValue1() == maxValue() && value1 < maxValue()){
    //.. Check each of value1 .. 20 for the max Value and Assign if
    }else if(value1 > maxValue()){
    maxValue = value1;
    }
}

This would be more efficient even if you are using an array, as you need to scan the entire array/fields only when the value you are setting is less than the current max value and the maxValue is same as the current value (another optimization when using the array would be to store the index of the maxValue and then scan the entire array only when you are reducing the field value and it is the current maxValue field.

ganaraj
  • 420
  • 3
  • 13