[英]Java 8: Group Object array to Map to return as JSON
I have an Object 2D array ( Object [][]
) that is returned from querying a database. 我有一个查询数据库所返回的Object 2D数组( Object [][]
)。 I now want to map it to Objects which can be returned in API call after grouping. 我现在想将其映射到可以在分组后通过API调用返回的对象。
Here is my 2D Object array. 这是我的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]
I want to return 我想回来
List<IdName> idsNames;
Where each of the classes should be mapped like this. 每个类应在何处映射,就像这样。
class IdName {
String id;
String name;
List<Another> anotherNameList;
}
class Another {
String anotherId;
String anotherName;
List<Format> formatList;
}
class Format {
String format;
Boolean isEnabled;
Boolean isManaged;
}
I tried using Java 8's groupingBy
but I couldn't get to the state that I want. 我尝试使用Java 8的groupingBy
但无法进入所需状态。
Sample expected result: 样本预期结果:
[
{
"id": 1,
"name": "a",
"another": [
{
"anotherId": 9,
"anotherName": "Content1",
"format": [
{
"format": "format1",
"isEnabled": true,
"isManaged": false
},
{
"format": "format2",
"isEnabled": true,
"isManaged": false
}
]
}
]
}
]
Looks like you should use Collectors.collectingAndThen
. 看起来您应该使用Collectors.collectingAndThen
。
First create the extractors (Assuming your classes have constructors & getters): 首先创建提取器(假设您的类具有构造函数和获取方法):
// 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]);
}
Then you will need the merge functions: 然后,您将需要合并功能:
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());
}
Finally this will do the trick: 最后,这将达到目的:
// 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));
It can be done in several steps. 它可以分几个步骤完成。 Let all values be String
for simplicity. 为了简单起见,让所有值都为String
。 Also you are supposed to have constructors and equals
/ hashcode
methods implemented. 另外,您应该已经实现了构造函数和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))));
Then we can create Format
objects and put everything together. 然后,我们可以创建Format
对象并将所有内容放在一起。
for (Map.Entry<IdName, Map<Another, List<String[]>>> entry : map.entrySet()) {
IdName idName = entry.getKey(); // main object
Set<Another> anothers = entry.getValue().keySet();
for (Another another : anothers) { // create list<Format> for each Another
List<Format> formatList = entry.getValue().get(another).stream()
.map(format -> new Format(format[4], format[5], format[6]))
.collect(Collectors.toList());
another.setFormatList(formatList);
}
idName.setAnotherNameList(anothers);
}
Now we can get all assembled objects 现在我们可以得到所有组装好的物体
Set<IdName> idNames = map.keySet();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.