簡體   English   中英

使用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解決。此應用程序有三個輸入[mealCost(double),tipPercent(int),taxPercent(int)]。
  2. 我正在嘗試計算以下值:

     tip = (mealCost*tipPercent)/100; tax = (mealCost*taxPercent)/100; TotalCost = mealCost+tip +tax; 
  3. 我無法將整數輸入傳遞給BinaryOperator的apply方法。 此外,biList中的計算值也不合適。 以下是我的代碼

其他答案已經告訴你計算錯誤的根本原因。 關於你問題的第二部分:

我無法將整數輸入傳遞給BinaryOperator的apply方法

這是因為您已聲明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);
    }

您將相同的鍵放在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)));
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM