Sure, here is the translation: 将 List<Map<String, Object>> 转换为 String[][]

huangapple go评论87阅读模式
英文:

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&lt;Map&lt;String, Object&gt;&gt; data.

public List&lt;Map&lt;String, Object&gt;&gt; selectAll(String tableName) throws SQLException{
    MapListHandler mapListHandler = new MapListHandler();
    QueryRunner queryRunner = new QueryRunner();
    List&lt;Map&lt;String, Object&gt;&gt; list = queryRunner.query(DBConnection.getConnection(), SELECT + tableName, mapListHandler);
    System.out.println(list);
    return list;
}

Then I get this

List&lt;Map&lt;String, Object&gt;&gt; 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 -&gt; m.values().toArray()).toArray(Object[][]::new);

huangapple
  • 本文由 发表于 2020年9月14日 20:47:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/63884647.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定