error: incompatible types: unexpected return value : Java 8 [duplicate]











up vote
9
down vote

favorite
2













This question already has an answer here:




  • error: incompatible types: unexpected return value Char compare to String

    3 answers




I have written a simple method which returns the boolean value.



private boolean isActionAvailable(Collection<StudentConfiguration> studentConfigs){
if(studentConfigs != null)
{
studentConfigs.forEach(studentConfig -> {
if(studentConfig.action() == null || !studentConfig.action().equals(Action.DELETE)) {
return true;
}
});
}
return false;
}


The method is throwing the following exception .



error: incompatible types: unexpected return value
studentConfigs.forEach(studentConfig ->


What's the problem with my code?










share|improve this question









New contributor




Ram R is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











marked as duplicate by Robert Harvey Nov 26 at 16:06


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















    up vote
    9
    down vote

    favorite
    2













    This question already has an answer here:




    • error: incompatible types: unexpected return value Char compare to String

      3 answers




    I have written a simple method which returns the boolean value.



    private boolean isActionAvailable(Collection<StudentConfiguration> studentConfigs){
    if(studentConfigs != null)
    {
    studentConfigs.forEach(studentConfig -> {
    if(studentConfig.action() == null || !studentConfig.action().equals(Action.DELETE)) {
    return true;
    }
    });
    }
    return false;
    }


    The method is throwing the following exception .



    error: incompatible types: unexpected return value
    studentConfigs.forEach(studentConfig ->


    What's the problem with my code?










    share|improve this question









    New contributor




    Ram R is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.











    marked as duplicate by Robert Harvey Nov 26 at 16:06


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

















      up vote
      9
      down vote

      favorite
      2









      up vote
      9
      down vote

      favorite
      2






      2






      This question already has an answer here:




      • error: incompatible types: unexpected return value Char compare to String

        3 answers




      I have written a simple method which returns the boolean value.



      private boolean isActionAvailable(Collection<StudentConfiguration> studentConfigs){
      if(studentConfigs != null)
      {
      studentConfigs.forEach(studentConfig -> {
      if(studentConfig.action() == null || !studentConfig.action().equals(Action.DELETE)) {
      return true;
      }
      });
      }
      return false;
      }


      The method is throwing the following exception .



      error: incompatible types: unexpected return value
      studentConfigs.forEach(studentConfig ->


      What's the problem with my code?










      share|improve this question









      New contributor




      Ram R is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.












      This question already has an answer here:




      • error: incompatible types: unexpected return value Char compare to String

        3 answers




      I have written a simple method which returns the boolean value.



      private boolean isActionAvailable(Collection<StudentConfiguration> studentConfigs){
      if(studentConfigs != null)
      {
      studentConfigs.forEach(studentConfig -> {
      if(studentConfig.action() == null || !studentConfig.action().equals(Action.DELETE)) {
      return true;
      }
      });
      }
      return false;
      }


      The method is throwing the following exception .



      error: incompatible types: unexpected return value
      studentConfigs.forEach(studentConfig ->


      What's the problem with my code?





      This question already has an answer here:




      • error: incompatible types: unexpected return value Char compare to String

        3 answers








      java foreach java-8 java-stream






      share|improve this question









      New contributor




      Ram R is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Ram R is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited Nov 26 at 12:55









      Andrew Tobilko

      24k84079




      24k84079






      New contributor




      Ram R is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked Nov 26 at 12:14









      Ram R

      513




      513




      New contributor




      Ram R is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Ram R is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Ram R is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




      marked as duplicate by Robert Harvey Nov 26 at 16:06


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






      marked as duplicate by Robert Harvey Nov 26 at 16:06


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          5 Answers
          5






          active

          oldest

          votes

















          up vote
          10
          down vote



          accepted










          The lambda expression passed to forEach shouldn't have a return value.



          It looks like you want to return true if any of the elements of the input Collection satisfies a condition:



          private boolean isActionAvailable(Collection<StudentConfiguration> studentConfigs){
          if(studentConfigs != null) {
          if (studentConfigs.stream().anyMatch(sc -> sc.action() == null || !sc.action().equals(Action.DELETE))) {
          return true;
          }
          }
          return false;
          }


          As Holger suggested, this can be reduced to a single statement:



          return studentConfigs != null && studentConfigs.stream().anyMatch(sc -> sc.action() == null || !sc.action().equals(Action.DELETE));


          or



          return studentConfigs != null ? studentConfigs.stream().anyMatch(sc -> sc.action() == null || !sc.action().equals(Action.DELETE)) : false;





          share|improve this answer



















          • 2




            Why not a simple enhanced forloop? When do you choose one over the other?
            – Luke Garrigan
            Nov 26 at 12:22






          • 1




            @LukeGarrigan Enhanced for loop would also be fine. As to which one to choose, I'd go for the one more readable to you (and to other developers that may read your code). I think using anyMatch in this example better expresses what the method is doing, which is to find whether any of the elements of the input collection satisfies some condition.
            – Eran
            Nov 26 at 12:26








          • 1




            @LukeGarrigan Java is an evolving language. Higher order functions are working their way in. I would suggest you embrace them, rather than get left behind
            – Alexander
            Nov 26 at 14:16






          • 1




            @LukeGarrigan "I suppose it's just because I don't use it as often as I potentially should." Yep, pretty much. This is the future of Java, so you'll only see more and more of these higher order functions.
            – Alexander
            Nov 26 at 15:12






          • 1




            @LukeGarrigan well, you would use collection.contains(obj) instead of looping over the collection to test each element for equality, wouldn’t you? So collection.stream().anyMatch(condition) is just a generalization of the idea, when the condition is more complex than plain equality.
            – Holger
            Nov 26 at 15:23


















          up vote
          5
          down vote













          Alternatively with Java9 and above you can use Stream.ofNullable and update as:



          private boolean isActionAvailable(Collection<StudentConfiguration> studentConfigs) {
          return Stream.ofNullable(studentConfigs)
          .flatMap(Collection::stream)
          .anyMatch(studentConfig -> studentConfig.action() == null || !studentConfig.action().equals(Action.DELETE));
          }





          share|improve this answer

















          • 4




            It’s debatable whether Stream.ofNullable(studentConfigs) .flatMap(Collection::stream) … is simpler than studentConfigs != null && studentConfigs.stream() …
            – Holger
            Nov 26 at 12:54










          • @Holger By simpler here you meant readability aspect or performance as well? Readability I believe would be opinion based, I find it easy to read now, maybe sometimes back I wouldn't have.
            – nullpointer
            Nov 26 at 13:06








          • 2




            I only meant readability when I wrote the comment, but now that you’re asking… There is the lack-of-laziness issue with flatMap which still exists in Java 9, which would be a reason not to use your variant before Java 10. But in recent versions, I wouldn’t think about any remaining minor performance differences.
            – Holger
            Nov 26 at 13:13


















          up vote
          4
          down vote













          I don't recommend you use Stream API here. Look at how clear and simple the foreach version is:



          private boolean isActionAvailable(Collection<StudentConfiguration> studentConfigurations) {
          if(studentConfigurations == null) {
          return false;
          }

          for (StudentConfiguration configuration : studentConfigurations) {
          if (!Action.DELETE.equals(configuration.action())) {
          return true;
          }
          }

          return false;
          }


          Otherwise, if you are a fanatic guy,



          private boolean isActionAvailable(Collection<StudentConfiguration> configs) {
          return configs != null &&
          configs.stream()
          .map(StudentConfiguration::action)
          .anyMatch(Predicate.isEqual​(Action.DELETE).negate()));
          }





          share|improve this answer






























            up vote
            1
            down vote













            The return statement in your lambda will terminate that lambda, not the isActionAvailable() method. Therefore, the inferred type of the lambda is now wrong, because forEach expects a Consumer.



            See the other answers for how to solve that problem.






            share|improve this answer




























              up vote
              1
              down vote













              This is signature of forEach() method forEach(Consumer<? super T> action) .

              It takes reference of Consumer interface which has method void accept(T t).
              In your code you are overriding accept() and returning a value which is not valid as accept() has void return type.

              Therefore it is showing error



               error: incompatible types: unexpected return value
              studentConfigs.forEach(studentConfig ->





              share|improve this answer




























                5 Answers
                5






                active

                oldest

                votes








                5 Answers
                5






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                10
                down vote



                accepted










                The lambda expression passed to forEach shouldn't have a return value.



                It looks like you want to return true if any of the elements of the input Collection satisfies a condition:



                private boolean isActionAvailable(Collection<StudentConfiguration> studentConfigs){
                if(studentConfigs != null) {
                if (studentConfigs.stream().anyMatch(sc -> sc.action() == null || !sc.action().equals(Action.DELETE))) {
                return true;
                }
                }
                return false;
                }


                As Holger suggested, this can be reduced to a single statement:



                return studentConfigs != null && studentConfigs.stream().anyMatch(sc -> sc.action() == null || !sc.action().equals(Action.DELETE));


                or



                return studentConfigs != null ? studentConfigs.stream().anyMatch(sc -> sc.action() == null || !sc.action().equals(Action.DELETE)) : false;





                share|improve this answer



















                • 2




                  Why not a simple enhanced forloop? When do you choose one over the other?
                  – Luke Garrigan
                  Nov 26 at 12:22






                • 1




                  @LukeGarrigan Enhanced for loop would also be fine. As to which one to choose, I'd go for the one more readable to you (and to other developers that may read your code). I think using anyMatch in this example better expresses what the method is doing, which is to find whether any of the elements of the input collection satisfies some condition.
                  – Eran
                  Nov 26 at 12:26








                • 1




                  @LukeGarrigan Java is an evolving language. Higher order functions are working their way in. I would suggest you embrace them, rather than get left behind
                  – Alexander
                  Nov 26 at 14:16






                • 1




                  @LukeGarrigan "I suppose it's just because I don't use it as often as I potentially should." Yep, pretty much. This is the future of Java, so you'll only see more and more of these higher order functions.
                  – Alexander
                  Nov 26 at 15:12






                • 1




                  @LukeGarrigan well, you would use collection.contains(obj) instead of looping over the collection to test each element for equality, wouldn’t you? So collection.stream().anyMatch(condition) is just a generalization of the idea, when the condition is more complex than plain equality.
                  – Holger
                  Nov 26 at 15:23















                up vote
                10
                down vote



                accepted










                The lambda expression passed to forEach shouldn't have a return value.



                It looks like you want to return true if any of the elements of the input Collection satisfies a condition:



                private boolean isActionAvailable(Collection<StudentConfiguration> studentConfigs){
                if(studentConfigs != null) {
                if (studentConfigs.stream().anyMatch(sc -> sc.action() == null || !sc.action().equals(Action.DELETE))) {
                return true;
                }
                }
                return false;
                }


                As Holger suggested, this can be reduced to a single statement:



                return studentConfigs != null && studentConfigs.stream().anyMatch(sc -> sc.action() == null || !sc.action().equals(Action.DELETE));


                or



                return studentConfigs != null ? studentConfigs.stream().anyMatch(sc -> sc.action() == null || !sc.action().equals(Action.DELETE)) : false;





                share|improve this answer



















                • 2




                  Why not a simple enhanced forloop? When do you choose one over the other?
                  – Luke Garrigan
                  Nov 26 at 12:22






                • 1




                  @LukeGarrigan Enhanced for loop would also be fine. As to which one to choose, I'd go for the one more readable to you (and to other developers that may read your code). I think using anyMatch in this example better expresses what the method is doing, which is to find whether any of the elements of the input collection satisfies some condition.
                  – Eran
                  Nov 26 at 12:26








                • 1




                  @LukeGarrigan Java is an evolving language. Higher order functions are working their way in. I would suggest you embrace them, rather than get left behind
                  – Alexander
                  Nov 26 at 14:16






                • 1




                  @LukeGarrigan "I suppose it's just because I don't use it as often as I potentially should." Yep, pretty much. This is the future of Java, so you'll only see more and more of these higher order functions.
                  – Alexander
                  Nov 26 at 15:12






                • 1




                  @LukeGarrigan well, you would use collection.contains(obj) instead of looping over the collection to test each element for equality, wouldn’t you? So collection.stream().anyMatch(condition) is just a generalization of the idea, when the condition is more complex than plain equality.
                  – Holger
                  Nov 26 at 15:23













                up vote
                10
                down vote



                accepted







                up vote
                10
                down vote



                accepted






                The lambda expression passed to forEach shouldn't have a return value.



                It looks like you want to return true if any of the elements of the input Collection satisfies a condition:



                private boolean isActionAvailable(Collection<StudentConfiguration> studentConfigs){
                if(studentConfigs != null) {
                if (studentConfigs.stream().anyMatch(sc -> sc.action() == null || !sc.action().equals(Action.DELETE))) {
                return true;
                }
                }
                return false;
                }


                As Holger suggested, this can be reduced to a single statement:



                return studentConfigs != null && studentConfigs.stream().anyMatch(sc -> sc.action() == null || !sc.action().equals(Action.DELETE));


                or



                return studentConfigs != null ? studentConfigs.stream().anyMatch(sc -> sc.action() == null || !sc.action().equals(Action.DELETE)) : false;





                share|improve this answer














                The lambda expression passed to forEach shouldn't have a return value.



                It looks like you want to return true if any of the elements of the input Collection satisfies a condition:



                private boolean isActionAvailable(Collection<StudentConfiguration> studentConfigs){
                if(studentConfigs != null) {
                if (studentConfigs.stream().anyMatch(sc -> sc.action() == null || !sc.action().equals(Action.DELETE))) {
                return true;
                }
                }
                return false;
                }


                As Holger suggested, this can be reduced to a single statement:



                return studentConfigs != null && studentConfigs.stream().anyMatch(sc -> sc.action() == null || !sc.action().equals(Action.DELETE));


                or



                return studentConfigs != null ? studentConfigs.stream().anyMatch(sc -> sc.action() == null || !sc.action().equals(Action.DELETE)) : false;






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 26 at 12:55

























                answered Nov 26 at 12:19









                Eran

                274k35439521




                274k35439521








                • 2




                  Why not a simple enhanced forloop? When do you choose one over the other?
                  – Luke Garrigan
                  Nov 26 at 12:22






                • 1




                  @LukeGarrigan Enhanced for loop would also be fine. As to which one to choose, I'd go for the one more readable to you (and to other developers that may read your code). I think using anyMatch in this example better expresses what the method is doing, which is to find whether any of the elements of the input collection satisfies some condition.
                  – Eran
                  Nov 26 at 12:26








                • 1




                  @LukeGarrigan Java is an evolving language. Higher order functions are working their way in. I would suggest you embrace them, rather than get left behind
                  – Alexander
                  Nov 26 at 14:16






                • 1




                  @LukeGarrigan "I suppose it's just because I don't use it as often as I potentially should." Yep, pretty much. This is the future of Java, so you'll only see more and more of these higher order functions.
                  – Alexander
                  Nov 26 at 15:12






                • 1




                  @LukeGarrigan well, you would use collection.contains(obj) instead of looping over the collection to test each element for equality, wouldn’t you? So collection.stream().anyMatch(condition) is just a generalization of the idea, when the condition is more complex than plain equality.
                  – Holger
                  Nov 26 at 15:23














                • 2




                  Why not a simple enhanced forloop? When do you choose one over the other?
                  – Luke Garrigan
                  Nov 26 at 12:22






                • 1




                  @LukeGarrigan Enhanced for loop would also be fine. As to which one to choose, I'd go for the one more readable to you (and to other developers that may read your code). I think using anyMatch in this example better expresses what the method is doing, which is to find whether any of the elements of the input collection satisfies some condition.
                  – Eran
                  Nov 26 at 12:26








                • 1




                  @LukeGarrigan Java is an evolving language. Higher order functions are working their way in. I would suggest you embrace them, rather than get left behind
                  – Alexander
                  Nov 26 at 14:16






                • 1




                  @LukeGarrigan "I suppose it's just because I don't use it as often as I potentially should." Yep, pretty much. This is the future of Java, so you'll only see more and more of these higher order functions.
                  – Alexander
                  Nov 26 at 15:12






                • 1




                  @LukeGarrigan well, you would use collection.contains(obj) instead of looping over the collection to test each element for equality, wouldn’t you? So collection.stream().anyMatch(condition) is just a generalization of the idea, when the condition is more complex than plain equality.
                  – Holger
                  Nov 26 at 15:23








                2




                2




                Why not a simple enhanced forloop? When do you choose one over the other?
                – Luke Garrigan
                Nov 26 at 12:22




                Why not a simple enhanced forloop? When do you choose one over the other?
                – Luke Garrigan
                Nov 26 at 12:22




                1




                1




                @LukeGarrigan Enhanced for loop would also be fine. As to which one to choose, I'd go for the one more readable to you (and to other developers that may read your code). I think using anyMatch in this example better expresses what the method is doing, which is to find whether any of the elements of the input collection satisfies some condition.
                – Eran
                Nov 26 at 12:26






                @LukeGarrigan Enhanced for loop would also be fine. As to which one to choose, I'd go for the one more readable to you (and to other developers that may read your code). I think using anyMatch in this example better expresses what the method is doing, which is to find whether any of the elements of the input collection satisfies some condition.
                – Eran
                Nov 26 at 12:26






                1




                1




                @LukeGarrigan Java is an evolving language. Higher order functions are working their way in. I would suggest you embrace them, rather than get left behind
                – Alexander
                Nov 26 at 14:16




                @LukeGarrigan Java is an evolving language. Higher order functions are working their way in. I would suggest you embrace them, rather than get left behind
                – Alexander
                Nov 26 at 14:16




                1




                1




                @LukeGarrigan "I suppose it's just because I don't use it as often as I potentially should." Yep, pretty much. This is the future of Java, so you'll only see more and more of these higher order functions.
                – Alexander
                Nov 26 at 15:12




                @LukeGarrigan "I suppose it's just because I don't use it as often as I potentially should." Yep, pretty much. This is the future of Java, so you'll only see more and more of these higher order functions.
                – Alexander
                Nov 26 at 15:12




                1




                1




                @LukeGarrigan well, you would use collection.contains(obj) instead of looping over the collection to test each element for equality, wouldn’t you? So collection.stream().anyMatch(condition) is just a generalization of the idea, when the condition is more complex than plain equality.
                – Holger
                Nov 26 at 15:23




                @LukeGarrigan well, you would use collection.contains(obj) instead of looping over the collection to test each element for equality, wouldn’t you? So collection.stream().anyMatch(condition) is just a generalization of the idea, when the condition is more complex than plain equality.
                – Holger
                Nov 26 at 15:23












                up vote
                5
                down vote













                Alternatively with Java9 and above you can use Stream.ofNullable and update as:



                private boolean isActionAvailable(Collection<StudentConfiguration> studentConfigs) {
                return Stream.ofNullable(studentConfigs)
                .flatMap(Collection::stream)
                .anyMatch(studentConfig -> studentConfig.action() == null || !studentConfig.action().equals(Action.DELETE));
                }





                share|improve this answer

















                • 4




                  It’s debatable whether Stream.ofNullable(studentConfigs) .flatMap(Collection::stream) … is simpler than studentConfigs != null && studentConfigs.stream() …
                  – Holger
                  Nov 26 at 12:54










                • @Holger By simpler here you meant readability aspect or performance as well? Readability I believe would be opinion based, I find it easy to read now, maybe sometimes back I wouldn't have.
                  – nullpointer
                  Nov 26 at 13:06








                • 2




                  I only meant readability when I wrote the comment, but now that you’re asking… There is the lack-of-laziness issue with flatMap which still exists in Java 9, which would be a reason not to use your variant before Java 10. But in recent versions, I wouldn’t think about any remaining minor performance differences.
                  – Holger
                  Nov 26 at 13:13















                up vote
                5
                down vote













                Alternatively with Java9 and above you can use Stream.ofNullable and update as:



                private boolean isActionAvailable(Collection<StudentConfiguration> studentConfigs) {
                return Stream.ofNullable(studentConfigs)
                .flatMap(Collection::stream)
                .anyMatch(studentConfig -> studentConfig.action() == null || !studentConfig.action().equals(Action.DELETE));
                }





                share|improve this answer

















                • 4




                  It’s debatable whether Stream.ofNullable(studentConfigs) .flatMap(Collection::stream) … is simpler than studentConfigs != null && studentConfigs.stream() …
                  – Holger
                  Nov 26 at 12:54










                • @Holger By simpler here you meant readability aspect or performance as well? Readability I believe would be opinion based, I find it easy to read now, maybe sometimes back I wouldn't have.
                  – nullpointer
                  Nov 26 at 13:06








                • 2




                  I only meant readability when I wrote the comment, but now that you’re asking… There is the lack-of-laziness issue with flatMap which still exists in Java 9, which would be a reason not to use your variant before Java 10. But in recent versions, I wouldn’t think about any remaining minor performance differences.
                  – Holger
                  Nov 26 at 13:13













                up vote
                5
                down vote










                up vote
                5
                down vote









                Alternatively with Java9 and above you can use Stream.ofNullable and update as:



                private boolean isActionAvailable(Collection<StudentConfiguration> studentConfigs) {
                return Stream.ofNullable(studentConfigs)
                .flatMap(Collection::stream)
                .anyMatch(studentConfig -> studentConfig.action() == null || !studentConfig.action().equals(Action.DELETE));
                }





                share|improve this answer












                Alternatively with Java9 and above you can use Stream.ofNullable and update as:



                private boolean isActionAvailable(Collection<StudentConfiguration> studentConfigs) {
                return Stream.ofNullable(studentConfigs)
                .flatMap(Collection::stream)
                .anyMatch(studentConfig -> studentConfig.action() == null || !studentConfig.action().equals(Action.DELETE));
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 26 at 12:33









                nullpointer

                36.2k1071143




                36.2k1071143








                • 4




                  It’s debatable whether Stream.ofNullable(studentConfigs) .flatMap(Collection::stream) … is simpler than studentConfigs != null && studentConfigs.stream() …
                  – Holger
                  Nov 26 at 12:54










                • @Holger By simpler here you meant readability aspect or performance as well? Readability I believe would be opinion based, I find it easy to read now, maybe sometimes back I wouldn't have.
                  – nullpointer
                  Nov 26 at 13:06








                • 2




                  I only meant readability when I wrote the comment, but now that you’re asking… There is the lack-of-laziness issue with flatMap which still exists in Java 9, which would be a reason not to use your variant before Java 10. But in recent versions, I wouldn’t think about any remaining minor performance differences.
                  – Holger
                  Nov 26 at 13:13














                • 4




                  It’s debatable whether Stream.ofNullable(studentConfigs) .flatMap(Collection::stream) … is simpler than studentConfigs != null && studentConfigs.stream() …
                  – Holger
                  Nov 26 at 12:54










                • @Holger By simpler here you meant readability aspect or performance as well? Readability I believe would be opinion based, I find it easy to read now, maybe sometimes back I wouldn't have.
                  – nullpointer
                  Nov 26 at 13:06








                • 2




                  I only meant readability when I wrote the comment, but now that you’re asking… There is the lack-of-laziness issue with flatMap which still exists in Java 9, which would be a reason not to use your variant before Java 10. But in recent versions, I wouldn’t think about any remaining minor performance differences.
                  – Holger
                  Nov 26 at 13:13








                4




                4




                It’s debatable whether Stream.ofNullable(studentConfigs) .flatMap(Collection::stream) … is simpler than studentConfigs != null && studentConfigs.stream() …
                – Holger
                Nov 26 at 12:54




                It’s debatable whether Stream.ofNullable(studentConfigs) .flatMap(Collection::stream) … is simpler than studentConfigs != null && studentConfigs.stream() …
                – Holger
                Nov 26 at 12:54












                @Holger By simpler here you meant readability aspect or performance as well? Readability I believe would be opinion based, I find it easy to read now, maybe sometimes back I wouldn't have.
                – nullpointer
                Nov 26 at 13:06






                @Holger By simpler here you meant readability aspect or performance as well? Readability I believe would be opinion based, I find it easy to read now, maybe sometimes back I wouldn't have.
                – nullpointer
                Nov 26 at 13:06






                2




                2




                I only meant readability when I wrote the comment, but now that you’re asking… There is the lack-of-laziness issue with flatMap which still exists in Java 9, which would be a reason not to use your variant before Java 10. But in recent versions, I wouldn’t think about any remaining minor performance differences.
                – Holger
                Nov 26 at 13:13




                I only meant readability when I wrote the comment, but now that you’re asking… There is the lack-of-laziness issue with flatMap which still exists in Java 9, which would be a reason not to use your variant before Java 10. But in recent versions, I wouldn’t think about any remaining minor performance differences.
                – Holger
                Nov 26 at 13:13










                up vote
                4
                down vote













                I don't recommend you use Stream API here. Look at how clear and simple the foreach version is:



                private boolean isActionAvailable(Collection<StudentConfiguration> studentConfigurations) {
                if(studentConfigurations == null) {
                return false;
                }

                for (StudentConfiguration configuration : studentConfigurations) {
                if (!Action.DELETE.equals(configuration.action())) {
                return true;
                }
                }

                return false;
                }


                Otherwise, if you are a fanatic guy,



                private boolean isActionAvailable(Collection<StudentConfiguration> configs) {
                return configs != null &&
                configs.stream()
                .map(StudentConfiguration::action)
                .anyMatch(Predicate.isEqual​(Action.DELETE).negate()));
                }





                share|improve this answer



























                  up vote
                  4
                  down vote













                  I don't recommend you use Stream API here. Look at how clear and simple the foreach version is:



                  private boolean isActionAvailable(Collection<StudentConfiguration> studentConfigurations) {
                  if(studentConfigurations == null) {
                  return false;
                  }

                  for (StudentConfiguration configuration : studentConfigurations) {
                  if (!Action.DELETE.equals(configuration.action())) {
                  return true;
                  }
                  }

                  return false;
                  }


                  Otherwise, if you are a fanatic guy,



                  private boolean isActionAvailable(Collection<StudentConfiguration> configs) {
                  return configs != null &&
                  configs.stream()
                  .map(StudentConfiguration::action)
                  .anyMatch(Predicate.isEqual​(Action.DELETE).negate()));
                  }





                  share|improve this answer

























                    up vote
                    4
                    down vote










                    up vote
                    4
                    down vote









                    I don't recommend you use Stream API here. Look at how clear and simple the foreach version is:



                    private boolean isActionAvailable(Collection<StudentConfiguration> studentConfigurations) {
                    if(studentConfigurations == null) {
                    return false;
                    }

                    for (StudentConfiguration configuration : studentConfigurations) {
                    if (!Action.DELETE.equals(configuration.action())) {
                    return true;
                    }
                    }

                    return false;
                    }


                    Otherwise, if you are a fanatic guy,



                    private boolean isActionAvailable(Collection<StudentConfiguration> configs) {
                    return configs != null &&
                    configs.stream()
                    .map(StudentConfiguration::action)
                    .anyMatch(Predicate.isEqual​(Action.DELETE).negate()));
                    }





                    share|improve this answer














                    I don't recommend you use Stream API here. Look at how clear and simple the foreach version is:



                    private boolean isActionAvailable(Collection<StudentConfiguration> studentConfigurations) {
                    if(studentConfigurations == null) {
                    return false;
                    }

                    for (StudentConfiguration configuration : studentConfigurations) {
                    if (!Action.DELETE.equals(configuration.action())) {
                    return true;
                    }
                    }

                    return false;
                    }


                    Otherwise, if you are a fanatic guy,



                    private boolean isActionAvailable(Collection<StudentConfiguration> configs) {
                    return configs != null &&
                    configs.stream()
                    .map(StudentConfiguration::action)
                    .anyMatch(Predicate.isEqual​(Action.DELETE).negate()));
                    }






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 26 at 12:57

























                    answered Nov 26 at 12:45









                    Andrew Tobilko

                    24k84079




                    24k84079






















                        up vote
                        1
                        down vote













                        The return statement in your lambda will terminate that lambda, not the isActionAvailable() method. Therefore, the inferred type of the lambda is now wrong, because forEach expects a Consumer.



                        See the other answers for how to solve that problem.






                        share|improve this answer

























                          up vote
                          1
                          down vote













                          The return statement in your lambda will terminate that lambda, not the isActionAvailable() method. Therefore, the inferred type of the lambda is now wrong, because forEach expects a Consumer.



                          See the other answers for how to solve that problem.






                          share|improve this answer























                            up vote
                            1
                            down vote










                            up vote
                            1
                            down vote









                            The return statement in your lambda will terminate that lambda, not the isActionAvailable() method. Therefore, the inferred type of the lambda is now wrong, because forEach expects a Consumer.



                            See the other answers for how to solve that problem.






                            share|improve this answer












                            The return statement in your lambda will terminate that lambda, not the isActionAvailable() method. Therefore, the inferred type of the lambda is now wrong, because forEach expects a Consumer.



                            See the other answers for how to solve that problem.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 26 at 12:50









                            ohlec

                            1,685717




                            1,685717






















                                up vote
                                1
                                down vote













                                This is signature of forEach() method forEach(Consumer<? super T> action) .

                                It takes reference of Consumer interface which has method void accept(T t).
                                In your code you are overriding accept() and returning a value which is not valid as accept() has void return type.

                                Therefore it is showing error



                                 error: incompatible types: unexpected return value
                                studentConfigs.forEach(studentConfig ->





                                share|improve this answer

























                                  up vote
                                  1
                                  down vote













                                  This is signature of forEach() method forEach(Consumer<? super T> action) .

                                  It takes reference of Consumer interface which has method void accept(T t).
                                  In your code you are overriding accept() and returning a value which is not valid as accept() has void return type.

                                  Therefore it is showing error



                                   error: incompatible types: unexpected return value
                                  studentConfigs.forEach(studentConfig ->





                                  share|improve this answer























                                    up vote
                                    1
                                    down vote










                                    up vote
                                    1
                                    down vote









                                    This is signature of forEach() method forEach(Consumer<? super T> action) .

                                    It takes reference of Consumer interface which has method void accept(T t).
                                    In your code you are overriding accept() and returning a value which is not valid as accept() has void return type.

                                    Therefore it is showing error



                                     error: incompatible types: unexpected return value
                                    studentConfigs.forEach(studentConfig ->





                                    share|improve this answer












                                    This is signature of forEach() method forEach(Consumer<? super T> action) .

                                    It takes reference of Consumer interface which has method void accept(T t).
                                    In your code you are overriding accept() and returning a value which is not valid as accept() has void return type.

                                    Therefore it is showing error



                                     error: incompatible types: unexpected return value
                                    studentConfigs.forEach(studentConfig ->






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 26 at 12:56









                                    Tarun

                                    611414




                                    611414















                                        Popular posts from this blog

                                        Plaza Victoria

                                        In PowerPoint, is there a keyboard shortcut for bulleted / numbered list?

                                        How to put 3 figures in Latex with 2 figures side by side and 1 below these side by side images but in...