Maximal element in an array in Java (Collections.max() for integer arrays int[])
Asked Answered
C

5

6

Is there anything like Collections.max which finds the maximal value in an array for regular arrays (such as an int[]) in the standard Java runtime library?

Coulombe answered 25/12, 2008 at 18:5 Comment(0)
J
0

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;
}
Jasmin answered 25/12, 2008 at 19:28 Comment(5)
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[]...Coulombe
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.Jasmin
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; }Tankoos
@Tankoos Thanks for the comment. You are absolutely right and I have updated the answer.Jasmin
You should probably throw an explicit NoSuchElementException if collection/array is empty/null.Trombone
C
6

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;
Cloudcapped answered 16/5, 2009 at 19:36 Comment(0)
E
1

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.

Embryotomy answered 25/12, 2008 at 19:14 Comment(0)
J
0

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;
}
Jasmin answered 25/12, 2008 at 19:28 Comment(5)
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[]...Coulombe
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.Jasmin
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; }Tankoos
@Tankoos Thanks for the comment. You are absolutely right and I have updated the answer.Jasmin
You should probably throw an explicit NoSuchElementException if collection/array is empty/null.Trombone
N
0

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.

Nahuatl answered 25/12, 2008 at 19:31 Comment(0)
L
0

This is straightforward to do using streams, added in Java 8:

int[] array = {1, 5, 3};

OptionalInt max = Arrays.stream(array).max();
Lamee answered 29/11, 2023 at 7:35 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.