Java 8 streams: process every possible pair of elements from list
<p>I have a <code>Collection</code> of elements of an arbitrary class. I want to iterate through the collection and perform some operation using the element and each other element of the collection one-by-one (excluding the element itself). Let it be <code>List&lt;Integer&gt;</code> for simplicity:</p>

<pre><code>List&lt;Integer&gt; list = Arrays.asList(1, 2, 3, 4, 5);
</code></pre>

<p>With <code>for</code> loops it will be:</p>

<pre><code>for (Integer i : list) {
    for (Integer j : list) {
        if (!i.equals(j)) System.out.println(i * 2 + j);  //just for example
    }
}
</code></pre>

<p>The question is <strong>how to do it with the <a href="https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html" rel="noreferrer">Stream API</a>?</strong></p>

<p>That's what I have come to:</p>

<pre><code>list.stream().forEach(i -&gt;
    list.stream().forEach(j -&gt; {
        if (!i.equals(j)) System.out.println(i * 2 + j);
    })
);
</code></pre>

<p>It doesn't look better than nested loop though. Is there a more elegant way?</p>