java - Replace String values with value in Hash Map -
i'm new java programming. have created hash map contains key value pairs utilize in replacing user input value corresponding respective key.
i.e.
hashmap<string,string> questionkey = new hashmap<>(); (item : itemset()) { questionkey.put(string.valueof(item.getorder()+1), item.getkey()); } string list = commandobject.getobjectitem().getlist(); if (list.contains("q")){ list.replaceall("q01", questionkey.get("1")); commandobject.getobjectitem().setlist(list); }
i using in formula evaluation
note: users given formula specific way of entry (value1 + value2 + value3)
i'm taking (value1 value2 value3) , converting (value1key value2key value3key)
update:
the question understand better meant to better understand how utilize hashmap in order evaluate user input. more clear question
what best approach evaluate expression i.e.
user input = "var1 + var2"
expected value: valueof(var1) + valueof(var2)
?
@test public void testsomething() { string str = "hello ${mykey1}, welcome stack overflow. have nice ${mykey2}"; map<string, string> map = new hashmap<string, string>(); map.put("mykey1", "dd84"); map.put("mykey2", "day"); (map.entry<string, string> entry : map.entryset()) { str = str.replace("${" + entry.getkey() + "}", entry.getvalue()); } system.out.println(str); }
output:
hello dd84, welcome stack overflow. have nice day
for more complex i'd rather use ognl.
Comments
Post a Comment