I want to pass list of key-values pairs into my function.
I can do this by following:
HashMap<String, String> params = new HashMap<>();
params.put("param1", value1);
params.put("param2", value2);
foo(params);
But there are 4 lines of code. And my foo
function will be called in many times in my code, so it becomes hard to read.
Can we decrease number of lines in this call?
- Is there a way to write
foo([("param1", value1), ("param2", value2)])
? - Maybe Java standard classes have constructors to pass there several elements?
- Or can we write some factory class, that will create HashMap in less lines of code?