【问题标题】:Addition in Java 8 using BinaryOperatorJava 8 中使用 BinaryOperator 的加法
【发布时间】:2018-01-01 08:14:50
【问题描述】:
  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 解决。此应用程序有三个输入 [mealCost(double)、tipPercent(int)、taxPercent(int)]。
  2. 我正在尝试计算以下值:

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

【问题讨论】:

  • 您可能还想使用DoubleBinaryOperator,它使用原始值而不是Double 包装器

标签: java addition binary-operators


【解决方案1】:

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

或将mealCost 保留在一个变量中,将其他值保留在List&lt;Double&gt;

    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)));
    }

【讨论】:

    【解决方案2】:

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

    试试下面的代码

    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)));
        }
    }
    

    【讨论】:

      【解决方案3】:

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

      我无法将整数输入传递给 二元运算符

      这是因为你已经声明你的BinaryOperatorDouble 作为输入参数类型和返回类型。 BinaryOperator 只接受一个 Type 作为参数,它既是输入参数的类型又是返回类型,所以如果你有 Double 作为方法参数和 Double 作为返回类型,你可以决定使用BinaryOperator。如果有多种类型作为参数和返回类型,可以考虑使用BiFunction

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

      这里我们说输入参数是Double和Inetger,对应mealCost和taxPercent,返回类型是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);
          }
      

      【讨论】:

        猜你喜欢
        • 2014-07-21
        • 1970-01-01
        • 2016-01-08
        • 2020-08-06
        • 1970-01-01
        • 1970-01-01
        • 2017-07-17
        • 1970-01-01
        • 2015-07-19
        相关资源
        最近更新 更多