本文整理汇总了Java中java.util.function.BinaryOperator类的典型用法代码示例。如果您正苦于以下问题:Java BinaryOperator类的具体用法?Java BinaryOperator怎么用?Java BinaryOperator使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
BinaryOperator类属于java.util.function包,在下文中一共展示了BinaryOperator类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testIntMethods
import java.util.function.BinaryOperator; //导入依赖的package包/类
public void testIntMethods() {
BinaryOperator<Integer> sum1 = Integer::sum;
IntBinaryOperator sum2 = Integer::sum;
BinaryOperator<Integer> max1 = Integer::max;
IntBinaryOperator max2 = Integer::max;
BinaryOperator<Integer> min1 = Integer::min;
IntBinaryOperator min2 = Integer::min;
Comparator<Integer> cmp = Integer::compare;
int[] numbers = { -1, 0, 1, 100, Integer.MAX_VALUE, Integer.MIN_VALUE };
for (int i : numbers) {
for (int j : numbers) {
assertEquals(i+j, (int) sum1.apply(i, j));
assertEquals(i+j, sum2.applyAsInt(i, j));
assertEquals(Math.max(i,j), (int) max1.apply(i, j));
assertEquals(Math.max(i,j), max2.applyAsInt(i, j));
assertEquals(Math.min(i,j), (int) min1.apply(i, j));
assertEquals(Math.min(i,j), min2.applyAsInt(i, j));
assertEquals(((Integer) i).compareTo(j), cmp.compare(i, j));
}
}
}
示例2: testLongMethods
import java.util.function.BinaryOperator; //导入依赖的package包/类
public void testLongMethods() {
BinaryOperator<Long> sum1 = Long::sum;
LongBinaryOperator sum2 = Long::sum;
BinaryOperator<Long> max1 = Long::max;
LongBinaryOperator max2 = Long::max;
BinaryOperator<Long> min1 = Long::min;
LongBinaryOperator min2 = Long::min;
Comparator<Long> cmp = Long::compare;
long[] numbers = { -1, 0, 1, 100, Long.MAX_VALUE, Long.MIN_VALUE };
for (long i : numbers) {
for (long j : numbers) {
assertEquals(i+j, (long) sum1.apply(i, j));
assertEquals(i+j, sum2.applyAsLong(i, j));
assertEquals(Math.max(i,j), (long) max1.apply(i, j));
assertEquals(Math.max(i,j), max2.applyAsLong(i, j));
assertEquals(Math.min(i,j), (long) min1.apply(i, j));
assertEquals(Math.min(i,j), min2.applyAsLong(i, j));
assertEquals(((Long) i).compareTo(j), cmp.compare(i, j));
}
}
}
示例3: solve
import java.util.function.BinaryOperator; //导入依赖的package包/类
public static <S, T> void solve(
Collection<? extends ProblemNeighborhoodAware<S, T>> problems,
S baseSolution,
Function<? super S, ? extends Collection<T>> getRelatedSources,
BinaryOperator<S> solutionCombiner,
Predicate<S> isUnsatisfiable,
Consumer<S> solutionCallback) {
ProblemContainerNeighbourhoodAware<S, T> result = new ProblemContainerNeighbourhoodAware<>(
getRelatedSources,
solutionCombiner,
isUnsatisfiable,
solutionCallback
);
for(ProblemNeighborhoodAware<S, T> problem : problems) {
result.addToRegularQueue(problem);
}
result.run(baseSolution);
//return result;
}
开发者ID:SmartDataAnalytics,项目名称:SubgraphIsomorphismIndex,代码行数:24,代码来源:ProblemContainerNeighbourhoodAware.java
示例4: toImmutableSortedMap
import java.util.function.BinaryOperator; //导入依赖的package包/类
/**
* Returns a {@link Collector} that accumulates elements into an {@code ImmutableSortedMap} whose
* keys and values are the result of applying the provided mapping functions to the input
* elements.
*
* <p>If the mapped keys contain duplicates (according to the comparator), the the values are
* merged using the specified merging function. Entries will appear in the encounter order of the
* first occurrence of the key.
*
* @since 21.0
*/
@Beta
public static <T, K, V> Collector<T, ?, ImmutableSortedMap<K, V>> toImmutableSortedMap(
Comparator<? super K> comparator,
Function<? super T, ? extends K> keyFunction,
Function<? super T, ? extends V> valueFunction,
BinaryOperator<V> mergeFunction) {
checkNotNull(comparator);
checkNotNull(keyFunction);
checkNotNull(valueFunction);
checkNotNull(mergeFunction);
return Collectors.collectingAndThen(
Collectors.toMap(
keyFunction, valueFunction, mergeFunction, () -> new TreeMap<K, V>(comparator)),
ImmutableSortedMap::copyOfSorted);
}
示例5: toImmutableEnumMap
import java.util.function.BinaryOperator; //导入依赖的package包/类
/**
* Returns a {@link Collector} that accumulates elements into an {@code ImmutableMap} whose keys
* and values are the result of applying the provided mapping functions to the input elements. The
* resulting implementation is specialized for enum key types. The returned map and its views will
* iterate over keys in their enum definition order, not encounter order.
*
* <p>If the mapped keys contain duplicates, the values are merged using the specified merging
* function.
*
* @since 21.0
*/
@Beta
public static <T, K extends Enum<K>, V> Collector<T, ?, ImmutableMap<K, V>> toImmutableEnumMap(
java.util.function.Function<? super T, ? extends K> keyFunction,
java.util.function.Function<? super T, ? extends V> valueFunction,
BinaryOperator<V> mergeFunction) {
checkNotNull(keyFunction);
checkNotNull(valueFunction);
checkNotNull(mergeFunction);
// not UNORDERED because we don't know if mergeFunction is commutative
return Collector.of(
() -> new Accumulator<K, V>(mergeFunction),
(accum, t) -> {
K key = checkNotNull(keyFunction.apply(t), "Null key for input %s", t);
V newValue = checkNotNull(valueFunction.apply(t), "Null value for input %s", t);
accum.put(key, newValue);
},
Accumulator::combine,
Accumulator::toImmutableMap);
}
示例6: testFloatMethods
import java.util.function.BinaryOperator; //导入依赖的package包/类
public void testFloatMethods() {
BinaryOperator<Float> sum1 = Float::sum;
BinaryOperator<Float> max1 = Float::max;
BinaryOperator<Float> min1 = Float::min;
Comparator<Float> cmp = Float::compare;
float[] numbers = { -1, 0, 1, 100, Float.MAX_VALUE, Float.MIN_VALUE };
for (float i : numbers) {
for (float j : numbers) {
assertEquals(i+j, (float) sum1.apply(i, j));
assertEquals(Math.max(i,j), (float) max1.apply(i, j));
assertEquals(Math.min(i,j), (float) min1.apply(i, j));
assertEquals(((Float) i).compareTo(j), cmp.compare(i, j));
}
}
}
示例7: of
import java.util.function.BinaryOperator; //导入依赖的package包/类
/**
* Returns a new {@code Collector} described by the given {@code supplier},
* {@code accumulator}, {@code combiner}, and {@code finisher} functions.
*
* @param supplier The supplier function for the new collector
* @param accumulator The accumulator function for the new collector
* @param combiner The combiner function for the new collector
* @param finisher The finisher function for the new collector
* @param characteristics The collector characteristics for the new
* collector
* @param <T> The type of input elements for the new collector
* @param <A> The intermediate accumulation type of the new collector
* @param <R> The final result type of the new collector
* @throws NullPointerException if any argument is null
* @return the new {@code Collector}
*/
public static<T, A, R> Collector<T, A, R> of(Supplier<A> supplier,
BiConsumer<A, T> accumulator,
BinaryOperator<A> combiner,
Function<A, R> finisher,
Characteristics... characteristics) {
Objects.requireNonNull(supplier);
Objects.requireNonNull(accumulator);
Objects.requireNonNull(combiner);
Objects.requireNonNull(finisher);
Objects.requireNonNull(characteristics);
Set<Characteristics> cs = Collectors.CH_NOID;
if (characteristics.length > 0) {
cs = EnumSet.noneOf(Characteristics.class);
Collections.addAll(cs, characteristics);
cs = Collections.unmodifiableSet(cs);
}
return new Collectors.CollectorImpl<>(supplier, accumulator, combiner, finisher, cs);
}
示例8: makeRef
import java.util.function.BinaryOperator; //导入依赖的package包/类
/**
* Constructs a {@code TerminalOp} that implements a mutable reduce on
* reference values.
*
* @param <T> the type of the input elements
* @param <I> the type of the intermediate reduction result
* @param collector a {@code Collector} defining the reduction
* @return a {@code ReduceOp} implementing the reduction
*/
public static <T, I> TerminalOp<T, I>
makeRef(Collector<? super T, I, ?> collector) {
Supplier<I> supplier = Objects.requireNonNull(collector).supplier();
BiConsumer<I, ? super T> accumulator = collector.accumulator();
BinaryOperator<I> combiner = collector.combiner();
class ReducingSink extends Box<I>
implements AccumulatingSink<T, I, ReducingSink> {
@Override
public void begin(long size) {
state = supplier.get();
}
@Override
public void accept(T t) {
accumulator.accept(state, t);
}
@Override
public void combine(ReducingSink other) {
state = combiner.apply(state, other.state);
}
}
return new ReduceOp<T, I, ReducingSink>(StreamShape.REFERENCE) {
@Override
public ReducingSink makeSink() {
return new ReducingSink();
}
@Override
public int getOpFlags() {
return collector.characteristics().contains(Collector.Characteristics.UNORDERED)
? StreamOpFlag.NOT_ORDERED
: 0;
}
};
}
示例9: toGroup
import java.util.function.BinaryOperator; //导入依赖的package包/类
public static <T extends Unit> Collector<T, Group<T>, Group<T>> toGroup() {
return new Collector<T, Group<T>, Group<T>>(){
@Override
public Supplier<Group<T>> supplier() {
return (Supplier<Group<T>>) Group::new;
}
@Override
public BiConsumer<Group<T>, T> accumulator() {
return (group, value) -> group.add(value);
}
@Override
public BinaryOperator<Group<T>> combiner() {
return (left, right) -> { left.addAll(right); return left; };
}
@Override
public Function<Group<T>, Group<T>> finisher() {
return Function.identity();
}
@Override
public Set<Characteristics> characteristics() {
return Collections.singleton(Characteristics.IDENTITY_FINISH);
}
};
}
示例10: combiner
import java.util.function.BinaryOperator; //导入依赖的package包/类
@Override
public BinaryOperator<Map<String, JsonArray>> combiner() {
return (m1, m2) -> Stream.concat(m1.entrySet().stream(), m2.entrySet().stream())
.collect(Collectors.groupingBy(Map.Entry::getKey,
HashMap::new,
Collector.of(JsonArray::new,
(arr, e) -> arr.addAll(e.getValue()),
JsonArray::addAll)));
}
示例11: combiner
import java.util.function.BinaryOperator; //导入依赖的package包/类
@Override
public BinaryOperator<List<Integer>> combiner() {
return (a, b) -> {
a.addAll(b);
return a;
};
}
示例12: RCombinedOrDefault
import java.util.function.BinaryOperator; //导入依赖的package包/类
/**
* Ctor.
*
* @param combineOperator Combination function
* @param defaultValue a value for default result
* @param results Results to combine
*/
public RCombinedOrDefault(BinaryOperator<T> combineOperator, T defaultValue, List<Result<T>> results) {
this(
combineOperator,
new RSuccess<>(defaultValue),
results
);
}
示例13: combiner
import java.util.function.BinaryOperator; //导入依赖的package包/类
@Override
public BinaryOperator<Map<K, List<T>>> combiner() {
return (left, right) -> {
right.forEach((key, value) -> {
left.merge(key, value, (leftValue, rightValue) -> {
leftValue.addAll(rightValue);
return leftValue;
});
});
return left;
};
}
开发者ID:jinyi233,项目名称:https-github.com-RichardWarburton-java-8-Lambdas-exercises,代码行数:13,代码来源:GroupingBy.java
示例14: makeInt
import java.util.function.BinaryOperator; //导入依赖的package包/类
/**
* Constructs a {@code TerminalOp} that implements a mutable reduce on
* {@code int} values.
*
* @param <R> The type of the result
* @param supplier a factory to produce a new accumulator of the result type
* @param accumulator a function to incorporate an int into an
* accumulator
* @param combiner a function to combine an accumulator into another
* @return A {@code ReduceOp} implementing the reduction
*/
public static <R> TerminalOp<Integer, R>
makeInt(Supplier<R> supplier,
ObjIntConsumer<R> accumulator,
BinaryOperator<R> combiner) {
Objects.requireNonNull(supplier);
Objects.requireNonNull(accumulator);
Objects.requireNonNull(combiner);
class ReducingSink extends Box<R>
implements AccumulatingSink<Integer, R, ReducingSink>, Sink.OfInt {
@Override
public void begin(long size) {
state = supplier.get();
}
@Override
public void accept(int t) {
accumulator.accept(state, t);
}
@Override
public void combine(ReducingSink other) {
state = combiner.apply(state, other.state);
}
}
return new ReduceOp<Integer, R, ReducingSink>(StreamShape.INT_VALUE) {
@Override
public ReducingSink makeSink() {
return new ReducingSink();
}
};
}
示例15: testBinaryOp
import java.util.function.BinaryOperator; //导入依赖的package包/类
@Test public void testBinaryOp() {
BinaryOperator<Float> floatBiOperand = (f, g) -> {
return f * g;
};
float expectedFloat = 84F;
assertEquals(expectedFloat, floatBiOperand.apply(7F, 12F), 0.1);
}