集册 Java实例教程 双函数合并数组

双函数合并数组

欢马劈雪     最近更新时间:2020-01-02 10:19:05

473
双函数合并数组


//package com.nowjava;

/*
来 自*
 N o w  J a v a  .   c o m
*/

import java.util.function.BiFunction;


import java.util.stream.DoubleStream;

import java.util.stream.Stream;


public class Main {

    public static <In1, In2, Out> Stream<Out> mergeByBiFunction(

            In1[] array1, In2[] array2, BiFunction<In1, In2, Out> function) {

        int length = Math.min(array1.length, array2.length);

        Stream.Builder<Out> streamBuilder = Stream.builder();

        for (int i = 0; i < length; i++) {

            streamBuilder.accept(function.apply(array1[i], array2[i]));

        }

        return streamBuilder.build();

    }


    public static DoubleStream mergeByBiFunction(double[] array1,

            double[] array2, BiFunction<Double, Double, Double> function) {

        int length = Math.min(array1.length, array2.length);/**来自 N o w  J a v a  . c o m**/

        DoubleStream.Builder streamBuilder = 
展开阅读全文