Reputation: 21
I have 3 columns from following query
select type, typeid, count(typeid)
from schema1.table_name
where typeid is not null
group by typeid;
I want to put output from this query into Map<String, Map<Integer, Integer>>
Data are:
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
Upvotes: 0
Views: 138
Reputation: 86296
I’m not sure it’s the best data structure for what you’re trying to obtain. Personally I’d consider a Type class with fields type and typeId and map from this class to the count. Anyway, to answer your question, for each row from the database do:
Map<Integer, Integer> innerMap = typeIdCounts.get(type);
if (innerMap == null) {
innerMap = new HashMap<>();
typeIdCounts.put(product, innerMap);
}
innerMap.put(typeId, count);
Upvotes: 1
Reputation: 1819
You only need to use a Map of Maps
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);
Upvotes: 0