使用 BinaryOperator 在 Java 8 中添加

Addition in Java 8 using BinaryOperator

  package com.operators;

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.Scanner;
    import java.util.function.BinaryOperator;

    public class TotalCost {

        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            double mealCost = scan.nextDouble(); // original meal price
            int tipPercent = scan.nextInt(); // tip percentage
            int taxPercent = scan.nextInt(); // tax percentage
            scan.close();

            Map<Double,Double> map = new HashMap<>();
            map.put(mealCost, (double)tipPercent);
            map.put(mealCost, (double)taxPercent);

            BinaryOperator<Double> opPercent = (t1,t2) -> (t1*t2)/100;
            BinaryOperator<Double> opSum = (t1,t2) -> (t1+t2);   
            calculation(opPercent,map);
        }

        public static void calculation(BinaryOperator<Double> opPercent , Map<Double,Double> map) {
            List<Double> biList = new ArrayList<>();
            map.forEach((s1,s2)-> biList.add(opPercent.apply(s1, s2)));
        }
    }
  1. 我有以下问题,我试图在 Java 8 中解决,使用 BinaryOperator.There 是此应用程序的三个输入 [mealCost(double)、tipPercent(int)、taxPercent(int) ].
  2. 我正在尝试计算以下值:

    tip = (mealCost*tipPercent)/100;
    tax = (mealCost*taxPercent)/100;
    TotalCost = mealCost+tip +tax;   
    
  3. 我无法将整数输入传递给 BinaryOperator 的应用方法。 biList 中的计算值也不正确。 下面是我的代码

您在 Map 中放置了相同的键两次,因此第二个值覆盖了第一个值。我认为 Map 不适合这些计算。您可以改用 List<SomePairType>

或者将 mealCost 保存在一个变量中,将其他值保存在 List<Double> 中:

    public static void calculation(BinaryOperator<Double> opPercent, Double cost, List<Double> rates) {
        List<Double> biList = new ArrayList<>();
        rates.forEach(d-> biList.add(opPercent.apply(cost, d)));
    }

您正在将同一键的值放入映射中。所以初始值被新值覆盖了。

试试下面的代码

package com.operators;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.function.BinaryOperator;

public class TotalCost {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        double mealCost = scan.nextDouble(); // original meal price
        int tipPercent = scan.nextInt(); // tip percentage
        int taxPercent = scan.nextInt(); // tax percentage
        scan.close();

        Map<Double,Double> map = new HashMap<>();

        map.put(mealCost, (double)taxPercent + (double)tipPercent);

        BinaryOperator<Double> opPercent = (t1,t2) -> (t1*t2)/100;
        BinaryOperator<Double> opSum = (t1,t2) -> (t1+t2);   
        calculation(opPercent,map);
    }

    public static void calculation(BinaryOperator<Double> opPercent , Map<Double,Double> map) {
        List<Double> biList = new ArrayList<>();
        map.forEach((s1,s2)-> biList.add(opPercent.apply(s1, s2)));
    }
}

其他答案已经告诉你计算错误的根本原因。关于你问题的第二部分:

I am unable to pass an integer input to the apply method of BinaryOperator

这是因为你声明了你的BinaryOperator的入参类型为Double,类型为return。 BinaryOperator takes only one Type as parameter and which is the type of both the input parameters as well as the return type, so if you have Double as method parameters and Double as the return type also, you can decide to use BinaryOperator. If you have multiple types as parameters and return type, you can consider using BiFunction.

BiFunction<Double, Integer, Double> opPercent = (t1,t2) -> (t1*t2)/100;

这里说输入参数是Double和Inetger,对应mealCost和taxPercent,return类型是Double。

您甚至可以使用更多参数定义自己的功能接口,如下例所示:

public class TotalCost {

   public static void main(String[] args) {
      Scanner scan = new Scanner(System.in);
      double mealCost = scan.nextDouble(); // original meal price
      int tipPercent = scan.nextInt(); // tip percentage
      int taxPercent = scan.nextInt(); // tax percentage
      scan.close();

      TriFunction<Double, Integer, Integer, Double> calcCost = (cost, tipPct, taxPcnt) -> 
                                        (cost + (cost * tipPct/100) + (cost * taxPcnt/100));
      Double totalBill = calculation(calcCost, mealCost, tipPercent, taxPercent);
      System.out.println(totalBill);
   }

    public static Double calculation(TriFunction<Double, Integer, Integer, Double> calcCost , 
                                       Double mealCost, Integer tipPct, Integer taxPct) {
       return calcCost.apply(mealCost, tipPct, taxPct);
        }
  }

    @FunctionalInterface
    interface TriFunction<T,U,V,R> {
        R apply(T t, U u, V v);
    }