Questions about Stream/Iterable/Files / FOLDING Collector
paul.sandoz at oracle.com
Mon Nov 9 09:43:34 UTC 2015
"Let a thousand flowers bloom” :-) It’s great you can do this.
For the moment i am ok with waiting until we have better mechanisms in the language platform to do even better [*], then we can gather use-cases based on developer experience such as yours.
[*] FWIW we experimented a couple of times with a Map/BiStream abstraction, and each time decided to back away.
> On 7 Nov 2015, at 05:49, Tagir F. Valeev <amaembo at gmail.com> wrote:
> PS> In other cases e.g. about preceding elements, a
> PS> history-based wrapping spliterator could work (IIRC Jose Paumard
> PS> has presented such examples), but we are currently lacking an SPI
> PS> to plug-in operations, so one needs to directly use the Stream.spliterator escape.
> Actually I wrote a spliterator in my StreamEx library which allows to
> process pairs of input elements (along with primitive
> specializations), of course using Stream.spliterator escape (which is
> not that bad if you avoid tryAdvance call):
> Despite it's not lock-free, it greatly parallelizes and has good
> overall performance, so I'm somewhat proud of it. It allows to solve
> many interesting problems.
> Pairwise differences:
> int diff = IntStreamEx.of(intArray).pairMap((a, b) -> b-a).toArray();
> Skip last stream element:
> Stream<T> stream = StreamEx.of(input).pairMap((a, b) -> a);
> Check if input is sorted:
> StreamEx.of(input).pairMap(Comparable::compareTo).allMatch(r -> r <= 0);
> Find first misplaced element:
> StreamEx.of(input).pairMap((a, b) -> a.compareTo(b) > 0 ? b : null).nonNull().findFirst();
> More complex example: sort word list and add header before each letter:
> .prepend(" ") // Stream.concat(Stream.of(" "), this)
> .pairMap((a, b) -> a.charAt(0) == b.charAt(0) ? Stream.of(b) :
> Stream.of("Words starting with letter "+b.substring(0,1), b))
> And so on. It would be great to see such feature in JDK as well!
More information about the core-libs-dev