Skip to content

Instantly share code, notes, and snippets.

@archena
Created April 7, 2014 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save archena/10022529 to your computer and use it in GitHub Desktop.
Save archena/10022529 to your computer and use it in GitHub Desktop.
Iterators
boolean r = isOrdered(Arrays.asList(1, 3, 5, 7)); // true
boolean r2 = isOrdered(Arrays.asList(1, 5, 5, 7)); // false
boolean r3 = isOrdered(Arrays.asList(5, 3, 6, 7)); // false
public static <T extends Comparable<T>> boolean isOrdered(Collection<T> collection) {
final Iterator<T> i = collection.iterator();
final Iterator<T> j = collection.iterator();
j.next();
while(j.hasNext())
if(i.next().compareTo(j.next()) >= 0)
return false;
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment