I have an sql table Children :
x
| ParentName | ChildrenName |
———————————————————————————
| parent 1 | child 1 |
| parent 1 | child 2 |
| parent 2 | child 3 |
| parent 1 | child 4 |
| | . |
My goal is to construct a java HashMap<String , ArrayList<String>>
that contains ParentName as key and a list of ChildrenName
as value.
Should I perform the processing by using an sql query? Or select all data and process it via java?
Advertisement
Answer
Concerning your second option:
Get all data from the database with a simple query and map the data in Java
You can do it like this (read the code comments):
public static void main(String[] args) {
// provide a query as String
String query = "SELECT ParentName, ChildName FROM some_table";
// provide the data structure
Map<String, List<String>> results = new HashMap<>();
// connect to your database
Connection connection; // this has to be created using a suitable JDBC implementation
try (PreparedStatement ps = connection.prepareStatement(query)) {
ResultSet rs = ps.executeQuery();
while (rs.next()) {
String parentName = rs.getString(1);
String childName = rs.getString(2);
// add them to the map
if (results.containsKey(parentName)) {
/*
* if the key already exists in the map,
* just add the current name to its value(s)
*/
results.get(parentName).add(childName);
} else {
// create a new list as the value for the new key
List<String> childNames = new ArrayList<>();
// add the current child name to it
childNames.add(childName);
// and put the new key and value in the map
results.put(parentName, childNames);
}
}
} catch (SQLException e) {
e.printStackTrace();
}
// print the results
results.forEach((p, c) -> System.out.println(p + ": " + String.join(", ", c)));
}