本文整理汇总了Java中java.util.Spliterators.AbstractSpliterator类的典型用法代码示例。如果您正苦于以下问题:Java AbstractSpliterator类的具体用法?Java AbstractSpliterator怎么用?Java AbstractSpliterator使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
AbstractSpliterator类属于java.util.Spliterators包,在下文中一共展示了AbstractSpliterator类的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: takeWhile
import java.util.Spliterators.AbstractSpliterator; //导入依赖的package包/类
private static <T> Stream<T> takeWhile(List<T> src, Predicate<T> p) {
Spliterator<T> iter = src.spliterator();
Spliterator<T> res = new AbstractSpliterator<T>(Long.MAX_VALUE, Spliterator.ORDERED) {
private boolean hasNext = true;
@Override
public boolean tryAdvance(Consumer<? super T> action) {
if(hasNext)
return iter.tryAdvance(item -> {
if(p.test(item)) action.accept(item);
else hasNext = false;
}) && hasNext;
return false;
}
};
return StreamSupport.stream(res, false);
}
示例2: distinct
import java.util.Spliterators.AbstractSpliterator; //导入依赖的package包/类
public static <T> Stream<T> distinct(Stream<T> src, Comparator<T> cmp) {
Spliterator<T> iter = src.spliterator();
Spliterator<T> res = new AbstractSpliterator<T>(Long.MAX_VALUE, Spliterator.ORDERED ) {
// ArrayList<T> distinctData = new ArrayList<>();
TreeSet<T> distinctData = new TreeSet<>(cmp);
@Override
public boolean tryAdvance(Consumer<? super T> action) {
return iter.tryAdvance( item -> {
// Versão 1: if (!contains(distinctData, cmp, item)) {
// Versão 2: if(!distinctData.stream().anyMatch(e -> cmp.compare(e, item) == 0)) {
// Versão 3:
if (!distinctData.contains(item)) {
distinctData.add(item);
action.accept(item);
}
});
}
};
return StreamSupport.stream(res, false);
}
示例3: zip
import java.util.Spliterators.AbstractSpliterator; //导入依赖的package包/类
/**
* Returns a stream in which each element is the result of passing the corresponding elementY of
* each of {@code streamA} and {@code streamB} to {@code function}.
*
* <p>For example:
*
* <pre>{@code
* Streams.zip(
* Stream.of("foo1", "foo2", "foo3"),
* Stream.of("bar1", "bar2"),
* (arg1, arg2) -> arg1 + ":" + arg2)
* }</pre>
*
* <p>will return {@code Stream.of("foo1:bar1", "foo2:bar2")}.
*
* <p>The resulting stream will only be as long as the shorter of the two input streams; if one
* stream is longer, its extra elements will be ignored.
*
* <p>Note that if you are calling {@link Stream#forEach} on the resulting stream, you might want
* to consider using {@link #forEachPair} instead of this method.
*
* <p><b>Performance note:</b> The resulting stream is not <a
* href="http://gee.cs.oswego.edu/dl/html/StreamParallelGuidance.html">efficiently splittable</a>.
* This may harm parallel performance.
*/
public static <A, B, R> Stream<R> zip(
Stream<A> streamA, Stream<B> streamB, BiFunction<? super A, ? super B, R> function) {
checkNotNull(streamA);
checkNotNull(streamB);
checkNotNull(function);
boolean isParallel = streamA.isParallel() || streamB.isParallel(); // same as Stream.concat
Spliterator<A> splitrA = streamA.spliterator();
Spliterator<B> splitrB = streamB.spliterator();
int characteristics =
splitrA.characteristics()
& splitrB.characteristics()
& (Spliterator.SIZED | Spliterator.ORDERED);
Iterator<A> itrA = Spliterators.iterator(splitrA);
Iterator<B> itrB = Spliterators.iterator(splitrB);
return StreamSupport.stream(
new AbstractSpliterator<R>(
Math.min(splitrA.estimateSize(), splitrB.estimateSize()), characteristics) {
@Override
public boolean tryAdvance(Consumer<? super R> action) {
if (itrA.hasNext() && itrB.hasNext()) {
action.accept(function.apply(itrA.next(), itrB.next()));
return true;
}
return false;
}
},
isParallel);
}
示例4: concat
import java.util.Spliterators.AbstractSpliterator; //导入依赖的package包/类
private static <T> Stream<T> concat(Stream<T> first, Stream<T> second) {
Spliterator<T> iter1 = first.spliterator();
Spliterator<T> iter2 = second.spliterator();
Spliterator<T> iter = new AbstractSpliterator<T>(Long.MAX_VALUE, Spliterator.ORDERED) {
boolean firstFinished = false;
@Override
public boolean tryAdvance(Consumer<? super T> action) {
if(!firstFinished && iter1.tryAdvance(action)) return true;
firstFinished = true;
return iter2.tryAdvance(action);
}
};
return StreamSupport.stream(iter, false);
}
示例5: filterEvenLine
import java.util.Spliterators.AbstractSpliterator; //导入依赖的package包/类
public static <T> Stream<T> filterEvenLine(Stream<T> src) {
Spliterator<T> iter = src.spliterator();
Spliterator<T> res = new AbstractSpliterator<T>(Long.MAX_VALUE, Spliterator.ORDERED ) {
@Override
public boolean tryAdvance(Consumer<? super T> action) {
return iter.tryAdvance(item -> {})
? iter.tryAdvance(action)
: false;
}
};
return StreamSupport.stream(res, false);
}
示例6: distinct
import java.util.Spliterators.AbstractSpliterator; //导入依赖的package包/类
public static <T> Stream<T> distinct(Stream<T> src, Comparator<T> cmp) {
Spliterator<T> iter = src.spliterator();
Spliterator<T> res = new AbstractSpliterator<T>(Long.MAX_VALUE, Spliterator.ORDERED ) {
@Override
public boolean tryAdvance(Consumer<? super T> action) {
return false;
}
};
return StreamSupport.stream(res, false);
}
示例7: zip
import java.util.Spliterators.AbstractSpliterator; //导入依赖的package包/类
/**
* Returns a stream in which each element is the result of passing the corresponding element of
* each of {@code streamA} and {@code streamB} to {@code function}.
*
* <p>For example:
*
* <pre>{@code
* Streams.zip(
* Stream.of("foo1", "foo2", "foo3"),
* Stream.of("bar1", "bar2"),
* (arg1, arg2) -> arg1 + ":" + arg2)
* }</pre>
*
* <p>will return {@code Stream.of("foo1:bar1", "foo2:bar2")}.
*
* <p>The resulting stream will only be as long as the shorter of the two input streams; if one
* stream is longer, its extra elements will be ignored.
*
* <p>The resulting stream is not <a
* href="http://gee.cs.oswego.edu/dl/html/StreamParallelGuidance.html">efficiently splittable</a>.
* This may harm parallel performance.
*/
public static <A, B, R> Stream<R> zip(
Stream<A> streamA, Stream<B> streamB, BiFunction<? super A, ? super B, R> function) {
checkNotNull(streamA);
checkNotNull(streamB);
checkNotNull(function);
boolean isParallel = streamA.isParallel() || streamB.isParallel(); // same as Stream.concat
Spliterator<A> splitrA = streamA.spliterator();
Spliterator<B> splitrB = streamB.spliterator();
int characteristics =
splitrA.characteristics()
& splitrB.characteristics()
& (Spliterator.SIZED | Spliterator.ORDERED);
Iterator<A> itrA = Spliterators.iterator(splitrA);
Iterator<B> itrB = Spliterators.iterator(splitrB);
return StreamSupport.stream(
new AbstractSpliterator<R>(
Math.min(splitrA.estimateSize(), splitrB.estimateSize()), characteristics) {
@Override
public boolean tryAdvance(Consumer<? super R> action) {
if (itrA.hasNext() && itrB.hasNext()) {
action.accept(function.apply(itrA.next(), itrB.next()));
return true;
}
return false;
}
},
isParallel);
}
示例8: zip
import java.util.Spliterators.AbstractSpliterator; //导入依赖的package包/类
/**
* Returns a stream in which each element is the result of passing the corresponding elementY of
* each of {@code streamA} and {@code streamB} to {@code function}.
*
* <p>For example:
*
* <pre>{@code
* Streams.zip(
* Stream.of("foo1", "foo2", "foo3"),
* Stream.of("bar1", "bar2"),
* (arg1, arg2) -> arg1 + ":" + arg2)
* }</pre>
*
* <p>will return {@code Stream.of("foo1:bar1", "foo2:bar2")}.
*
* <p>The resulting stream will only be as long as the shorter of the two input streams; if one
* stream is longer, its extra elements will be ignored.
*
* <p>Note that if you are calling {@link Stream#forEach} on the resulting stream, you might want
* to consider using {@link #forEachPair} instead of this method.
*
* <p><b>Performance note:</b> The resulting stream is not <a
* href="http://gee.cs.oswego.edu/dl/html/StreamParallelGuidance.html">efficiently splittable</a>.
* This may harm parallel performance.
*/
public static <A, B, R> Stream<R> zip(
Stream<A> streamA, Stream<B> streamB, BiFunction<? super A, ? super B, R> function) {
checkNotNull(streamA);
checkNotNull(streamB);
checkNotNull(function);
boolean isParallel = streamA.isParallel() || streamB.isParallel(); // same as Stream.concat
Spliterator<A> splitrA = streamA.spliterator();
Spliterator<B> splitrB = streamB.spliterator();
int characteristics =
splitrA.characteristics()
& splitrB.characteristics()
& (Spliterator.SIZED | Spliterator.ORDERED);
Iterator<A> itrA = Spliterators.iterator(splitrA);
Iterator<B> itrB = Spliterators.iterator(splitrB);
return StreamSupport.stream(
new AbstractSpliterator<R>(
Math.min(splitrA.estimateSize(), splitrB.estimateSize()), characteristics) {
@Override
public boolean tryAdvance(Consumer<? super R> action) {
if (itrA.hasNext() && itrB.hasNext()) {
action.accept(function.apply(itrA.next(), itrB.next()));
return true;
}
return false;
}
},
isParallel)
.onClose(streamA::close)
.onClose(streamB::close);
}