2

I get the five double data type values from five different function.Without adding those value into array is there any efficient java code for get the maximum value from that five value set.

double d1=r1.function1();
double d2=r1.function2();
double d3=r1.function3();
double d4=r1.function4();
double d5=r1.function5();

double[] d=new  double[5];
for(int i=0:i<5;i++){
    d[i]="d"+i;
}
double x=max(d);

public static double max(double[] t) {
    double maximum = t[0];   // start with the first value
    for (int i=1; i<t.length; i++) {
        if (t[i] > maximum) {
            maximum = t[i];   // new maximum
        }
    }
    return maximum;
}

Without going this much effort is there any efficient way to get the maximum value from above double type data set? And also when adding value to the loop there also some error represent in d[i]="d"+i; part. Please provide me better solution.

stealthjong
  • 10,858
  • 13
  • 45
  • 84
Maduri
  • 249
  • 1
  • 5
  • 19

8 Answers8

4

You can use ArrayList<Double> and than

    ArrayList<Double> alist=new ArrayList<Double>();
    //Add double values to this list

You can use Collections.max(alist) to get maximum value and Collections.min(alist) to get minimun value.

akash
  • 22,664
  • 11
  • 59
  • 87
1

You could use varargs instead of an array.

public double max(double... values) {
    int max = -Double.MAX_VALUE;
    for(double value : values) {
        if(value > max) {
            max = value;
        }
    }
    return max;
}

Then you call your method like double max = max(d1, d2, d3, d4, d5);

EDIT :

Your lines

for(int i=0:i<5;i++){
    d[i]="d"+i;
}

don't work, because "d"+i will create a String due to string concatentation and will not be the double variable with that name. You cannot fill your array like this. You will have to do:

d[0] = d1;
d[1] = d2;
d[2] = d3;
d[3] = d4;
d[4] = d5;
QBrute
  • 4,405
  • 6
  • 34
  • 40
1
Collections.max(Arrays.asList(d));

if would be more efficient if instead of having d as an array. have it as list to begin with.

nafas
  • 5,283
  • 3
  • 29
  • 57
  • 1
    *Without adding those value into array is there any efficient java code for get the maximum value from that five value set.* – TheLostMind Aug 01 '14 at 12:00
  • @TheLostMind if you have 5 values (not in list or array) the only way is to compare all the values against each other. also the solution would be only good for 5 values , no more, no less. – nafas Aug 01 '14 at 12:20
  • my point exactly.. That's why I've voted to close it.. there is *no efficient* solution. :) – TheLostMind Aug 01 '14 at 12:21
  • @TheLostMind the efficient solution would be to store values in a list. then use Collections class. – nafas Aug 01 '14 at 12:25
  • @TheLostMind thats true...I also think best way to add values into arraylist and then max method.Thank you for all. :) (and @nafas) – Maduri Aug 01 '14 at 13:27
1

In Java 8:

Arrays.stream(darray).max().getAsDouble()

Full method (in our case, return zero if the array is empty; change your default as desired).

public static double max(final @Nullable double... values)
{
    return ((values != null) && (values.length > 0)) ? Arrays.stream(values).max().getAsDouble() : 0;
}

This method can be called with either an array or varargs parameter:

double[] darray = new double[]{43.7, 65.2, 99.1, 87.6};
double max1 = max(darray);
double max2 = max(1.2, 3.4, 5.6);
Rob Stoecklein
  • 749
  • 6
  • 9
0

You dont have to put them in array yourself, let Java do that part of work using varargs:

public static double max(Double... numbers){
    double max = numbers[0];

    for(int i = 0; i < numbers.length; i++){
        if(numbers[i] > max){
            max = numbers[i];
        }
    }

    return max;
}
kevdev
  • 1,054
  • 8
  • 9
0

You can add d1,d2,d3,d4,d5 to TreeSet

Set<Double> set=new TreeSet<>(); // while you adding set will be sorted
// add values to set
List<Double> setAsList=new ArrayList(set); // convert set to list
System.out.println(setAsList.get(setAsList.size()-1)); // last value is the max
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
0
List <Double> d = new ArrayList<>();
 d. add (d1);
d. add (d2);
d. add (d3);
d. add (d4);
d. add (d5);
Double max=Collections. max (d);
Ritesh Bhakre
  • 203
  • 3
  • 10
  • To improve your answer you could briefly explain how the `.Max` function is a general function of Enumerable objects to make the usage clearer. – Jens Aug 01 '14 at 12:33
  • I deemed it ineligible and self-evident all by itself, the code according to me was self-explanatory. . Though I could've footnoted the code as you adjudged. . As of now I shall see to it that I annotate my work – Ritesh Bhakre Aug 01 '14 at 19:00
0

In Java 8,

java.util.Arrays.parallelSort(d).get(d.size-1)

to get max value in (merge sorted) array.

elm
  • 20,117
  • 14
  • 67
  • 113