gpt4 book ai didi

java - 将数据库中的数据放入 map 内的 map 中

转载 作者:行者123 更新时间:2023-12-01 10:12:33 25 4
gpt4 key购买 nike

我有以下查询的 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

最佳答案

我不确定这是否是您想要获取的最佳数据结构。就我个人而言,我会考虑一个带有字段 type 和 typeId 的 Type 类,并从该类映射到计数。无论如何,要回答您的问题,对于数据库中的每一行,请执行以下操作:

Map<Integer, Integer> innerMap = typeIdCounts.get(type);
if (innerMap == null) {
innerMap = new HashMap<>();
typeIdCounts.put(product, innerMap);
}
innerMap.put(typeId, count);

关于java - 将数据库中的数据放入 map 内的 map 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36059585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com