英文:
How to override duplicate keys in Java Map
问题
我有这个作为我的查询。
select a.cust_id,a.cust_name,b.cust_id,b.cust_name
from acustomer a,bcustomer b
DaoImp 使用 Spring NamedJdbcparameterTemplate
方法:
NamedJdbcparameterTemplate temp= new NJPT(datasource);
List<Map<String,Object>> out=temp.quertForList(query,parametermap);
但问题是,每当我在数据库工具中获得这个查询的输出时,我得到4列,但在程序输出中,我只得到2列,即由于Map中具有相同键名,a
的 cust_id
和 cust_name
被b
覆盖。
我应该如何修复这个问题,请注意查询每次都会不同,因为我将这个方法用作我的程序的通用方法,输出将是值的列表,因此无法为输出映射任何模型类。
请注意,我希望这个函数是通用的,这意味着查询每次都会改变,输出将是不同类型的。
英文:
I have this as my query.
select a.cust_id,a.cust_name,b.cust_id,b.cust_name
from acustomer a,bcustomer b
DaoImp using Spring NamedJdbcparameterTemplate
method:
NamedJdbcparameterTemplate temp= new NJPT(datasource);
List<Map<String,Object>> out=temp.quertForList(query,parametermap);
But the problem is that whenever I get the output for this query in db tool, I get 4 columns but in program output I am only getting 2 columns, i.e cust_id
and cust_name
of a is getting overridden by b
due to same key name in Map.
How can I fix this, please note the query will be different each time as I am using this method as a general one for my program and output will be a list of values, so cannot map any model class for the output.
Please note I want this function to be generic one which means the query will be changing each time and output will be of different types.
答案1
得分: 5
"Well the easy solution would be to give your fields aliases, so that their keys would be different."
select a.cust_id a_cust_id, a.cust_name a_cust_name, b.cust_id b_cust_id, b.cust_name b_cust_name
from acustomer a, bcustomer b
where a.cust_id=b.cust_id
Then in the map you would find the aliases, a_cust_id
, a_cust_name
, b_cust_id
, b_cust_name
.
英文:
Well the easy solution would be to give your fields aliases, so that their keys would be different.
select a.cust_id a_cust_id, a.cust_name a_cust_name, b.cust_id b_cust_id, b.cust_name b_cust_name
from acustomer a, bcustomer b
where a.cust_id=b.cust_id
Then in the map you would find the aliases, a_cust_id
, a_cust_name
, b_cust_id
, b_cust_name
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论