本文整理匯總了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();
}