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











up vote
7
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 10 hours ago


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
    7
    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 10 hours ago


    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
      7
      down vote

      favorite
      2









      up vote
      7
      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 13 hours ago









      Andrew Tobilko

      23.9k84078




      23.9k84078






      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 14 hours ago









      Ram R

      413




      413




      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 10 hours ago


      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 10 hours ago


      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
          9
          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
            14 hours ago






          • 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
            14 hours ago








          • 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
            12 hours ago






          • 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
            11 hours ago






          • 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
            11 hours ago


















          up vote
          4
          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
            13 hours ago










          • @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
            13 hours ago








          • 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
            13 hours ago


















          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
              0
              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
                9
                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
                  14 hours ago






                • 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
                  14 hours ago








                • 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
                  12 hours ago






                • 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
                  11 hours ago






                • 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
                  11 hours ago















                up vote
                9
                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
                  14 hours ago






                • 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
                  14 hours ago








                • 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
                  12 hours ago






                • 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
                  11 hours ago






                • 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
                  11 hours ago













                up vote
                9
                down vote



                accepted







                up vote
                9
                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 13 hours ago

























                answered 14 hours ago









                Eran

                273k35434518




                273k35434518








                • 2




                  Why not a simple enhanced forloop? When do you choose one over the other?
                  – Luke Garrigan
                  14 hours ago






                • 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
                  14 hours ago








                • 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
                  12 hours ago






                • 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
                  11 hours ago






                • 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
                  11 hours ago














                • 2




                  Why not a simple enhanced forloop? When do you choose one over the other?
                  – Luke Garrigan
                  14 hours ago






                • 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
                  14 hours ago








                • 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
                  12 hours ago






                • 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
                  11 hours ago






                • 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
                  11 hours ago








                2




                2




                Why not a simple enhanced forloop? When do you choose one over the other?
                – Luke Garrigan
                14 hours ago




                Why not a simple enhanced forloop? When do you choose one over the other?
                – Luke Garrigan
                14 hours ago




                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
                14 hours ago






                @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
                14 hours ago






                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
                12 hours ago




                @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
                12 hours ago




                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
                11 hours ago




                @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
                11 hours ago




                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
                11 hours ago




                @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
                11 hours ago












                up vote
                4
                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
                  13 hours ago










                • @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
                  13 hours ago








                • 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
                  13 hours ago















                up vote
                4
                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
                  13 hours ago










                • @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
                  13 hours ago








                • 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
                  13 hours ago













                up vote
                4
                down vote










                up vote
                4
                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 14 hours ago









                nullpointer

                35.5k1071141




                35.5k1071141








                • 4




                  It’s debatable whether Stream.ofNullable(studentConfigs) .flatMap(Collection::stream) … is simpler than studentConfigs != null && studentConfigs.stream() …
                  – Holger
                  13 hours ago










                • @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
                  13 hours ago








                • 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
                  13 hours ago














                • 4




                  It’s debatable whether Stream.ofNullable(studentConfigs) .flatMap(Collection::stream) … is simpler than studentConfigs != null && studentConfigs.stream() …
                  – Holger
                  13 hours ago










                • @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
                  13 hours ago








                • 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
                  13 hours ago








                4




                4




                It’s debatable whether Stream.ofNullable(studentConfigs) .flatMap(Collection::stream) … is simpler than studentConfigs != null && studentConfigs.stream() …
                – Holger
                13 hours ago




                It’s debatable whether Stream.ofNullable(studentConfigs) .flatMap(Collection::stream) … is simpler than studentConfigs != null && studentConfigs.stream() …
                – Holger
                13 hours ago












                @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
                13 hours ago






                @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
                13 hours ago






                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
                13 hours ago




                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
                13 hours ago










                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 13 hours ago

























                    answered 13 hours ago









                    Andrew Tobilko

                    23.9k84078




                    23.9k84078






















                        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 13 hours ago









                            ohlec

                            1,685717




                            1,685717






















                                up vote
                                0
                                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
                                  0
                                  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
                                    0
                                    down vote










                                    up vote
                                    0
                                    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 13 hours ago









                                    Tarun

                                    601414




                                    601414















                                        Popular posts from this blog

                                        サソリ

                                        広島県道265号伴広島線

                                        Setup Asymptote in Texstudio