private Map<String, List<Object>> resultSetToArrayList(ResultSet rs) throws SQLException {
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
Map<String, List<Object>> map = new HashMap<>(columns);
for (int i = 1; i <= columns; ++i) {
map.put(md.getColumnName(i), new ArrayList<>());
}
while (rs.next()) {
for (int i = 1; i <= columns; ++i) {
map.get(md.getColumnName(i)).add(rs.getObject(i));
}
}
return map; }
I am using the above code to store results of my resultSets into a Collection. There are two SQL Queries generating two different resultSets and different amount of rows. I would like to write a code which would eliminate rows which equal, and would print rows paired based on the least amount of differences in them. The columns which differ should be braced in square brackets.
Could anyone help with this one? Any help would be greatly appreciated.
For your requirement there is no need of two Map. You can create a class to represent your table and create a list of that class, something like this:
List<SomeClass> list = new ArrayList<SomeClass>();
SomeClass s = null;
while (rs.next()) {
s = new SomeClass();
s.name = rs.getString(1);
.......
list.add(s);
}
Then you can add comparator to this class and decide which columns you want to be sorted.