等价于Scala drop

等价于Scala drop,第1张

等价于Scala drop

这种 *** 作不是

Stream
s 的预期用例,因为它并入了元素之间的依赖关系。因此,该解决方案可能看起来不太好,因为您必须为谓词引入一个全状态变量:

class MutableBoolean { boolean b; }MutableBoolean inTail = new MutableBoolean();IntStream.of(0, 1, 2, 3, 0, 1, 2, 3, 4)         .filter(i -> inTail.b || i >= 3 && (inTail.b = true))         .forEach(System.out::println);

请注意,与您的示例相比,该条件必须颠倒。

当然,您可以在方法中隐藏令人讨厌的细节:

public static void main(String... arg) {    dropWhile(n -> n < 3, Stream.of(0, 1, 2, 3, 0, 1, 2, 3, 4))      .forEach(System.out::println);}static <T> Stream<T> dropWhile(Predicate<T> p, Stream<T> s) {    class MutableBoolean { boolean b; }    MutableBoolean inTail = new MutableBoolean();    return s.filter(i -> inTail.b || !p.test(i) && (inTail.b = true));}

一个更复杂,但更清洁,可能更有效的方法是深入研究金属,即

Spliterator
界面:

static <T> Stream<T> dropWhile(Predicate<T> p, Stream<T> s) {    Spliterator<T> sp = s.spliterator();    return StreamSupport.stream(new Spliterators.AbstractSpliterator<T>( sp.estimateSize(), sp.characteristics() & ~Spliterator.SIZED) {        boolean dropped;        public boolean tryAdvance(Consumer<? super T> action) { if(dropped) return sp.tryAdvance(action); do {} while(!dropped && sp.tryAdvance(t -> {     if(!p.test(t)) {         dropped=true;         action.accept(t);     } })); return dropped;        }        public void forEachRemaining(Consumer<? super T> action) { while(!dropped) if(!tryAdvance(action)) return; sp.forEachRemaining(action);        }    }, s.isParallel());}

可以使用与第

dropWhile
一种方法相同的方法,但是即使并行流也可以使用,尽管效率不如您期望的那样。



欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5478157.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存