Reputation: 279
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.
Upvotes: 2
Views: 7003
Reputation: 989
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);
Upvotes: 2
Reputation: 20415
In Java 8,
java.util.Arrays.parallelSort(d).get(d.size-1)
to get max value in (merge sorted) array.
Upvotes: 0
Reputation: 4535
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;
Upvotes: 1
Reputation: 203
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);
Upvotes: 0
Reputation: 35557
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
Upvotes: 0
Reputation: 5423
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.
Upvotes: 1
Reputation: 1124
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;
}
Upvotes: 0
Reputation: 22972
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.
Upvotes: 4