Skip to content

Instantly share code, notes, and snippets.

@liviutudor
Last active April 1, 2017 00:12
Show Gist options
  • Save liviutudor/9cdb85850bc4e52a4fa8848de68bba62 to your computer and use it in GitHub Desktop.
Save liviutudor/9cdb85850bc4e52a4fa8848de68bba62 to your computer and use it in GitHub Desktop.
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