error: incompatible types: unexpected return value : Java 8 [duplicate]
up vote
8
down vote
favorite
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?
java foreach java-8 java-stream
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♦ 2 days 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.
add a comment |
up vote
8
down vote
favorite
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?
java foreach java-8 java-stream
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♦ 2 days 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.
add a comment |
up vote
8
down vote
favorite
up vote
8
down vote
favorite
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?
java foreach java-8 java-stream
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
java foreach java-8 java-stream
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.
edited 2 days ago
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 2 days ago
Ram R
463
463
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♦ 2 days 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♦ 2 days 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.
add a comment |
add a comment |
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;
2
Why not a simple enhanced forloop? When do you choose one over the other?
– Luke Garrigan
2 days 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 usinganyMatchin 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
2 days 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
2 days 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
2 days ago
1
@LukeGarrigan well, you would usecollection.contains(obj)instead of looping over the collection to test each element for equality, wouldn’t you? Socollection.stream().anyMatch(condition)is just a generalization of the idea, when the condition is more complex than plain equality.
– Holger
2 days ago
|
show 2 more comments
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));
}
4
It’s debatable whetherStream.ofNullable(studentConfigs) .flatMap(Collection::stream) …is simpler thanstudentConfigs != null && studentConfigs.stream() …
– Holger
2 days 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
2 days ago
2
I only meant readability when I wrote the comment, but now that you’re asking… There is the lack-of-laziness issue withflatMapwhich 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
2 days ago
add a comment |
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()));
}
add a comment |
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.
add a comment |
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 ->
add a comment |
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;
2
Why not a simple enhanced forloop? When do you choose one over the other?
– Luke Garrigan
2 days 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 usinganyMatchin 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
2 days 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
2 days 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
2 days ago
1
@LukeGarrigan well, you would usecollection.contains(obj)instead of looping over the collection to test each element for equality, wouldn’t you? Socollection.stream().anyMatch(condition)is just a generalization of the idea, when the condition is more complex than plain equality.
– Holger
2 days ago
|
show 2 more comments
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;
2
Why not a simple enhanced forloop? When do you choose one over the other?
– Luke Garrigan
2 days 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 usinganyMatchin 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
2 days 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
2 days 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
2 days ago
1
@LukeGarrigan well, you would usecollection.contains(obj)instead of looping over the collection to test each element for equality, wouldn’t you? Socollection.stream().anyMatch(condition)is just a generalization of the idea, when the condition is more complex than plain equality.
– Holger
2 days ago
|
show 2 more comments
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;
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;
edited 2 days ago
answered 2 days ago
Eran
274k35435519
274k35435519
2
Why not a simple enhanced forloop? When do you choose one over the other?
– Luke Garrigan
2 days 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 usinganyMatchin 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
2 days 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
2 days 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
2 days ago
1
@LukeGarrigan well, you would usecollection.contains(obj)instead of looping over the collection to test each element for equality, wouldn’t you? Socollection.stream().anyMatch(condition)is just a generalization of the idea, when the condition is more complex than plain equality.
– Holger
2 days ago
|
show 2 more comments
2
Why not a simple enhanced forloop? When do you choose one over the other?
– Luke Garrigan
2 days 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 usinganyMatchin 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
2 days 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
2 days 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
2 days ago
1
@LukeGarrigan well, you would usecollection.contains(obj)instead of looping over the collection to test each element for equality, wouldn’t you? Socollection.stream().anyMatch(condition)is just a generalization of the idea, when the condition is more complex than plain equality.
– Holger
2 days ago
2
2
Why not a simple enhanced forloop? When do you choose one over the other?
– Luke Garrigan
2 days ago
Why not a simple enhanced forloop? When do you choose one over the other?
– Luke Garrigan
2 days 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
2 days 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
2 days 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
2 days 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
2 days 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
2 days 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
2 days 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
2 days 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
2 days ago
|
show 2 more comments
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));
}
4
It’s debatable whetherStream.ofNullable(studentConfigs) .flatMap(Collection::stream) …is simpler thanstudentConfigs != null && studentConfigs.stream() …
– Holger
2 days 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
2 days ago
2
I only meant readability when I wrote the comment, but now that you’re asking… There is the lack-of-laziness issue withflatMapwhich 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
2 days ago
add a comment |
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));
}
4
It’s debatable whetherStream.ofNullable(studentConfigs) .flatMap(Collection::stream) …is simpler thanstudentConfigs != null && studentConfigs.stream() …
– Holger
2 days 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
2 days ago
2
I only meant readability when I wrote the comment, but now that you’re asking… There is the lack-of-laziness issue withflatMapwhich 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
2 days ago
add a comment |
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));
}
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));
}
answered 2 days ago
nullpointer
35.9k1071142
35.9k1071142
4
It’s debatable whetherStream.ofNullable(studentConfigs) .flatMap(Collection::stream) …is simpler thanstudentConfigs != null && studentConfigs.stream() …
– Holger
2 days 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
2 days ago
2
I only meant readability when I wrote the comment, but now that you’re asking… There is the lack-of-laziness issue withflatMapwhich 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
2 days ago
add a comment |
4
It’s debatable whetherStream.ofNullable(studentConfigs) .flatMap(Collection::stream) …is simpler thanstudentConfigs != null && studentConfigs.stream() …
– Holger
2 days 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
2 days ago
2
I only meant readability when I wrote the comment, but now that you’re asking… There is the lack-of-laziness issue withflatMapwhich 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
2 days ago
4
4
It’s debatable whether
Stream.ofNullable(studentConfigs) .flatMap(Collection::stream) … is simpler than studentConfigs != null && studentConfigs.stream() …– Holger
2 days ago
It’s debatable whether
Stream.ofNullable(studentConfigs) .flatMap(Collection::stream) … is simpler than studentConfigs != null && studentConfigs.stream() …– Holger
2 days 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
2 days 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
2 days 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
2 days 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
2 days ago
add a comment |
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()));
}
add a comment |
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()));
}
add a comment |
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()));
}
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()));
}
edited 2 days ago
answered 2 days ago
Andrew Tobilko
24k84079
24k84079
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered 2 days ago
ohlec
1,685717
1,685717
add a comment |
add a comment |
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 ->
add a comment |
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 ->
add a comment |
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 ->
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 ->
answered 2 days ago
Tarun
613414
613414
add a comment |
add a comment |