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

标签 java dictionary

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

相关文章:

java - 抽屉导航菜单子(monad)类别

python - 使用字符串、列表和字典进行操作

java - Spring Boot (Netflix Eureka) - 访问微服务的自定义 URI

java - 构建 Jersey Web 服务可执行文件

java - 是否有任何工具可以检测类成员变量中可能出现的 NullPointerException

dictionary - 在字典中将 c++/cli 类指定为 TValue 类型的正确语法是什么

python - 如何根据用户输入从最高到下一个最低的顺序绘制字典中的值

java - 没有明确答案 : Which Java Map is the cheapest?

python - 在Python中的排序字典中查找并计算行中的相同值?

java - Apache 公共(public)邮件 : how get new line in simple email?