本文整理汇总了Java中java.util.function.LongConsumer类的典型用法代码示例。如果您正苦于以下问题:Java LongConsumer类的具体用法?Java LongConsumer怎么用?Java LongConsumer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
LongConsumer类属于java.util.function包,在下文中一共展示了LongConsumer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: tryAdvance
import java.util.function.LongConsumer; //导入依赖的package包/类
@Override
public boolean tryAdvance(LongConsumer consumer) {
Objects.requireNonNull(consumer);
final long i = from;
if (i < upTo) {
from++;
consumer.accept(i);
return true;
}
else if (last > 0) {
last = 0;
consumer.accept(i);
return true;
}
return false;
}
示例2: peek
import java.util.function.LongConsumer; //导入依赖的package包/类
@Override
public final LongStream peek(LongConsumer action) {
Objects.requireNonNull(action);
return new StatelessOp<Long>(this, StreamShape.LONG_VALUE,
0) {
@Override
Sink<Long> opWrapSink(int flags, Sink<Long> sink) {
return new Sink.ChainedLong<Long>(sink) {
@Override
public void accept(long t) {
action.accept(t);
downstream.accept(t);
}
};
}
};
}
示例3: iterate
import java.util.function.LongConsumer; //导入依赖的package包/类
/**
* Returns an infinite sequential ordered {@code LongStream} produced by iterative
* application of a function {@code f} to an initial element {@code seed},
* producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
* {@code f(f(seed))}, etc.
*
* <p>The first element (position {@code 0}) in the {@code LongStream} will
* be the provided {@code seed}. For {@code n > 0}, the element at position
* {@code n}, will be the result of applying the function {@code f} to the
* element at position {@code n - 1}.
*
* <p>The action of applying {@code f} for one element
* <a href="../concurrent/package-summary.html#MemoryVisibility"><i>happens-before</i></a>
* the action of applying {@code f} for subsequent elements. For any given
* element the action may be performed in whatever thread the library
* chooses.
*
* @param seed the initial element
* @param f a function to be applied to the previous element to produce
* a new element
* @return a new sequential {@code LongStream}
*/
public static LongStream iterate(final long seed, final LongUnaryOperator f) {
Objects.requireNonNull(f);
Spliterator.OfLong spliterator = new Spliterators.AbstractLongSpliterator(Long.MAX_VALUE,
Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL) {
long prev;
boolean started;
@Override
public boolean tryAdvance(LongConsumer action) {
Objects.requireNonNull(action);
long t;
if (started)
t = f.applyAsLong(prev);
else {
t = seed;
started = true;
}
action.accept(prev = t);
return true;
}
};
return StreamSupport.longStream(spliterator, false);
}
示例4: forEachLong
import java.util.function.LongConsumer; //导入依赖的package包/类
@Override
public Array<T> forEachLong(LongConsumer consumer) {
final int length = length();
if (isParallel() && length > 0) {
final int processors = Runtime.getRuntime().availableProcessors();
final int splitThreshold = parallel ? Math.max(length() / processors, 10000) : Integer.MAX_VALUE;
final ForEach action = new ForEach(0, length - 1, splitThreshold, consumer);
ForkJoinPool.commonPool().invoke(action);
} else {
for (int i=0; i<length; ++i) {
final long value = getLong(i);
consumer.accept(value);
}
}
return this;
}
示例5: computeUserRanking
import java.util.function.LongConsumer; //导入依赖的package包/类
@Override
protected final void computeUserRanking(final Set<RunFile> runs, final long userID,
final LongSet candidateItems, final TreeMap<Double, LongSet> ranking) {
candidateItems.forEach((LongConsumer) itemID -> {
final MutableInt n = new MutableInt(0);
final MutableDouble accum = new MutableDouble(0.0);
runs.forEach(run -> {
final double score = run.getScore(userID, itemID, Double.NaN);
if (!Double.isNaN(score)) {
n.increment();
accum.add(score);
}
});
if (n.get() > 0) {
saveScore(ranking, itemID, computeScore(n.get(), accum.get()));
}
});
}
示例6: testLongForEachRemainingWithNull
import java.util.function.LongConsumer; //导入依赖的package包/类
public void testLongForEachRemainingWithNull() {
PrimitiveIterator.OfLong i = new PrimitiveIterator.OfLong() {
@Override
public long nextLong() {
return 0;
}
@Override
public boolean hasNext() {
return false;
}
};
executeAndCatch(() -> i.forEachRemaining((LongConsumer) null));
executeAndCatch(() -> i.forEachRemaining((Consumer<Long>) null));
}
示例7: testLongForEachRemainingWithNull
import java.util.function.LongConsumer; //导入依赖的package包/类
public void testLongForEachRemainingWithNull() {
PrimitiveIterator.OfLong i = new PrimitiveIterator.OfLong() {
@Override
public long nextLong() {
return 0;
}
@Override
public boolean hasNext() {
return false;
}
};
assertThrowsNPE(() -> i.forEachRemaining((LongConsumer) null));
assertThrowsNPE(() -> i.forEachRemaining((Consumer<Long>) null));
}
示例8: forEachRemaining
import java.util.function.LongConsumer; //导入依赖的package包/类
@Override
public void forEachRemaining(LongConsumer consumer) {
Objects.requireNonNull(consumer);
long i = from;
final long hUpTo = upTo;
int hLast = last;
from = upTo;
last = 0;
while (i < hUpTo) {
consumer.accept(i++);
}
if (hLast > 0) {
// Last element of closed range
consumer.accept(i);
}
}
示例9: testLongOps
import java.util.function.LongConsumer; //导入依赖的package包/类
@Test(dataProvider = "LongStreamTestData", dataProviderClass = LongStreamTestDataProvider.class)
public void testLongOps(String name, final TestData.OfLong data) {
class RecordingConsumer extends AbstractRecordingConsumer<Long> implements LongConsumer {
public void accept(long t) {
list.add(t);
}
}
final RecordingConsumer b = new RecordingConsumer();
withData(data)
.stream(s -> s.peek(b))
.before(b::before)
.after(b::after)
.exercise();
}
示例10: tryAdvance
import java.util.function.LongConsumer; //导入依赖的package包/类
@Override
public boolean tryAdvance(LongConsumer action) {
Objects.requireNonNull(action);
action.accept(s.getAsLong());
return true;
}
示例11: fuseAndPrint
import java.util.function.LongConsumer; //导入依赖的package包/类
/**
* Fuse the specified combination of runs and print the result.
*
* @param fold
* the fold
* @param runs
* the runs of the current combination
* @param allUsers
* all the users in the current combination
* @param outputFile
* the path to the output file
*/
protected final void fuseAndPrint(final int fold, final Set<RunFile> runs,
final LongSet allUsers, final Path outputFile) {
try (final PrintWriter writer = new PrintWriter(Files.newBufferedWriter(outputFile))) {
final LongSet all = HashLongSets.newUpdatableSet(maxRank);
runs.forEach(run -> {
all.addAll(run.getItems());
});
allUsers.forEach((LongConsumer) userID -> {
final TreeMap<Double, LongSet> ranking = new TreeMap<Double, LongSet>(
Collections.reverseOrder());
final LongSet candidateItems = HashLongSets.newUpdatableSet(maxRank);
runs.forEach(run -> {
candidateItems.addAll(run.getRanking(userID).keySet());
});
computeUserRanking(runs, userID, candidateItems, ranking);
printRanking(userID, ranking, writer);
});
} catch (final IOException e) {
throw new RuntimeException(e);
}
}
示例12: forEachRemaining
import java.util.function.LongConsumer; //导入依赖的package包/类
@Override
public void forEachRemaining(LongConsumer action) {
Objects.requireNonNull(action);
if (count == -2) {
action.accept(first);
count = -1;
}
}
示例13: adapt
import java.util.function.LongConsumer; //导入依赖的package包/类
/**
* Adapt a {@code Sink<Long> to an {@code LongConsumer}, ideally simply
* by casting.
*/
private static LongConsumer adapt(Sink<Long> sink) {
if (sink instanceof LongConsumer) {
return (LongConsumer) sink;
} else {
if (Tripwire.ENABLED)
Tripwire.trip(AbstractPipeline.class,
"using LongStream.adapt(Sink<Long> s)");
return sink::accept;
}
}
示例14: forEach
import java.util.function.LongConsumer; //导入依赖的package包/类
@Override
public void forEach(LongConsumer action) {
if (!isParallel()) {
adapt(sourceStageSpliterator()).forEachRemaining(action);
} else {
super.forEach(action);
}
}
示例15: forEachOrdered
import java.util.function.LongConsumer; //导入依赖的package包/类
@Override
public void forEachOrdered(LongConsumer action) {
if (!isParallel()) {
adapt(sourceStageSpliterator()).forEachRemaining(action);
} else {
super.forEachOrdered(action);
}
}