How should I check whether a Stream<T> is sorted?
问题 With an Iterable<T> , it's easy: T last = null; for (T t : iterable) { if (last != null && last.compareTo(t) > 0) { return false; } last = t; } return true; But I can't think of a clean way to do the same thing for a Stream<T> that avoids consuming all the elements when it doesn't have to. 回答1: There are several methods to iterate over the successive pairs of the stream. For example, you can check this question. Of course my favourite method is to use the library I wrote: boolean unsorted =