Path: csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Martin Gregorie Newsgroups: comp.lang.java.programmer Subject: Re: stream concat distinct indeterministic Date: Mon, 15 Feb 2016 21:58:17 -0000 (UTC) Organization: A noiseless patient Spider Lines: 43 Message-ID: References: <6e75cf37-61c1-4ab8-af9b-851fc280244d@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Injection-Date: Mon, 15 Feb 2016 21:58:17 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="e5160e2be48f1ea550cf61512446f525"; logging-data="4756"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX199Pm4HPYK5Bz8H/5RQiflRRE8vY21r098=" User-Agent: Pan/0.140 (Chocolate Salty Balls; GIT 3663720 git://git.gnome.org/pan2) Cancel-Lock: sha1:rV8C7iVeamGzu5EOm8le3lnNjb0= Xref: csiph.com comp.lang.java.programmer:33831 On Mon, 15 Feb 2016 16:13:14 -0500, Eric Sosman wrote: > 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. > This doesn't surprise me because the Stream.distinct() method is only guaranteed to produce a stable result from an ordered input stream, which implies that the distinctness comparison is made between adjacent elements in the stream. So, I think there are two possible sources of instability: 1) Philipp hasn't said whether any of the streams being concatenated are ordered, 2) Even if the original streams are all ordered, simply concatenating two or more streams will create a single unordered stream because concat() merely appends the content of Stream b to the content of Stream a So, applying the sorted() to the output of concat() restores stability to distinct()'s operation no matter what the key sequence is in the original streams, which is why it solved Philipp's problem. The only case calling sorted() adds no value is when the input streams are already ordered and each successive stream to be concatenated contains a key range whose lowest value is greater than or equal to the highest key value in the preceding stream. -- martin@ | Martin Gregorie gregorie. | Essex, UK org |