[英]Java Streams : how to filter object based on key value
I have a class as below which is mapped to the response of one of the api request.我有一个 class 如下,它映射到 api 请求之一的响应。 The response json is nothing but RootResponse[]
响应 json 只不过是 RootResponse[]
public class RootResponse {
String header;
String tailer;
List<Map<Object, subRecords>> transactions;
DcfSummary summary;
}
The json response which I received is mapped to the class as map<string, Object>我收到的 json 响应映射到 class 作为 map<string, Object>
below is the sample json response, In the transactions which is ( List<Map<Object, subRecords>> transactions;) there is a key for example "789983333" which is dynamic.下面是示例 json 响应,在 (List<Map<Object, subRecords>> transactions;) 的事务中,有一个键,例如“789983333”,它是动态的。 so what I want to achieve if I give the key then I want to get that object which is
所以如果我给出密钥我想实现的目标然后我想得到那个 object 这是
"789983333": {
"r_01": "record1",
"r_02": "record2",
"r_03": "record3",
"r_04": "record4"
}
}
[{
"header": "header1",
"transactions": [{
"789983333": {
"r_01": "record1",
"r_02": "record2",
"r_03": "record3",
"r_04": "record4"
}
}],
"tailer": "tailer1",
"summary": {
"item1": "370840",
"item2": "0",
"item3": "0",
"item4": "0",
"item5": "0",
"item6": "0",
"totaltransactions": "00000000"
}
},
{
"header": "header1",
"transactions": [{
"789983333": {
"r_01": "record1",
"r_02": "record2",
"r_03": "record3",
"r_04": "record4"
}
}],
"tailer": "tailer1",
"summary": {
"item1": "370840",
"item2": "0",
"item3": "0",
"item4": "0",
"item5": "0",
"item6": "0",
"totaltransactions": "00000000"
}
}
]
I was trying to use streams in java but no luck.我试图在 java 中使用流,但没有运气。 The idea is once I get the object based on the reference key then I can get records and validate the response.
这个想法是,一旦我基于参考密钥获得 object,我就可以获取记录并验证响应。 tried various options not able to get it.
尝试了各种无法获得的选项。 Any help would be appreciated.
任何帮助,将不胜感激。
Found the solution找到了解决方案
for (Map.Entry<String, Object> entry : RootResponse.entrySet()){
RootResponse[] ls = (RootResponse[]) entry.getValue();
for(RootResponse a : ls){
List<Map<Object, SubRecords>> dcfrecord = (List<Map<Object, SubRecords>>) a.getTransactions();
Optional<SubRecords> subRecords = Optional.of((SubRecords) dcfrecord.stream().flatMap(key -> key.entrySet().stream().filter(k1 -> k1.getKey().equals(rrn))).map(value -> value.getValue()).findFirst().get());
SubRecords subrecord = SubRecords.get();
System.out.println(gen4record.getR_6220());
if(gen4record != null){
break;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.