[英]Optimize Stream Support operations Java
I have a ArrayNode like this names "results"
: 我有一个像这样的ArrayNode名称"results"
:
[{
"subjectName": "Eclipse",
"code": "EC1",
"states": [
{
"subjectName": "LunorEx1",
"code":"E1"
},
{
"subjectName": "LunorEx2",
"code":"E2"
},
{
"subjectName": "Expus LunorEx3 ",
"code":"E6"
}]
},
{
"subjectName": "Lasena",
"code": "LS1",
"states": [
{
"subjectName": "SunorEx1",
"code":"S1"
},
{
"subjectName": "SunorEx2",
"code":"S2"
}]
}, {
"subjectName": "Gilesh",
"code": "GL2",
"states": [ ]
}]
this variable is public. 此变量是公共的。
Using Java 8, I want to be able to check if exists for example a subjectName equals to "Eclipse" and code equals to "EC1", if this exists then I want to search inside its json states and find a state that has, for example, a subjectName equals to "LunorEx1" and code "E1", if all these are found I want to return true 使用Java 8,我希望能够检查是否存在,例如subjectName等于“ Eclipse”,代码等于“ EC1”,如果存在,那么我想在其json状态中进行搜索并找到具有例如,subjectName等于“ LunorEx1”和代码“ E1”,如果找到所有这些,我想返回true
private static boolean subjectValidation( String parentSubjectName, String parentCode, String childSubjectName, String childCode){
boolean valid = false;
try {
JsonNode subjectData = StreamSupport.stream(results.spliterator(), true)
.filter(c -> c.get("subjectName").asText().equals(parentSubjectName) &&
c.get("code").asText().equals(parentCode) )
.collect(Collectors.toList()).get(0);
valid = StreamSupport.stream(subjectData.get("states").spliterator(), true)
.anyMatch(k -> k.get("states").get("subjectName").asText().equals(childSubjectName) &&
k.get("states").get("code").asText().equals(childCode));
} catch (Exception e) {
}
return valid;
}
I want to optimize this cause I have several JSON arrays with the same structure and I do several similar checks ... . 我想优化这个原因,因为我有几个具有相同结构的JSON数组,并且执行了多个类似的检查...。 and to handle when the fist stream return nothing. 并在拳头一无所有时处理。 Can someone help me, to give some advice on how can I do this better? 有人可以帮助我,给我一些建议,我如何做得更好?
I thought it could look like this: 我认为它可能看起来像这样:
private static boolean subjectValidation(String parentSubjectName, String parentCode, String childSubjectName,
String childCode) {
boolean valid = false;
try {
Optional<JsonNode> subjectData = StreamSupport.stream(results.spliterator(), true)
.filter(c -> exists(c, "subjectName", parentSubjectName) && exists(c, "code", parentCode))
.findFirst();
if (subjectData.isPresent()) {
valid = StreamSupport.stream(subjectData.get().get("states").spliterator(), true)
.anyMatch(k -> exists(k.get("states"), "subjectName", childSubjectName)
&& exists(k.get("states"), "code", childCode));
}
} catch (Exception e) {
}
return valid;
}
private static boolean exists(JsonNode node, String nodeName, String value) {
return node.get(nodeName).asText().equals(value);
}
I wanted to take into account that subjectName and code are not unique. 我想考虑一下subjectName和代码不是唯一的。
private static boolean subjectValidation(String parentSubjectName, String parentCode, String childSubjectName, String childCode) {
try {
return StreamSupport.stream(results.spliterator(), true)
.filter(c -> hasSubject(c, parentSubjectName) && hasCode(c, parentCode))
.flatmap(s -> StreamSupport.stream(s.get("states").spliterator(), true)
.map(k -> k.get("states"))
.anyMatch(k -> hasSubject(k, childSubjectName) && hasCode(k, childCode));
} catch (Exception e) {
return false;
}
}
private static boolean hasSubject(JsonNode node, String value) {
return fieldHasValue(node, "subjectName", value);
}
private static boolean hasCode(JsonNode node, String value) {
return fieldHasValue(node, "code", value);
}
private static boolean fieldHasValue(JsonNode node, String field, String value) {
return node.get(field).asText().equals(value);
}
There should probably be better exception handling but it should work. 可能应该有更好的异常处理,但是它应该可以工作。
If you want to even further generify this I would make a function which lets you make these checks on the fly simply based on setting variables for "code", "subjectname" and "states" 如果您想进一步泛化,我将提供一个函数,让您可以简单地基于设置“代码”,“主题名称”和“状态”的变量来进行这些检查。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.