问题描述
在带lambda b93的JDK 8中,有一个类这可以用于压缩流(这在教程中进行了说明)。此函数:然而在b98中这个消失了。事实上, Streams 类甚至在中的java.util.stream。
是否已移动此功能,如果是的话,我如何使用b98精简地压缩数据流? $ b
我想到的应用程序是中 ,我在这里替换了 功能相当详细的代码(它不使用b98的功能)。 我还需要这个我只是从b93获取源代码并将其放在util类中。我不得不稍微修改它以适应当前的API。 这里是工作代码的参考(承担您的风险......): In JDK 8 with lambda b93 there was a class java.util.stream.Streams.zip in b93 which could be used to zip streams (this is illustrated in the tutorial Exploring Java8 Lambdas. Part 1 by Dhananjay Nene). This function : However in b98 this has disappeared. Infact the Streams class is not even accessible in java.util.stream in b98. Has this functionality been moved, and if so how do I zip streams concisely using b98? The application I have in mind is in this java implementation of Shen, where I replaced the zip functionality in the functions with rather verbose code (which doesn't use functionality from b98). I needed this as well so I just took the source code from b93 and put it in a "util" class. I had to modify it slightly to work with the current API. For reference here's the working code (take it at your own risk...): 这篇关于使用带有lambda的JDK8压缩流(java.util.stream.Streams.zip)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
pre > public static< A,B,C>流< c取代; zip(Stream a,
Stream b,
BiFunction ?extends C> zipper){
Objects.requireNonNull (拉链);
Spliterator Spliterator bSpliterator = Objects.requireNonNull(b).spliterator();
//压缩失去DISTINCT和SORTED特性
int特性= aSpliterator.characteristics()& bSpliterator.characteristics()&
〜(Spliterator.DISTINCT | Spliterator.SORTED);
long zipSize =((characteristics& Spliterator.SIZED)!= 0)
? Math.min(aSpliterator.getExactSizeIfKnown(),bSpliterator.getExactSizeIfKnown())
:-1;
迭代器< A> aIterator = Spliterators.iterator(aSpliterator);
迭代器< B> bIterator = Spliterators.iterator(bSpliterator);
迭代器< C> cIterator = new Iterator< C>(){
@Override
public boolean hasNext(){
return aIterator.hasNext()&& bIterator.hasNext();
}
@Override
public C next(){
return zipper.apply(aIterator.next(),bIterator.next());
}
};
Spliterator< C> split = Spliterators.spliterator(cIterator,zipSize,features);
return(a.isParallel()|| b.isParallel())
? StreamSupport.stream(split,true)
:StreamSupport.stream(split,false);
}
public static<A, B, C> Stream<C> zip(Stream<? extends A> a,
Stream<? extends B> b,
BiFunction<? super A, ? super B, ? extends C> zipper) {
Objects.requireNonNull(zipper);
Spliterator<? extends A> aSpliterator = Objects.requireNonNull(a).spliterator();
Spliterator<? extends B> bSpliterator = Objects.requireNonNull(b).spliterator();
// Zipping looses DISTINCT and SORTED characteristics
int characteristics = aSpliterator.characteristics() & bSpliterator.characteristics() &
~(Spliterator.DISTINCT | Spliterator.SORTED);
long zipSize = ((characteristics & Spliterator.SIZED) != 0)
? Math.min(aSpliterator.getExactSizeIfKnown(), bSpliterator.getExactSizeIfKnown())
: -1;
Iterator<A> aIterator = Spliterators.iterator(aSpliterator);
Iterator<B> bIterator = Spliterators.iterator(bSpliterator);
Iterator<C> cIterator = new Iterator<C>() {
@Override
public boolean hasNext() {
return aIterator.hasNext() && bIterator.hasNext();
}
@Override
public C next() {
return zipper.apply(aIterator.next(), bIterator.next());
}
};
Spliterator<C> split = Spliterators.spliterator(cIterator, zipSize, characteristics);
return (a.isParallel() || b.isParallel())
? StreamSupport.stream(split, true)
: StreamSupport.stream(split, false);
}