views:

2528

answers:

5

Is there anything like Collections.max which finds the maximal value in an array for regular arrays in the standard java runtime library?

A: 

As far as I know, no. You could look at asList (http://java.sun.com/j2se/1.5.0/docs/api/java/util/Arrays.html#asList(T...)), but it's probably not worth it.

Matthew Flaschen
I know about asList, but I don't want the overhead. Finding a maximal value in an array is extremely simple, and I figured it'll be somewhere in JRE's lib.
Arrays.asList on int[] will actually give you a List<int[]> containing a single element. It could be overloaded for the primitives, but that would lead to other problems.
Tom Hawtin - tackline
+1  A: 

You could use Arrays.sort(int[]) and then access the first (or last) element of it. Or you could simply iterate over the array and look for the largest/biggest element. That’s basically a no-brainer.

Bombe
+1  A: 

No, there is no Arrays.max or a lookalike, at least in Java 6.

If you look at the signature and implementation of Collections.max, it makes quite heavy use of parameterized types. In Java, generic arrays are problematic to say the least, so maybe therefore it is not a good idea to provide a generic max implementation for arrays in Java, and keep the focus on (generic) collections.

Edit: as newacct correctly points out, the usage of generic arrays is not necessarily more problematic than the usage of generic collections, so I've edited the above text since the original was wrong. Still, the main argument of "generic arrays are problematic" is still valid in my opinion, and collections should be preferred over reference type arrays.

public static <T> T max(Collection<? extends T> coll, Comparator<? super T> comp) {
    if (comp==null)
        return (T)max((Collection<SelfComparable>) (Collection) coll);

Iterator<? extends T> i = coll.iterator();
T candidate = i.next();

    while (i.hasNext()) {
    T next = i.next();
    if (comp.compare(next, candidate) > 0)
 candidate = next;
}
return candidate;
}
eljenso
I didn't look for generic max, just for plain ol' integer max. Just like Math.max doesn't work with generic comparator. There are some examples in the API for functions that works for numbers arrays and are overloaded for int[] float[] long[] short[] etc[]...
If you were looking for "plain ol' integer max", the first line of my answer, the one that starts with "No", is applicable. If you ask "is there anything like Collections.max" I think it is appropriate to mention this method, its genericity and the implications for arrays in an answer.
eljenso
There is nothing more "problematic" about *using* generic arrays than other generic collections. The code you posted above is equivalent to: public static <T> T max(T[] arr, Comparator<? super T> comp) {T candidate = arr[0]; for (int i = 1; i < arr.length; i++) { T next = arr[i]; if (comp.compare(next, candidate) > 0) candidate = next;}return candidate;}
newacct
@newacct Thanks for the comment. You are absolutely right and I have updated the answer.
eljenso
You should probably throw an explicit NoSuchElementException if collection/array is empty/null.
egaga
A: 

You could also create a decorator for Collection which contains extra methods like getMaximumValue() and make it update the value returned every time element is added/removed if there's need.

This would only make sense though if you'd use the maximum value a lot in your program which would mean that iterating through the list every time would cause significant overhead.

Esko
A: 

If you have an array of Objects you can use

Collections.max(Arrays.asList(array));

If you have an array of primitive you can just use a simple loop.

long[] array;
long max = array[0];
for(long l : array) if (max < l) max = l;
Peter Lawrey