英文:
Convert List<Map<String, Object>> to String[][]
问题
我有一个 List<Map<String, Object>> data
。
public List<Map<String, Object>> selectAll(String tableName) throws SQLException{
MapListHandler mapListHandler = new MapListHandler();
QueryRunner queryRunner = new QueryRunner();
List<Map<String, Object>> list = queryRunner.query(DBConnection.getConnection(), SELECT + tableName, mapListHandler);
System.out.println(list);
return list;
}
然后我获得了这个
List<Map<String, Object>> data = databaseMetadata.selectAll(buttonElements.getTables_in_migrate_schema());
我需要将它添加到 JTable 作为 rowData,所以它必须是 String[][]
。
我尝试过类似这样的做法。
JTable jTable = new JTable((Object[][]) data.toArray(), fields.toArray());
有人可以告诉我如何进行转换吗?提前感谢!
英文:
I have a List<Map<String, Object>> data
.
public List<Map<String, Object>> selectAll(String tableName) throws SQLException{
MapListHandler mapListHandler = new MapListHandler();
QueryRunner queryRunner = new QueryRunner();
List<Map<String, Object>> list = queryRunner.query(DBConnection.getConnection(), SELECT + tableName, mapListHandler);
System.out.println(list);
return list;
}
Then I get this
List<Map<String, Object>> data = databaseMetadata.selectAll(buttonElements.getTables_in_migrate_schema());
And I need to add it to JTable as rowData so it has to be String[][]
.
I tried to make something like this.
JTable jTable = new JTable((Object[][]) data.toArray(), fields.toArray());
Can someone please say me how to convert? Thanks in advance!
答案1
得分: 1
因为你已经拥有了字段的名称,所以只需收集每个映射的列表值,并收集为二维对象数组。你可以使用Stream API来实现:
Object[][] objList = data.stream().map(m -> m.values().toArray()).toArray(Object[][]::new);
英文:
As you already have the field's name, so just collect every map's value of list and collect as 2D Object array. You can use Stream API this way
Object[][] objList = data.stream().map(m -> m.values().toArray()).toArray(Object[][]::new);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论