简体   繁体   中英

Java put Mysql query result into multimap

For example I have the following MySql table:

A    B     C

1    aaa   2017

1    bbb   2018

2    ccc   2016

2    ddd   2015

I want to put all rows into a multimap structure, using column A as key (the key is not unique, so why multimap). I searched and it seems org.apache.commons.dbutils ResultSetHandler only has BeanMapHandler (but not something like BeanMultiMapHandler).

Is there anyway to put rows into a multimap using column A as key? Thanks.

Google Guava can be a possible soulution. Following code can serve as a guideline:

Multimap<Integer, Map<String, Integer> myMultimap = ArrayListMultimap.create();
// create and inflate nested maps here. Code is not shown 
myMultimap.put(1, nestedMap1);
myMultimap.put(1, nestedMap2);
myMultimap.put(2, nestedMap3);
myMultimap.put(2, nestedMap4);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM