Example usage for java.util.function BinaryOperator apply

List of usage examples for java.util.function BinaryOperator apply

Introduction

In this page you can find the example usage for java.util.function BinaryOperator apply.

Prototype

R apply(T t, U u);

Source Link

Document

Applies this function to the given arguments.

Usage

From source file:Main.java

public static void main(String[] args) {
    BinaryOperator<String> bi = (s1, s2) -> s1 + s2;

    String result = bi.apply("Just ", "Java 8");
    System.out.println(">" + result);
}

From source file:Main.java

public static void main(String[] args) {
    BinaryOperator<Integer> adder = (n1, n2) -> n1 + n2;

    System.out.println(adder.apply(3, 4));
}

From source file:Main.java

public static void main(String[] args) {
    BinaryOperator<Integer> bi = BinaryOperator.maxBy(Comparator.reverseOrder());
    System.out.println(bi.apply(2, 3));
}

From source file:Main.java

public static void main(String[] args) {
    BinaryOperator<Integer> bi = BinaryOperator.minBy(Comparator.reverseOrder());
    System.out.println(bi.apply(2, 3));
}

From source file:Main.java

private static int fibonacciOperation(Predicate<Integer> condition, BinaryOperator<Integer> operation) {

    int i = 0;/*from w w  w  . j a va2 s.  c o m*/
    int result = 0;
    int fibo = fibonacci(i);
    for (int temp = operation.apply(result, fibo); condition
            .test(temp); fibo = fibonacci(i), temp = operation.apply(result, fibo)) {
        System.out.println(fibo + " ");
        result = temp;
        i++;
    }
    return result;
}

From source file:Main.java

public static <T> Stream<T> cartesian(BinaryOperator<T> aggregator, Supplier<Stream<T>>... streams) {
    return Arrays.stream(streams)
            .reduce((s1, s2) -> () -> s1.get().flatMap(t1 -> s2.get().map(t2 -> aggregator.apply(t1, t2))))
            .orElse(Stream::empty).get();
}

From source file:Main.java

/**
 * Combines 2 maps by just adding the values of the same key together.
 *
 * @param a A map/*from w  w  w  . j  a va  2  s .c om*/
 * @param b Another map
 * @param combiner The combiner of the values
 * @param <K> The type of the keys
 * @param <V> The type of the values
 *
 * @return a map that contains all the key of map a and b and the combined values.
 */
public static <K, V> Map<K, V> combine(Map<? extends K, ? extends V> a, Map<? extends K, ? extends V> b,
        BinaryOperator<V> combiner) {
    Map<K, V> result = new HashMap<>();

    for (Entry<? extends K, ? extends V> entry : a.entrySet()) {
        if (b.containsKey(entry.getKey()))
            result.put(entry.getKey(), combiner.apply(entry.getValue(), b.get(entry.getKey())));
        else
            result.put(entry.getKey(), entry.getValue());
    }

    b.entrySet().stream().filter(e -> !result.containsKey(e.getKey()))
            .forEach(e -> result.put(e.getKey(), e.getValue()));

    return result;
}

From source file:com.centurylink.cloud.sdk.core.services.filter.Filters.java

static <T extends Filter<T>> T reduce(List<T> filters, BinaryOperator<T> operator) {
    int length = filters.size();
    T head = filters.get(0);//from w w  w  .  j ava  2  s .c om
    List<T> tail = filters.subList(1, length);

    if (length == 1) {
        return head;
    } else if (length == 2) {
        return operator.apply(head, tail.get(0));
    } else {
        return operator.apply(head, reduce(tail, operator));
    }
}

From source file:Main.java

static <T, K, U> Map<K, U> toMap(List<T> elements, Function<T, K> keyFunction, Function<T, U> valueFunction,
        BinaryOperator<U> mergeFunction) {
    //TODO Implement me
    Map<K, U> map = new HashMap<>();
    K key;//from   w  w  w  . j a v  a  2 s.c  o m
    U value;
    for (T element : elements) {
        key = keyFunction.apply(element);
        value = valueFunction.apply(element);
        if (map.containsKey(key)) {
            map.put(key, mergeFunction.apply(map.get(key), value));
        } else {
            map.put(key, value);
        }
    }
    return map;
}

From source file:it.unibo.alchemist.model.implementations.positions.LatLongPosition.java

private LatLongPosition ebeOperation(final BinaryOperator<Double> op, final Position other) {
    if (other instanceof LatLongPosition) {
        final LatLng l2 = ((LatLongPosition) other).latlng;
        return new LatLongPosition(op.apply(latlng.getLatitude(), l2.getLatitude()),
                op.apply(latlng.getLongitude(), l2.getLongitude()));
    }//  www.  ja  va2  s.co m
    throw new IllegalArgumentException(
            "You are trying to do math combining " + this + "with " + other + ". This is not supported.");
}