4

I'm looking to have an array of floats(doubles) & integers as below -

double[] myArr = {0, 0, 95, 9.5, 19, 610.5, 1217, 5.14, 4038.66, 10.23, 7961, 828, 199858, 37325.3};

I then have this function that returns the max & min

    public static void getMaxArrVal(double[] myArr) {           

        myCol = /* Code to convert myarr to collection should come here */
        System.out.println(Collections.min(myCol ));
        System.out.println(Collections.max(myCol ));
    }
}

getMaxArrVal(myArr);

I'm having problems converting primitive data type to a collection.I tried converting this to double list, tried by converting myArr to char/String to check a few things but nothing has worked obviously.

I keep seeing the compilation error of primitive data type. I did try Double too but no luck.

How could I possibly convert the array to collection & calculate min/max on this one. I'd appreciate any help her please.

2
  • 1
    check this stackoverflow.com/questions/5178854/… Commented Mar 6, 2016 at 9:25
  • @prasad is correct; this question is essentially an exact duplicate. Commented Mar 6, 2016 at 9:29

6 Answers 6

4

This works nicely with Java 8:

Arrays.stream(myArr).boxed().collect(Collectors.toCollection(ArrayList::new));

Any Collection may be used instead of ArrayList, of course. The code I have here may actually be simplified to Collectors.toList().

Also, as noted in @nukie's answer, DoubleStream has its own min() and max() methods, rendering the conversion useless. Merely use two streams created using Arrays.stream(myArr) and call these methods.

Sign up to request clarification or add additional context in comments.

Comments

2

Guava comes with nice solution

Lists.newArrayList(double[])

Comments

1
double[] primitives = new double[] { 1.0, 2.0, 3.0};
List<Double> objects = new ArrayList<>();
for(double num : primitives) {
    objects.add(num);
}

Comments

1

Using Arrays.sort will sort your array in ascending order. Therefore, you know the min will be the first element of your array, myArr[0], and your max will be the last element in your array myArr[myArr.length-1].

double[] myArr = {0, 0, 95, 9.5, 19, 610.5, 1217, 5.14, 4038.66, 10.23, 7961,  828, 199858, 37325.3};
Arrays.sort(myArr);
System.out.println(myArr[0]);
System.out.println(myArr[myArr.length-1]);

The following will result in

0.0 <-- min

199858.0 <-- max

2 Comments

This approach will change original array's order. But if OP don't cares - perfect solution.
Thank you Mike. I need the array order to remain the same.
1

if you run this code at java8 you can try lambdas:

Arrays.stream(myArr).max();

and

Arrays.stream(myArr).max();

3 Comments

Please check your spelling, and also your code. This won't work because primitive streams like the generated DoubleStream don't accept Collectors in their collect() method. Stream is not spelt straem.
thank you for remark! this was unexpected for me that DoubleStream not provide collect(Collector) method )
It wouldn't make much sense, as Collectors are generic and you can't have a Collector<double, X, X>. Still, we all wish things like this could be possible sometimes.
1
public static void main(String[] args) throws Exception {

    Double [] myArr = {0., 0., 95., 9.5, 19., 610.5, 1217., 5.14, 4038.66, 10.23,  7961., 828., 199858., 37325.3};      

    Double min = Arrays.stream(myArr).min(Comparator.<Double>naturalOrder()).get();
    Double max = Arrays.stream(myArr).max(Comparator.<Double>naturalOrder()).get();

    System.out.println(min);
    System.out.println(max);
}

If you don't need collection to anything else, except find max and min, create stream right from array. In this case you don't need create separate collection at all.

3 Comments

Do not forget a Stream object can only be used once. Just use Arrays.stream(myArr) twice...
@YassinHajaj I did. And you're right. Thanx for comment.
Thank you Ken. I prefer double as I've written quite a lot of code with that type. So Double doesn't quite work in this situation. However, this is a good future reference for me. Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.