Path: csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Eric Sosman Newsgroups: comp.lang.java.programmer Subject: Re: stream concat distinct indeterministic Date: Mon, 15 Feb 2016 16:13:14 -0500 Organization: A noiseless patient Spider Lines: 48 Message-ID: References: <6e75cf37-61c1-4ab8-af9b-851fc280244d@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Mon, 15 Feb 2016 21:10:20 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="2d081e2f7155c28005d4472f67c2f43d"; logging-data="31634"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/qELyTYtyYRR416s64aUI/" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.5.1 In-Reply-To: Cancel-Lock: sha1:Re6c6vZFS1d7V314pxxcPpDm7b4= Xref: csiph.com comp.lang.java.programmer:33830 On 2/15/2016 3:54 PM, Philipp Kraus wrote: > Am Montag, 15. Februar 2016 17:30:23 UTC+1 schrieb Arne Vajhøj: >> On 2/15/2016 11:08 AM, Philipp Kraus wrote: >>> I run Stream.concat to concat two streams. These two streams are >>> "stream" and use a flatMap call. After the concat I run distinct() to >>> create an unique stream of elements, but I get an indeterministic >>> behaviour of my concated stream, so my question is how can I create a >>> concated and distinct stream? >> >> Docs for distinct says: >> >> "For ordered streams, the selection of distinct elements is stable (for >> duplicated elements, the element appearing first in the encounter order >> is preserved.) For unordered streams, no stability guarantees are made." >> >> so I would guess that you would need to do .sorted().distinct() instead >> of just .distinct(). > > Thanks, this seems to be working, I have added a compareTo (overload) and run sorted() before distinct(). Let's review for a moment. From bits and pieces of several posts, it seems you began with Stream.concat(str1, str2).distinct().count(); ... and reported that the same str1/2 produced different counts in different runs. Now you've changed to Stream.concat(str1, str2).sorted().distinct().count(); ... which shouldn't ("shouldn't") change anything, yet you now report that everything appears to be working. Except, of course, that there's no obvious reason why the original pipeline would misbehave, and no obvious reason why sorted() should make any difference (there are exactly as many elements in ACDB as in ABCD). So I *strongly* encourage you not to let the matter rest, but to look deeper and see what's really going on. You may have uncovered a serious bug in the streams framework -- or you may have made a slip-up elsewhere. The former is possible, but to me the latter seems far more likely. For the last time: SSCCE? -- esosman@comcast-dot-net.invalid "Don't be afraid of work. Make work afraid of you." -- TLM