javascript - Return anonymous object in java -
in javascript can do:
function foo() { ... return { attr1 : ... , attr2 : ..., }; }
but equivalent in java?
because want return custom json object controller after ajax call , want create new bean.
as java dictates, should create new class , convert json. also, can use map<string,object>
accomplish same thing. generate following json:
{ "attr1":1, "attr2":2 }
you can use following code:
map<string,object> map = new hashmap<>(3); map.put("attr1", 1); map.put("attr2", 2);
and convert json.
p.s.: hashmap
in java causes heap increase , unnecessary garbage, specified enough size keep 2 elements.