Optimize Stream Support operations Java










0















I have a ArrayNode like this names "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



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 ... . and to handle when the fist stream return nothing.
Can someone help me, to give some advice on how can I do this better?










share|improve this question
























  • Don't use collect(toList()).get(0): use findFirst().get() (skip the get to handle the "not found" case: it will give you an Optional<JsonNode> instead).

    – Andy Turner
    Nov 14 '18 at 8:56












  • Please add a "json"-tag. I think you deliver the answer yourself. Make separate functions of all you described: check for subjectName "Eclipse" is one function and so one. If you have a lot of small functions it is easier to combine them.

    – Ralf Renz
    Nov 14 '18 at 8:57











  • @RalfRenz can you just help me to understand more what you suggested. So basically you since here I'm checking for 4 values I should do 4 functions to do the check, is this what you're suggesting?

    – Kimberly Fernandez
    Nov 14 '18 at 9:08











  • @RalfRenz it makes sense cause I also have to return a message where it failed in the parent subject code or name or the child subject name and code. but I'm not sure how should I do it, I thought to check if subject data are not null that means that the parent subject is valid .. :/

    – Kimberly Fernandez
    Nov 14 '18 at 9:14











  • Not necessarily. You should try to parameterize as far as possible. The structure of c.get("subjectName").asText().equals(parentSubjectName) is: var1.get(var2).asText().equals(var3). You can make a function of it with three parameters var1, var2, var3 and reuse it.

    – Ralf Renz
    Nov 14 '18 at 9:36















0















I have a ArrayNode like this names "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



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 ... . and to handle when the fist stream return nothing.
Can someone help me, to give some advice on how can I do this better?










share|improve this question
























  • Don't use collect(toList()).get(0): use findFirst().get() (skip the get to handle the "not found" case: it will give you an Optional<JsonNode> instead).

    – Andy Turner
    Nov 14 '18 at 8:56












  • Please add a "json"-tag. I think you deliver the answer yourself. Make separate functions of all you described: check for subjectName "Eclipse" is one function and so one. If you have a lot of small functions it is easier to combine them.

    – Ralf Renz
    Nov 14 '18 at 8:57











  • @RalfRenz can you just help me to understand more what you suggested. So basically you since here I'm checking for 4 values I should do 4 functions to do the check, is this what you're suggesting?

    – Kimberly Fernandez
    Nov 14 '18 at 9:08











  • @RalfRenz it makes sense cause I also have to return a message where it failed in the parent subject code or name or the child subject name and code. but I'm not sure how should I do it, I thought to check if subject data are not null that means that the parent subject is valid .. :/

    – Kimberly Fernandez
    Nov 14 '18 at 9:14











  • Not necessarily. You should try to parameterize as far as possible. The structure of c.get("subjectName").asText().equals(parentSubjectName) is: var1.get(var2).asText().equals(var3). You can make a function of it with three parameters var1, var2, var3 and reuse it.

    – Ralf Renz
    Nov 14 '18 at 9:36













0












0








0








I have a ArrayNode like this names "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



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 ... . and to handle when the fist stream return nothing.
Can someone help me, to give some advice on how can I do this better?










share|improve this question
















I have a ArrayNode like this names "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



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 ... . and to handle when the fist stream return nothing.
Can someone help me, to give some advice on how can I do this better?







java java-8 java-stream filtering






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 17:32









Stefan Zobel

2,47231830




2,47231830










asked Nov 14 '18 at 8:50









Kimberly FernandezKimberly Fernandez

11




11












  • Don't use collect(toList()).get(0): use findFirst().get() (skip the get to handle the "not found" case: it will give you an Optional<JsonNode> instead).

    – Andy Turner
    Nov 14 '18 at 8:56












  • Please add a "json"-tag. I think you deliver the answer yourself. Make separate functions of all you described: check for subjectName "Eclipse" is one function and so one. If you have a lot of small functions it is easier to combine them.

    – Ralf Renz
    Nov 14 '18 at 8:57











  • @RalfRenz can you just help me to understand more what you suggested. So basically you since here I'm checking for 4 values I should do 4 functions to do the check, is this what you're suggesting?

    – Kimberly Fernandez
    Nov 14 '18 at 9:08











  • @RalfRenz it makes sense cause I also have to return a message where it failed in the parent subject code or name or the child subject name and code. but I'm not sure how should I do it, I thought to check if subject data are not null that means that the parent subject is valid .. :/

    – Kimberly Fernandez
    Nov 14 '18 at 9:14











  • Not necessarily. You should try to parameterize as far as possible. The structure of c.get("subjectName").asText().equals(parentSubjectName) is: var1.get(var2).asText().equals(var3). You can make a function of it with three parameters var1, var2, var3 and reuse it.

    – Ralf Renz
    Nov 14 '18 at 9:36

















  • Don't use collect(toList()).get(0): use findFirst().get() (skip the get to handle the "not found" case: it will give you an Optional<JsonNode> instead).

    – Andy Turner
    Nov 14 '18 at 8:56












  • Please add a "json"-tag. I think you deliver the answer yourself. Make separate functions of all you described: check for subjectName "Eclipse" is one function and so one. If you have a lot of small functions it is easier to combine them.

    – Ralf Renz
    Nov 14 '18 at 8:57











  • @RalfRenz can you just help me to understand more what you suggested. So basically you since here I'm checking for 4 values I should do 4 functions to do the check, is this what you're suggesting?

    – Kimberly Fernandez
    Nov 14 '18 at 9:08











  • @RalfRenz it makes sense cause I also have to return a message where it failed in the parent subject code or name or the child subject name and code. but I'm not sure how should I do it, I thought to check if subject data are not null that means that the parent subject is valid .. :/

    – Kimberly Fernandez
    Nov 14 '18 at 9:14











  • Not necessarily. You should try to parameterize as far as possible. The structure of c.get("subjectName").asText().equals(parentSubjectName) is: var1.get(var2).asText().equals(var3). You can make a function of it with three parameters var1, var2, var3 and reuse it.

    – Ralf Renz
    Nov 14 '18 at 9:36
















