title: Which operations preserve order
body: <p><strong>TL;DR;</strong> I am looking for a place where I can lookup whether a certain intermediate or terminal operation. Where can I find such documentation?</p>
  
  <p><strong>Edit</strong> This is not a duplicate of <a href="https://stackoverflow.com/questions/29216588/">How to ensure order of processing in java8 streams?</a>, since that question does not provide a comprehensive list of operations.</p>
  
  <h1>Background</h1>
  
  <p>The <a href="https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html" rel="noreferrer">the package documentation</a> says:</p>
  
  <blockquote>
    <p>Whether or not a stream has an encounter order depends on the source and the intermediate operations</p>
  </blockquote>
  
  <p>Which is repeated in <a href="https://stackoverflow.com/a/29218074/722929">this excellent stackoverflow answer</a></p>
  
  <blockquote>
    <p>In order to ensure maintenance of ordering throughout an entire stream operation, you have to study the documentation of the stream’s source, all intermediate operations and the terminal operation for whether they maintain the order or not (or whether the source has an ordering in the first place).</p>
  </blockquote>
  
  <p>That is all well and good, but which documentation should I look at? The <a href="https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html" rel="noreferrer">the package documentation</a> mentions in an example that <code>map</code> guarantees ordering, but it doesn't have a exhaustive list. The <a href="https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html" rel="noreferrer">javadoc for the Stream class</a> documents some intermediate operations, but not all.
  Take for example <a href="https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#map-java.util.function.Function-" rel="noreferrer"><code>map</code></a>:</p>
  
  <blockquote>
    <p>Returns a stream consisting of the results of applying the given function to the elements of this stream.</p>
    
    <p>This is an intermediate operation.</p>
  </blockquote>
  
  <p>or <a href="https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#filter-java.util.function.Predicate-" rel="noreferrer"><code>filter</code></a></p>
  
  <blockquote>
    <p>Returns a stream consisting of the elements of this stream that match the given predicate.</p>
    
    <p>This is an intermediate operation.</p>
  </blockquote>
  
  <p>None of these describe whether they preserve ordering.</p>
  
  <p><a href="https://stackoverflow.com/a/35664976/722929">This stackoverflow answer</a> claims:</p>
  
  <blockquote>
    <p>Actually every intermediate operation preserves an order by default. The only exceptions are:</p>
    
    <ul>
    <li>unordered() which removes the ordering constraint.</li>
    <li>sorted() which changes the order.</li>
    </ul>
    
    <p>When it's not explicitly specified, you can assume that operation keeps the order. Even distinct() keeps the order, though it adds much complexity for parallel stream.</p>
  </blockquote>
  
  <p>but is there any official documentation to back that up?</p>
  
  <h1>Extra credit </h1>
  
  <p>The is actually has two separate ordering issues.</p>
  
  <ol>
  <li>Does the output of the operation maintain the same ordering as the input?</li>
  <li>Is the operation executed in-order on each element.</li>
  </ol>
  
  <p>For example, a parallel <code>map</code> operation could go over all elements in an arbitrary order (violating 2.), but still maintain order in the returned stream  (obeying 1.)</p>