Last active
April 1, 2017 00:12
-
-
Save liviutudor/9cdb85850bc4e52a4fa8848de68bba62 to your computer and use it in GitHub Desktop.
Find max int in a collection in java without using streams
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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