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))); } }
- i have below problem trying solve in java 8 using binaryoperator.there 3 inputs application [mealcost(double), tippercent(int),taxpercent(int)].
i trying calculate below values :
tip = (mealcost*tippercent)/100; tax = (mealcost*taxpercent)/100; totalcost = mealcost+tip +tax;
i unable pass integer input apply method of binaryoperator. calculated value bilist not proper. below code
you putting same key twice in map, second value overrides first value. don't think map
appropriate these calculations. can use list<somepairtype>
instead.
or keep mealcost
in 1 variable , other values in 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))); }
Comments
Post a Comment