我有以下查询的3列
select type, typeid, count(typeid)
from schema1.table_name
where typeid is not null
group by typeid;
我想将此查询的输出放入Map<String, Map<Integer, Integer>>
数据是:
type type_id count(type_id)
Product Class 7 1
Product Class 109 2
Product Class 123 1
Product Class 132 1
SubCategory 362 3
Category 364 2
SubCategory 430 1
SubCategory 434 7
SubCategory 532 1
SubCategory 683 1
Brand 10002 10
Brand 10003 2
Brand 10393 3
Brand 12068 1
答案 0 :(得分:1)
我不确定这是您尝试获取的最佳数据结构。就个人而言,我会考虑一个带有字段类型和类型ID的Type类,并将此类映射到count。无论如何,要回答你的问题,对于数据库中的每一行都做:
Map<Integer, Integer> innerMap = typeIdCounts.get(type);
if (innerMap == null) {
innerMap = new HashMap<>();
typeIdCounts.put(product, innerMap);
}
innerMap.put(typeId, count);
答案 1 :(得分:0)
您只需使用地图地图
String type;
int type_id, count;
Map<String, Map<Integer, Integer>> data = new HashMap<>();
Map<Integer, Integer> innerMap = data.get(type);
if(innerMap == null)
{
innerMap = new HashMap<Integer, Integer>();
data.put(type, innerMap);
}
innerMap.put(type_id, count);