本文整理汇总了Java中java.util.Spliterator.tryAdvance方法的典型用法代码示例。如果您正苦于以下问题:Java Spliterator.tryAdvance方法的具体用法?Java Spliterator.tryAdvance怎么用?Java Spliterator.tryAdvance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.Spliterator
的用法示例。
在下文中一共展示了Spliterator.tryAdvance方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testSplitAfterFullTraversal
import java.util.Spliterator; //导入方法依赖的package包/类
private static <T, S extends Spliterator<T>> void testSplitAfterFullTraversal(
Supplier<S> supplier,
UnaryOperator<Consumer<T>> boxingAdapter) {
// Full traversal using tryAdvance
Spliterator<T> spliterator = supplier.get();
while (spliterator.tryAdvance(boxingAdapter.apply(e -> { }))) { }
Spliterator<T> split = spliterator.trySplit();
assertNull(split);
// Full traversal using forEach
spliterator = supplier.get();
spliterator.forEachRemaining(boxingAdapter.apply(e -> { }));
split = spliterator.trySplit();
assertNull(split);
// Full traversal using tryAdvance then forEach
spliterator = supplier.get();
spliterator.tryAdvance(boxingAdapter.apply(e -> { }));
spliterator.forEachRemaining(boxingAdapter.apply(e -> { }));
split = spliterator.trySplit();
assertNull(split);
}
示例2: distinct
import java.util.Spliterator; //导入方法依赖的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: mixedTraverseAndSplit
import java.util.Spliterator; //导入方法依赖的package包/类
static<U> void mixedTraverseAndSplit(Consumer<U> b, Spliterator<U> splTop) {
Spliterator<U> spl1, spl2, spl3;
splTop.tryAdvance(b);
spl2 = splTop.trySplit();
if (spl2 != null) {
spl2.tryAdvance(b);
spl1 = spl2.trySplit();
if (spl1 != null) {
spl1.tryAdvance(b);
spl1.forEachRemaining(b);
}
spl2.tryAdvance(b);
spl2.forEachRemaining(b);
}
splTop.tryAdvance(b);
spl3 = splTop.trySplit();
if (spl3 != null) {
spl3.tryAdvance(b);
spl3.forEachRemaining(b);
}
splTop.tryAdvance(b);
splTop.forEachRemaining(b);
}
示例4: testSplitAfterFullTraversal
import java.util.Spliterator; //导入方法依赖的package包/类
private static <T, S extends Spliterator<T>> void testSplitAfterFullTraversal(
Supplier<S> supplier,
UnaryOperator<Consumer<T>> boxingAdapter) {
// Full traversal using tryAdvance
Spliterator<T> spliterator = supplier.get();
while (spliterator.tryAdvance(boxingAdapter.apply(e -> { }))) { }
Spliterator<T> split = spliterator.trySplit();
assertNull(split);
// Full traversal using forEach
spliterator = supplier.get();
spliterator.forEachRemaining(boxingAdapter.apply(e -> {
}));
split = spliterator.trySplit();
assertNull(split);
// Full traversal using tryAdvance then forEach
spliterator = supplier.get();
spliterator.tryAdvance(boxingAdapter.apply(e -> { }));
spliterator.forEachRemaining(boxingAdapter.apply(e -> {
}));
split = spliterator.trySplit();
assertNull(split);
}
示例5: truncate
import java.util.Spliterator; //导入方法依赖的package包/类
/**
* Return a node describing a subsequence of the elements of this node,
* starting at the given inclusive start offset and ending at the given
* exclusive end offset.
*
* @param from The (inclusive) starting offset of elements to include, must
* be in range 0..count().
* @param to The (exclusive) end offset of elements to include, must be
* in range 0..count().
* @param generator A function to be used to create a new array, if needed,
* for reference nodes.
* @return the truncated node
*/
default Node<T> truncate(long from, long to, IntFunction<T[]> generator) {
if (from == 0 && to == count())
return this;
Spliterator<T> spliterator = spliterator();
long size = to - from;
Node.Builder<T> nodeBuilder = Nodes.builder(size, generator);
nodeBuilder.begin(size);
for (int i = 0; i < from && spliterator.tryAdvance(e -> { }); i++) { }
if (to == count()) {
spliterator.forEachRemaining(nodeBuilder);
} else {
for (int i = 0; i < size && spliterator.tryAdvance(nodeBuilder); i++) { }
}
nodeBuilder.end();
return nodeBuilder.build();
}
示例6: forEach
import java.util.Spliterator; //导入方法依赖的package包/类
@Override
<E> void forEach(Spliterator<E> spliterator, Consumer<? super E> consumer) {
while (spliterator.tryAdvance(consumer)) {
Spliterator<E> prefix = trySplitTestingSize(spliterator);
if (prefix != null) {
forEach(prefix, consumer);
}
}
}
示例7: concat
import java.util.Spliterator; //导入方法依赖的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);
}
示例8: filterEvenLine
import java.util.Spliterator; //导入方法依赖的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);
}
示例9: lateBindingTestWithTryAdvance
import java.util.Spliterator; //导入方法依赖的package包/类
@Test(dataProvider = "Source")
public <T> void lateBindingTestWithTryAdvance(String description, Supplier<Source<T>> ss) {
Source<T> source = ss.get();
Collection<T> c = source.asCollection();
Spliterator<T> s = c.spliterator();
source.update();
Set<T> r = new HashSet<>();
while (s.tryAdvance(r::add)) { }
assertEquals(r, new HashSet<>(c));
}
示例10: testTryAdvance
import java.util.Spliterator; //导入方法依赖的package包/类
@Test(dataProvider = "Source")
public <T> void testTryAdvance(String description, Supplier<Source<T>> ss) {
Source<T> source = ss.get();
Spliterator<T> s = source.spliterator();
source.update();
Set<T> a = new HashSet<>();
while (s.tryAdvance(a::add)) {
}
Set<T> e = new HashSet<>();
source.spliterator().forEachRemaining(e::add);
assertEquals(a, e);
}
示例11: filter
import java.util.Spliterator; //导入方法依赖的package包/类
/** Returns a {@code Spliterator} filtered by the specified predicate. */
static <T> Spliterator<T> filter(Spliterator<T> fromSpliterator, Predicate<? super T> predicate) {
checkNotNull(fromSpliterator);
checkNotNull(predicate);
class Splitr implements Spliterator<T>, Consumer<T> {
T holder = null;
@Override
public void accept(T t) {
this.holder = t;
}
@Override
public boolean tryAdvance(Consumer<? super T> action) {
while (fromSpliterator.tryAdvance(this)) {
try {
if (predicate.test(holder)) {
action.accept(holder);
return true;
}
} finally {
holder = null;
}
}
return false;
}
@Override
public Spliterator<T> trySplit() {
Spliterator<T> fromSplit = fromSpliterator.trySplit();
return (fromSplit == null) ? null : filter(fromSplit, predicate);
}
@Override
public long estimateSize() {
return fromSpliterator.estimateSize() / 2;
}
@Override
public Comparator<? super T> getComparator() {
return fromSpliterator.getComparator();
}
@Override
public int characteristics() {
return fromSpliterator.characteristics()
& (Spliterator.DISTINCT
| Spliterator.NONNULL
| Spliterator.ORDERED
| Spliterator.SORTED);
}
}
return new Splitr();
}
示例12: forEachWithCancel
import java.util.Spliterator; //导入方法依赖的package包/类
@Override
final void forEachWithCancel(Spliterator<P_OUT> spliterator, Sink<P_OUT> sink) {
do { } while (!sink.cancellationRequested() && spliterator.tryAdvance(sink));
}
示例13: truncate
import java.util.Spliterator; //导入方法依赖的package包/类
/**
* Return a node describing a subsequence of the elements of this node,
* starting at the given inclusive start offset and ending at the given
* exclusive end offset.
*
* @param from The (inclusive) starting offset of elements to include, must
* be in range 0..count().
* @param to The (exclusive) end offset of elements to include, must be
* in range 0..count().
* @param generator A function to be used to create a new array, if needed,
* for reference nodes.
* @return the truncated node
*/
default Node<T> truncate(long from, long to, IntFunction<T[]> generator) {
if (from == 0 && to == count())
return this;
Spliterator<T> spliterator = spliterator();
long size = to - from;
Node.Builder<T> nodeBuilder = Nodes.builder(size, generator);
nodeBuilder.begin(size);
for (int i = 0; i < from && spliterator.tryAdvance(e -> { }); i++) { }
for (int i = 0; (i < size) && spliterator.tryAdvance(nodeBuilder); i++) { }
nodeBuilder.end();
return nodeBuilder.build();
}