I have List of class Data
as dataList
, Data
contains two variable a
and b
.
public class Data {
int a,b;
public Data(int a, int b){
this.a=a;
this.b=b;
}
//also contains getter and setter method
}
List<Data> dataList=new ArrayList<>();
dataList.add(new Data(2,3));
dataList.add(new Data(6,2));
Also, I have mathematical expression in the form of string for eg. 3*a+5*b;
I want to apply above expression to data list, to get output as 21 and 28.
dataList.forEach(v1 ->System.out.println(//some code to evaluate the expression));
Thanks in advance
You can use
ScriptEngineManager
. WithscriptEngine.eval(expression)
theexpression
script will be executed.Then you can do:
With
Data
class: