如何在Java Map中覆盖重复的键

huangapple go评论16阅读模式
英文:

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中具有相同键名,acust_idcust_nameb覆盖。

我应该如何修复这个问题,请注意查询每次都会不同,因为我将这个方法用作我的程序的通用方法,输出将是值的列表,因此无法为输出映射任何模型类。

请注意,我希望这个函数是通用的,这意味着查询每次都会改变,输出将是不同类型的。

英文:

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.

huangapple
  • 本文由 发表于 2020年8月2日 14:09:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63212934.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定