Reputation: 2777
Actually I retrieve data from database which is surely in the form of table. I want to manage data (table) in hash-map in specific format. Firstly I show you the following code and it's output in a console:
Code:
ResultSetMetaData rsmd = resultSet.getMetaData();
int columnsNumber = rsmd.getColumnCount();
while (resultSet.next()) {
for (int i = 1; i <= columnsNumber; i++) {
if (i > 1) System.out.print(", ");
String columnValue = resultSet.getString(i);
System.out.print(columnValue );
}
System.out.println("");
}
Output:
ID SkillGroup
47010, EN.SG
47013, shabber_hassan
47014, CAM.SG
47014, CIM.SG
47014, EN.SG
47014, JZskillGroup
47014, camp_agent.SG
999, EN.SG
3333, absaara_test2.SG
3333, absaara_test3.SG
I want to make a Hasp-Map which should be filled with following way:
Key: Should be the ID.
Value: All the skillgroup (maybe in Arraylist) related to one ID
How I Can Do This:
ID ---> [All skill group related to associated ID]
47010 ---> [EN.SG]
47013 ---> [shabber_hassan,]
47014 ---> [CAM.SG,CIM.SG,EN.SG,JZskillGroup,camp_agent.SG]
999 ---> [EN.SG]
333 ---> [ab_test2.SG,ab_test3.SG]
Some help would be apppreciated !!
Upvotes: 2
Views: 182
Reputation: 393791
Use a HashMap<Integer,List<String>>
.
Assuming your table has two columns (ID and Skill Group), based on your output :
Map<Integer,List<String>> map = new HashMap<>();
while (resultSet.next()) {
String key = resultSet.getInt(1);
String value = resultSet.getString(2);
List<String> values = map.get(key);
if (values == null) {
values = new ArrayList<String>();
map.put(key,values);
}
values.add(value);
}
Upvotes: 3