本文整理匯總了Java中java.util.stream.Stream.reduce方法的典型用法代碼示例。如果您正苦於以下問題:Java Stream.reduce方法的具體用法?Java Stream.reduce怎麽用?Java Stream.reduce使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.util.stream.Stream
的用法示例。
在下文中一共展示了Stream.reduce方法的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: map
import java.util.stream.Stream; //導入方法依賴的package包/類
public static <I, O> List<O> map(Stream<I> stream, Function<I, O> mapper) {
return stream.reduce(new ArrayList<O>(), (acc, x) -> {
// We are copying data from acc to new list instance. It is very inefficient,
// but contract of Stream.reduce method requires that accumulator function does
// not mutate its arguments.
// Stream.collect method could be used to implement more efficient mutable reduction,
// but this exercise asks to use reduce method.
List<O> newAcc = new ArrayList<>(acc);
newAcc.add(mapper.apply(x));
return newAcc;
}, (List<O> left, List<O> right) -> {
// We are copying left to new list to avoid mutating it.
List<O> newLeft = new ArrayList<>(left);
newLeft.addAll(right);
return newLeft;
});
}
開發者ID:jinyi233,項目名稱:https-github.com-RichardWarburton-java-8-Lambdas-exercises,代碼行數:18,代碼來源:MapUsingReduce.java
示例2: filter
import java.util.stream.Stream; //導入方法依賴的package包/類
public static <I> List<I> filter(Stream<I> stream, Predicate<I> predicate) {
List<I> initial = new ArrayList<>();
return stream.reduce(initial,
(List<I> acc, I x) -> {
if (predicate.test(x)) {
// We are copying data from acc to new list instance. It is very inefficient,
// but contract of Stream.reduce method requires that accumulator function does
// not mutate its arguments.
// Stream.collect method could be used to implement more efficient mutable reduction,
// but this exercise asks to use reduce method explicitly.
List<I> newAcc = new ArrayList<>(acc);
newAcc.add(x);
return newAcc;
} else {
return acc;
}
},
FilterUsingReduce::combineLists);
}
開發者ID:jinyi233,項目名稱:https-github.com-RichardWarburton-java-8-Lambdas-exercises,代碼行數:20,代碼來源:FilterUsingReduce.java
示例3: executeFirstMatchConstraint
import java.util.stream.Stream; //導入方法依賴的package包/類
private List<? extends Element> executeFirstMatchConstraint(
MemgraphCypherQueryContext ctx,
MatchConstraint<?, ?> matchConstraint,
ExpressionScope scope
) {
try {
List<String> labelNames = getLabelNamesFromMatchConstraint(matchConstraint);
ListMultimap<String, CypherAstBase> propertiesMap = getPropertiesMapFromElementPatterns(ctx, matchConstraint.getPatterns());
Iterable<? extends Element> elements;
if (labelNames.size() == 0 && propertiesMap.size() == 0) {
if (matchConstraint instanceof NodeMatchConstraint) {
elements = ctx.getGraph().getVertices(ctx.getFetchHints(), ctx.getAuthorizations());
} else if (matchConstraint instanceof RelationshipMatchConstraint) {
elements = ctx.getGraph().getEdges(ctx.getFetchHints(), ctx.getAuthorizations());
} else {
throw new MemgraphCypherNotImplemented("unexpected constraint type: " + matchConstraint.getClass().getName());
}
} else {
Query query = ctx.getGraph().query(ctx.getAuthorizations());
if (labelNames.size() > 0) {
Stream<String> labelNamesStream = labelNames.stream()
.map(ctx::normalizeLabelName);
if (matchConstraint instanceof NodeMatchConstraint) {
query = labelNamesStream
.reduce(
query,
(q, labelName) -> q.has(ctx.getLabelPropertyName(), labelName),
(q, q2) -> q
);
} else if (matchConstraint instanceof RelationshipMatchConstraint) {
List<String> normalizedLabelNames = labelNamesStream.collect(Collectors.toList());
query = query.hasEdgeLabel(normalizedLabelNames);
} else {
throw new MemgraphCypherNotImplemented("unexpected constraint type: " + matchConstraint.getClass().getName());
}
}
for (Map.Entry<String, CypherAstBase> propertyMatch : propertiesMap.entries()) {
Object value = ctx.getExpressionExecutor().executeExpression(ctx, propertyMatch.getValue(), scope);
if (value instanceof CypherAstBase) {
throw new MemgraphException("unexpected value: " + value.getClass().getName() + ": " + value);
}
if (value instanceof List) {
query.has(propertyMatch.getKey(), Contains.IN, value);
} else {
query.has(propertyMatch.getKey(), value);
}
}
if (matchConstraint instanceof NodeMatchConstraint) {
elements = query.vertices(ctx.getFetchHints());
} else if (matchConstraint instanceof RelationshipMatchConstraint) {
elements = query.edges(ctx.getFetchHints());
} else {
throw new MemgraphCypherNotImplemented("unexpected constraint type: " + matchConstraint.getClass().getName());
}
}
return Lists.newArrayList(elements);
} catch (MemgraphPropertyNotDefinedException e) {
LOGGER.error(e.getMessage());
return Lists.newArrayList();
}
}
示例4: addUp
import java.util.stream.Stream; //導入方法依賴的package包/類
public static int addUp(Stream<Integer> numbers) {
return numbers.reduce(0, (acc, x) -> acc + x);
}
開發者ID:jinyi233,項目名稱:https-github.com-RichardWarburton-java-8-Lambdas-exercises,代碼行數:4,代碼來源:Question1.java
示例5: operate
import java.util.stream.Stream; //導入方法依賴的package包/類
@Override
Object operate(Stream<?> stream) {
return stream.reduce((a, b) -> b);
}
示例6: flatten
import java.util.stream.Stream; //導入方法依賴的package包/類
public static <T> Stream<T> flatten(Stream<Stream<T>> streams){
return streams.reduce(Stream.empty(), Stream::concat);
}
示例7: reduce
import java.util.stream.Stream; //導入方法依賴的package包/類
static Optional<StackFrame> reduce(Stream<StackFrame> stream) {
return stream.reduce((r,f) -> r.getClassName().compareTo(f.getClassName()) > 0 ? f : r);
}
示例8: allInSequence
import java.util.stream.Stream; //導入方法依賴的package包/類
/**
* Execute all the computations in the list in sequence. Return a completable future which will complete as follows:-
* If any succeeded (marked by a completion value of true) complete with true, otherwise complete with false
* @return completable future representing the combined result above
*/
public static CompletableFuture<Boolean> allInSequence(final Stream<Supplier<CompletableFuture<Boolean>>> futureSuppliers) {
return futureSuppliers.reduce(completedFuture(Boolean.FALSE),
(f, a) -> f.thenCompose(b1 -> a.get().thenCompose(b2 -> CompletableFuture.completedFuture(b1 || b2))),
(bf1, bf2) -> bf1.thenCompose(b1 -> bf2.thenCompose(b2 -> CompletableFuture.completedFuture(b1 || b2))));
}
示例9: shortCircuitedFuture
import java.util.stream.Stream; //導入方法依賴的package包/類
public static CompletableFuture<Boolean> shortCircuitedFuture(final Stream<Supplier<CompletableFuture<Boolean>>> futureSuppliers,
final Boolean fallbackOn) {
return futureSuppliers.reduce(completedFuture(!fallbackOn),
(f, a) -> f.thenCompose(b -> (b != fallbackOn) ? a.get() : completedFuture(fallbackOn)),
(bf1, bf2) -> bf1.thenCompose(b -> (b != fallbackOn) ? bf2 : CompletableFuture.completedFuture(fallbackOn)));
}
示例10: compose
import java.util.stream.Stream; //導入方法依賴的package包/類
/**
* Takes a Stream of functions (which take and return the same type) and composes
* them into a single function, applying the provided functions in order
*/
static <T> Function<T, T> compose(Stream<Function<T, T>> functions) {
return functions.reduce(identity(), Function::andThen);
}
示例11: fold
import java.util.stream.Stream; //導入方法依賴的package包/類
/**
* "Multiplies" the given sequence of objects.
* This is the canonical reduction operation on the free monoid, see e.g.
* <a href="https://en.wikipedia.org/wiki/Monoid#Monoids_in_computer_science">
* monoids in computer science</a>.
* @param ts the values to multiply.
* @return the result of multiplying the stream of values.
* @throws NullPointerException if the argument is {@code null} or any of
* the contained objects is {@code null}.
*/
public T fold(Stream<T> ts) {
requireNonNull(ts, "ts");
return ts.reduce(monoid.unit(), monoid::multiply);
}