Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Find max int in a collection in java without using streams
public Integer max(Collection<Integer> collection) {
if( collection == null || collection.isEmpty() ) return null;
Iterator<Integer> i = collection.iterator();
int max = i.next();
while (i.hasNext()) {
int n = i.next();
if (max < n) max = n;
}
return max;
}
//usage
List<Integer> list = ...;
Integer m = max(list);
int maximum = (m == null) ? 0 : m.intVal();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment