Java8:映射为JSON返回的组对象数组

Java8:映射为JSON返回的组对象数组,java,java-8,mapping,java-stream,Java,Java 8,Mapping,Java Stream,我有一个查询数据库返回的对象2D数组(Object[][])。现在,我想将它映射到分组后可以在API调用中返回的对象 这是我的2D对象数组 Object [][] dbResult = { {1, "a", 9, "Content1", "format1", false, true}, {1, "a", 9, "Content1", "format2", true, false}, {2, "b", 8, "Content2", "format3", true, false},

我有一个查询数据库返回的对象2D数组(
Object[][]
)。现在,我想将它映射到分组后可以在API调用中返回的对象

这是我的2D对象数组

Object [][] dbResult = 
{
  {1, "a", 9, "Content1", "format1", false, true},
  {1, "a", 9, "Content1", "format2", true, false},
  {2, "b", 8, "Content2", "format3", true, false},
  {2, "b", 8, "Content2", "format4", false, false},
  {3, "c", 7, "Content3", "format5", true, true},
  {4, "d", 8, "Content2", "format6", false, true},
  {4, "d", 6, "Content3", "format7", true, true},
  {4, "d", 5, "Content4", "format8", false, false},
  {5, "e", 4, "Content5", "format9", false, true},
  {6, "f", 3, "Content6", "format10", true, false}
};

Here is the legend/key for the index.
[ID, Name, AnotherID, AnotherName, Format, Boolean, Boolean]
我想回去

List<IdName> idsNames;

这可以通过几个步骤来完成。为了简单起见,将所有值设为
String
。 此外,还应该实现构造函数和
equals
/
hashcode
方法

Map<IdName, Map<Another, List<String[]>>> map = Arrays.stream(dbResult)
    .collect(
        groupingBy(s -> new IdName(s[0], s[1], null),
            groupingBy(s -> new Another(s[2], s[3], null))));
现在我们可以得到所有组装的对象

Set<IdName> idNames = map.keySet();
Set idNames=map.keySet();

看起来您应该使用
收集器。先收集,然后使用

首先创建提取器(假设类具有构造函数和getter):

然后您将需要合并功能:

List<Another> setFormats(Map<Another, List<Format>> map) {
    return map.entrySet()
              .stream()
              .map(e -> {
                  e.getKey().setFormatList(e.getValue());
                  return e.getKey();
              })
              .collect(toList());
}

List<IdName> setAnothers(Map<IdName, List<Another>> map) {
    return map.entrySet()
              .stream()
              .map(entry -> {
                  entry.getKey().setAnotherNameList(entry.getValue());
                  return entry.getKey();
              })
              .collect(toList());
}
列表集合格式(映射){
return map.entrySet()
.stream()
.map(e->{
e、 getKey().setFormatList(例如getValue());
返回e.getKey();
})
.collect(toList());
}
列出其他集合(地图){
return map.entrySet()
.stream()
.map(条目->{
entry.getKey().setAnotherNameList(entry.getValue());
return entry.getKey();
})
.collect(toList());
}
最后,这将实现以下目的:

// Converting Object[][] to List<IdName>
List<IdName> list = 
      Arrays.stream(dbResult)
            .collect(
                collectingAndThen(
                    groupingBy(this::extractIdName,
                        collectingAndThen(
                            groupingBy(this::extractAnother,
                                mapping(this::extractFormat, toList())),
                            this::setFormats
                        )),                                                             
                    this::setAnothers));
//将对象[]转换为列表
列表=
Arrays.stream(dbResult)
.收集(
收集然后(
groupingBy(this::extractIdName,
收集然后(
分组方式(此::提取另一个,
映射(this::extractFormat,toList()),
此::setFormats
)),                                                             
这个(其他的);

您的字符串[][]dbResult无效array@LhoBen我已接受您的编辑并添加了更多信息。dbResult中的值不是
字符串
。不兼容的类型..好的。我把它改成了Object。我的主要问题是如何转换。我已将公司拥有的代码/实体更改为stackoverflow的示例。这正是我想要的。谢谢你,谢谢你的回答。我无法编译创建
map
Set<IdName> idNames = map.keySet();
// The cast types are just an example. You can Cast/convert the array values to any type you want

IdName extractIdName(Object[] row) {
    return new IdName((String) row[0], (String) row[1], null);
}

Another extractAnother(Object[] row) {
    return new Another((String) row[2], (String) row[3], null);
}

Format extractFormat(Object[] row) {
    return new Format((String) row[4], (boolean) row[5], (boolean) row[6]);
}
List<Another> setFormats(Map<Another, List<Format>> map) {
    return map.entrySet()
              .stream()
              .map(e -> {
                  e.getKey().setFormatList(e.getValue());
                  return e.getKey();
              })
              .collect(toList());
}

List<IdName> setAnothers(Map<IdName, List<Another>> map) {
    return map.entrySet()
              .stream()
              .map(entry -> {
                  entry.getKey().setAnotherNameList(entry.getValue());
                  return entry.getKey();
              })
              .collect(toList());
}
// Converting Object[][] to List<IdName>
List<IdName> list = 
      Arrays.stream(dbResult)
            .collect(
                collectingAndThen(
                    groupingBy(this::extractIdName,
                        collectingAndThen(
                            groupingBy(this::extractAnother,
                                mapping(this::extractFormat, toList())),
                            this::setFormats
                        )),                                                             
                    this::setAnothers));