如何提取列表中的列表?
How to Extract list within a list?
我有两个 JSON 个回复,我必须将其合并为一个回复。
第一个 JSON 响应:(DTO2)
"data1": [{
"id": "ABC",
"variables": [{
"id": "XYZ",
"name":"name",
"description":"description"}]
}]
class A{
List <Data1> data1;
class Data1 data1 {
....
List<Variables> variables;
}}
第二个 JSON 响应:(DTO1)
"data2": [{
"id": "XYZ",
"parameters": [{
"timeStamp":"12345678",
"value": "value",
"category":"category" }]
}]
Class B{
List <Data2> data2;
public Data2 data2 {
.....
List<Parameters> parameters;
}
}
最终回复应该是这样的:
[{
"id":"XYZ",
"name":"name",
"description":"description",
"parameter":{
"value": "value",
"category":"category",
"timeStamp":"timeStamp"
}
}]
我为每个响应创建了 DTO。
我必须检查 id 并合并最后一个的响应。
我对每个都使用了嵌套并将其组合起来。
// received response is mapped to classA
classAResponse.getData1.forEach{ a ->
a.getVariables.forEach{ v ->
classBResponse.getData2.forEach { b ->
b.getParameters.forEach{ p ->
if(a.getId.equals(b.getId)) then
new Response(....)
});
});.....
有没有更好的方法?
假设:
- 两个 dtos 示例都表示具有多个对象的数组。
- 您必须仅从出现在两个数组中的参数和变量中提取
Response
的列表,否则将其排除。
然后我想我会使用 HashMap 因为他的操作具有 O(1) 复杂性,这意味着获取和放置值所需的时间不取决于集合的大小,它是延迟可以估计为一个常量值(请看一下散列的概念以及它在这个 Map 实现中的使用方式)。
所以我认为更好的解决方案应该是这样的:
Set<Response> responses = new HashSet<>(); //Or whatever collection you want.
HashMap<String, Variables> mapWithAValues = new HashMap<>(); //Key of type string, Value of type Variables.
// Put in the map every nested variable in the list of Data1
classAResponse.getData1()
.stream()
.flatMap(d -> d.getVariables().stream())
.forEach(v -> mapWithAValues.put(v.getId(), v));
classBResponse.getData2()
.stream()
.forEach(data2 -> {
String id = data2.getId();
// Search for a variable corresponding to this parameter.
Variables aValue = mapWithAValues.get(id);
if(aValue != null) {
List<Parameter> parameters = data2.getParameters();
// Merge data in a Response
Response newResponse = new Response(...);
...
//Add response to the list of responses.
responses.add(newResponse);
}
});
return responses;
此解决方案的复杂度为 O(N),其中 N 是变量数量与可用参数数量的总和。在您的示例中,我认为复杂度为 O(d2^V),因为您需要迭代 V(可用变量数)乘以 d2(可用数据 2 值数)项的列表。
希望这对您有所帮助 :)
我有两个 JSON 个回复,我必须将其合并为一个回复。
第一个 JSON 响应:(DTO2)
"data1": [{
"id": "ABC",
"variables": [{
"id": "XYZ",
"name":"name",
"description":"description"}]
}]
class A{
List <Data1> data1;
class Data1 data1 {
....
List<Variables> variables;
}}
第二个 JSON 响应:(DTO1)
"data2": [{
"id": "XYZ",
"parameters": [{
"timeStamp":"12345678",
"value": "value",
"category":"category" }]
}]
Class B{
List <Data2> data2;
public Data2 data2 {
.....
List<Parameters> parameters;
}
}
最终回复应该是这样的:
[{
"id":"XYZ",
"name":"name",
"description":"description",
"parameter":{
"value": "value",
"category":"category",
"timeStamp":"timeStamp"
}
}]
我为每个响应创建了 DTO。 我必须检查 id 并合并最后一个的响应。
我对每个都使用了嵌套并将其组合起来。
// received response is mapped to classA
classAResponse.getData1.forEach{ a ->
a.getVariables.forEach{ v ->
classBResponse.getData2.forEach { b ->
b.getParameters.forEach{ p ->
if(a.getId.equals(b.getId)) then
new Response(....)
});
});.....
有没有更好的方法?
假设:
- 两个 dtos 示例都表示具有多个对象的数组。
- 您必须仅从出现在两个数组中的参数和变量中提取
Response
的列表,否则将其排除。
然后我想我会使用 HashMap 因为他的操作具有 O(1) 复杂性,这意味着获取和放置值所需的时间不取决于集合的大小,它是延迟可以估计为一个常量值(请看一下散列的概念以及它在这个 Map 实现中的使用方式)。
所以我认为更好的解决方案应该是这样的:
Set<Response> responses = new HashSet<>(); //Or whatever collection you want.
HashMap<String, Variables> mapWithAValues = new HashMap<>(); //Key of type string, Value of type Variables.
// Put in the map every nested variable in the list of Data1
classAResponse.getData1()
.stream()
.flatMap(d -> d.getVariables().stream())
.forEach(v -> mapWithAValues.put(v.getId(), v));
classBResponse.getData2()
.stream()
.forEach(data2 -> {
String id = data2.getId();
// Search for a variable corresponding to this parameter.
Variables aValue = mapWithAValues.get(id);
if(aValue != null) {
List<Parameter> parameters = data2.getParameters();
// Merge data in a Response
Response newResponse = new Response(...);
...
//Add response to the list of responses.
responses.add(newResponse);
}
});
return responses;
此解决方案的复杂度为 O(N),其中 N 是变量数量与可用参数数量的总和。在您的示例中,我认为复杂度为 O(d2^V),因为您需要迭代 V(可用变量数)乘以 d2(可用数据 2 值数)项的列表。
希望这对您有所帮助 :)