Don't use collect(toList()).get(0): use findFirst().get() (skip the get to handle the "not found" case: it will give you an Optional<JsonNode> instead).

– Andy Turner
Nov 14 '18 at 8:56






Don't use collect(toList()).get(0): use findFirst().get() (skip the get to handle the "not found" case: it will give you an Optional<JsonNode> instead).

– Andy Turner
Nov 14 '18 at 8:56














Please add a "json"-tag. I think you deliver the answer yourself. Make separate functions of all you described: check for subjectName "Eclipse" is one function and so one. If you have a lot of small functions it is easier to combine them.

– Ralf Renz
Nov 14 '18 at 8:57





Please add a "json"-tag. I think you deliver the answer yourself. Make separate functions of all you described: check for subjectName "Eclipse" is one function and so one. If you have a lot of small functions it is easier to combine them.

– Ralf Renz
Nov 14 '18 at 8:57













@RalfRenz can you just help me to understand more what you suggested. So basically you since here I'm checking for 4 values I should do 4 functions to do the check, is this what you're suggesting?

– Kimberly Fernandez
Nov 14 '18 at 9:08





@RalfRenz can you just help me to understand more what you suggested. So basically you since here I'm checking for 4 values I should do 4 functions to do the check, is this what you're suggesting?

– Kimberly Fernandez
Nov 14 '18 at 9:08













@RalfRenz it makes sense cause I also have to return a message where it failed in the parent subject code or name or the child subject name and code. but I'm not sure how should I do it, I thought to check if subject data are not null that means that the parent subject is valid .. :/

– Kimberly Fernandez
Nov 14 '18 at 9:14





@RalfRenz it makes sense cause I also have to return a message where it failed in the parent subject code or name or the child subject name and code. but I'm not sure how should I do it, I thought to check if subject data are not null that means that the parent subject is valid .. :/

– Kimberly Fernandez
Nov 14 '18 at 9:14













Not necessarily. You should try to parameterize as far as possible. The structure of c.get("subjectName").asText().equals(parentSubjectName) is: var1.get(var2).asText().equals(var3). You can make a function of it with three parameters var1, var2, var3 and reuse it.

– Ralf Renz
Nov 14 '18 at 9:36





Not necessarily. You should try to parameterize as far as possible. The structure of c.get("subjectName").asText().equals(parentSubjectName) is: var1.get(var2).asText().equals(var3). You can make a function of it with three parameters var1, var2, var3 and reuse it.

– Ralf Renz
Nov 14 '18 at 9:36












2 Answers
2






active

oldest

votes


















0














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);






share|improve this answer























  • I would add a map(k -> k.get("states")) in the second stream before the anymatch to Prevent that operation of having to happen twice

    – Dinomaster
    Nov 14 '18 at 10:21











  • @RalfRenz this was helpful

    – Kimberly Fernandez
    Nov 14 '18 at 14:51


















0














I wanted to take into account that subjectName and code are not unique.



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"






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53296178%2foptimize-stream-support-operations-java%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    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);






    share|improve this answer























    • I would add a map(k -> k.get("states")) in the second stream before the anymatch to Prevent that operation of having to happen twice

      – Dinomaster
      Nov 14 '18 at 10:21











    • @RalfRenz this was helpful

      – Kimberly Fernandez
      Nov 14 '18 at 14:51















    0














    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);






    share|improve this answer























    • I would add a map(k -> k.get("states")) in the second stream before the anymatch to Prevent that operation of having to happen twice

      – Dinomaster
      Nov 14 '18 at 10:21











    • @RalfRenz this was helpful

      – Kimberly Fernandez
      Nov 14 '18 at 14:51













    0












    0








    0







    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);






    share|improve this answer













    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);







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 14 '18 at 9:45









    Ralf RenzRalf Renz

    74335




    74335












    • I would add a map(k -> k.get("states")) in the second stream before the anymatch to Prevent that operation of having to happen twice

      – Dinomaster
      Nov 14 '18 at 10:21











    • @RalfRenz this was helpful

      – Kimberly Fernandez
      Nov 14 '18 at 14:51

















    • I would add a map(k -> k.get("states")) in the second stream before the anymatch to Prevent that operation of having to happen twice

      – Dinomaster
      Nov 14 '18 at 10:21











    • @RalfRenz this was helpful

      – Kimberly Fernandez
      Nov 14 '18 at 14:51
















    I would add a map(k -> k.get("states")) in the second stream before the anymatch to Prevent that operation of having to happen twice

    – Dinomaster
    Nov 14 '18 at 10:21





    I would add a map(k -> k.get("states")) in the second stream before the anymatch to Prevent that operation of having to happen twice

    – Dinomaster
    Nov 14 '18 at 10:21













    @RalfRenz this was helpful

    – Kimberly Fernandez
    Nov 14 '18 at 14:51





    @RalfRenz this was helpful

    – Kimberly Fernandez
    Nov 14 '18 at 14:51













    0














    I wanted to take into account that subjectName and code are not unique.



    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"






    share|improve this answer



























      0














      I wanted to take into account that subjectName and code are not unique.



      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"






      share|improve this answer

























        0












        0








        0







        I wanted to take into account that subjectName and code are not unique.



        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"






        share|improve this answer













        I wanted to take into account that subjectName and code are not unique.



        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"







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 10:45









        DinomasterDinomaster

        2366




        2366



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53296178%2foptimize-stream-support-operations-java%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

            Syphilis

            Darth Vader #20